0

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

Pete
  • 1
  • 1
  • You should provide sample data and outputs you tried both of these with. Chances are you don't need the call to `hexdigest`. nor the call to `decode` – pvg Dec 24 '16 at 15:18
  • 1
    you tried `digest()` instead of `hexdigest` function ?, provide sample outputs for clarification – Renjith Thankachan Dec 24 '16 at 15:27
  • 1
    Like @sideffect0 says, `hexdigest` is one way of serializing the digest but you want a base64 encoding instead. So, `hash256 = hmac.new(Secret, rawSignature, hashlib.sha256).digest()`. Does that work? Let us know so sideeffect0 can answer the question and people will know its done. – tdelaney Dec 24 '16 at 16:05

0 Answers0