javascript Base64 encoding to python

352 views Asked by At

I have this line in a postman pre-script that uses javascript:

var hash256 = CryptoJS.HmacSHA256(rawSignature, Secret);
var hashbase64 = hash256.toString(CryptoJS.enc.Base64);

im trying to convert that to a python script (2.7) and have come up with this :

hash256 = hmac.new(Secret, rawSignature, hashlib.sha256).hexdigest()
hashbase64 = base64.b64encode(hash256).decode()

The sha256 result appears to be the same but when I try to encode with base64 I get a different result. I have little to no javascript knowledge but from what I have found it may be something to do with .toString part converting from a word array and may be resulting in the string being base64 encoded in a different format.

I have tried a few different things converting to binary / hex strings and converting etc but still get the wrong result I'm sure if something stupid but can't quite figure it out any ideas ?

thanks

***edit

clarification as requested

an example correctly generated HMAC sha256 was: d5d2f2c64933c4d45f457f32d892534971d372cfec99956db506afad0e12aefb

the above codes produces these results: hashbase64(javascript postman) = 1dLyxkkzxNRfRX8y2JJTSXHTcs/smZVttQavrQ4Srvs= hashbase64(python) = ZDVkMmYyYzY0OTMzYzRkNDVmNDU3ZjMyZDg5MjUzNDk3MWQzNzJjZmVjOTk5NTZkYjUwNmFmYWQwZTEyYWVmYg=='

I have now tried the digest() instead of hexdigest and am now getting the desired result so that was the problem, thanks for the responses appreciate it

0

There are 0 answers