NoobEditor
NoobEditor

Reputation: 15871

java equivalent to python for hashing

I have below code in java documentation (it takes secret_key and data as input) :

javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1")
mac.init(new javax.crypto.spec.SecretKeySpec(secret_key.getBytes(), "HmacSHA1"))
byte[] hexBytes = new org.apache.commons.codec.binary.Hex().encode(mac.doFinal(data.getBytes()))
String signature = new String(hexBytes, "UTF-8")

after doing some RnD online , i wrote equivalent python to :

decodedKey = secret_key.decode("hex")
hmac_val = hmac.new(decodedKey, data.encode('UTF-8'), hashlib.sha1)
signature = hmac_val.digest().encode('base64')

but on doing a post request with this signature value in header, i am getting

ValueError: Invalid header value 'XXXXXXXXXX'

is my python equivalent correct? it would be great help if someone can explain!

EDIT

Java

public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
        String secret_key = "c84766ca4a3ce52c3602bbf02ad1f7";
        String data = "some data";
        javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
        mac.init(new javax.crypto.spec.SecretKeySpec(secret_key.getBytes(), "HmacSHA1"));
        byte[] hexBytes = new org.apache.commons.codec.binary.Hex().encode(mac.doFinal(data.getBytes()));
        String signature = new String(hexBytes, "UTF-8");
        System.out.println("signature : "+signature);
 }

o/p

signature : 2b565c0476eed0f350ddb3a2852a4cab91281bdc

Python :

In [1]: import hmac

In [2]: import hashlib

In [3]: secret_key = "c84766ca4a3ce52c3602bbf02ad1f7"

In [4]: data = "some data"

In [5]: decodedKey = secret_key.decode("hex")

In [6]: hmac_val = hmac.new(decodedKey, data.encode('UTF-8'), hashlib.sha1)

In [7]: signature = hmac_val.digest().encode('base64')

In [8]: signature
Out[8]: '3qE5SqSdvBEJcy8mSF+srqNXCd4=\n'

In [9]:

Upvotes: 3

Views: 2666

Answers (3)

NoobEditor
NoobEditor

Reputation: 15871

Referrring to this thread :

Java method which can provide the same output as Python method for HMAC-SHA256 in Hex

minor tweeking for sha1, below is simple equivalent :

In [13]: print hmac.new(secret_key, data, hashlib.sha1).hexdigest()
2b565c0476eed0f350ddb3a2852a4cab91281bdc

Upvotes: 1

ralf htp
ralf htp

Reputation: 9412

if you want it easy try this: https://pythonhosted.org/pycrypto/Crypto.Hash.HMAC-module.html

maybe the encoding influences the result, [UTF-8] then [base-64]

Upvotes: 0

ralf htp
ralf htp

Reputation: 9412

pycrypto has a hash function https://pypi.python.org/pypi/pycrypto

because of ValueError: Invalid header value 'XXXXXXXXXX' see this thread ValueError: Invalid header value 'H2O Python client/2.7.9 (default, Apr 2 2015, 15:33:21) \n[GCC 4.9.2]'

maybe the header in your post is not compatible with the library you use for the post

what libraries do you import in the python code ?

Upvotes: 0

Related Questions