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

2024/4/15 1:51:25

I am now trying to encode the string using HMAC-SHA256 using Java. The encoded string required to match another set of encoded string generated by Python using hmac.new(mySecret, myPolicy, hashlib.sha256).hexdigest(). I have tried

    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(), "HmacSHA256");sha256_HMAC.init(secretKey);byte[] hash = sha256_HMAC.doFinal(policy.getBytes());byte[] hexB = new Hex().encode(hash);String check = Hex.encodeHexString(hash);String sha256 = DigestUtils.sha256Hex(secret.getBytes());

after I print them out, hash, hexB, check and sha256 didn't provide the same result as the following Python encryption method

hmac.new(mySecret, myPolicy, hashlib.sha256).hexdigest()

So, I have try to looking for the library or something that work similar to the above Python function. Can anybody help me out?

Answer

Are you sure your key and input are identical and correctly encoded in both java and python?

HMAC-SHA256 works the same on both platforms.

Java

Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec("1234".getBytes(), "HmacSHA256");
sha256_HMAC.init(secretKey);
byte[] hash = sha256_HMAC.doFinal("test".getBytes());
String check = Hex.encodeHexString(hash);
System.out.println(new String(check));Output
24c4f0295e1bea74f9a5cb5bc40525c8889d11c78c4255808be00defe666671f

Python

print hmac.new("1234", "test", hashlib.sha256).hexdigest();Output
24c4f0295e1bea74f9a5cb5bc40525c8889d11c78c4255808be00defe666671f
https://en.xdnf.cn/q/73006.html

Related Q&A

How to get response from scrapy.Request without callback?

I want to send a request and wait for a response from the server in order to perform action-dependent actions. I write the followingresp = yield scrapy.Request(*kwargs)and got None in resp. In document…

install error thinks pythonpath is empty

I am trying to install the scikits.nufft package here I download the zip file, unpack and cd to the directory. It contains a setup.py file so I run python setup.py installbut it gives me the following …

Conditionally installing importlib on python2.6

I have a python library that has a dependency on importlib. importlib is in the standard library in Python 2.7, but is a third-party package for older pythons. I typically keep my dependencies in a pip…

Python/pandas: Find matching values from two dataframes and return third value

I have two different dataframes (df1, df2) with completely different shapes: df1: (64, 6); df2: (564, 9). df1 contains a column (df1.objectdesc) which has values (strings) that can also be found in a c…

random.choice broken with dicts

The random.choice input is supposed to be a sequence. This causes odd behavior with a dict, which is not a sequence type but can be subscripted like one: >>> d = {0: spam, 1: eggs, 3: potato} …

Tornado [Errno 24] Too many open files [duplicate]

This question already has an answer here:Tornado "error: [Errno 24] Too many open files" error(1 answer)Closed 9 years ago.We are running a Tornado 3.0 service on a RedHat OS and getting the …

How to check if an RGB image contains only one color?

Im using Python and PIL.I have images in RGB and I would like to know those who contain only one color (say #FF0000 for example) or a few very close colors (#FF0000 and #FF0001).I was thinking about us…

python requests and cx_freeze

I am trying to freeze a python app that depends on requests, but I am getting the following error:Traceback (most recent call last):File "c:\Python33\lib\site-packages\requests\packages\urllib3\ut…

django changing a date field to integer field cant migrate

I recently changed a date field to an integer field (the data was specified in number of months remaining rather than a date). However all though the make migrations command works fine when I attempt t…

Sqlalchemy get row in timeslot

I have a model called Appointment which has the columns datetime which is a DateTime field and duration which is an Integer field and represents duration in minutes. Now I want to check if func.now() i…