Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Added python3 example
Source Link
Artjom B.
  • 62k
  • 26
  • 137
  • 236

You can't simply send bytes to JavaScript. You need to convert this to a textual representation for it to be comparable. Hex encoding is supported by both python's hmac module and CryptoJS.

CryptoJS:

CryptoJS.HmacSHA256("test", "secret").toString(CryptoJS.enc.Hex) 

PythonPython2:

hmac.new("secret", "test", hashlib.sha256).hexdigest() 
hmac.new("secret", "test", hashlib.sha256).hexdigest() 

Python3:

hmac.new("secret".encode("utf-8"), "test".encode("utf-8"), hashlib.sha256).hexdigest() 

Note the difference in the argument ordering.

BothAll produce

0329a06b62cd16b33eb6792be8c60b158d89a2ee3a876fce9a881ebb488c0914 

You can't simply send bytes to JavaScript. You need to convert this to a textual representation for it to be comparable. Hex encoding is supported by both python's hmac module and CryptoJS.

CryptoJS:

CryptoJS.HmacSHA256("test", "secret").toString(CryptoJS.enc.Hex) 

Python:

hmac.new("secret", "test", hashlib.sha256).hexdigest() 

Note the difference in the argument ordering.

Both produce

0329a06b62cd16b33eb6792be8c60b158d89a2ee3a876fce9a881ebb488c0914 

You can't simply send bytes to JavaScript. You need to convert this to a textual representation for it to be comparable. Hex encoding is supported by both python's hmac module and CryptoJS.

CryptoJS:

CryptoJS.HmacSHA256("test", "secret").toString(CryptoJS.enc.Hex) 

Python2:

hmac.new("secret", "test", hashlib.sha256).hexdigest() 

Python3:

hmac.new("secret".encode("utf-8"), "test".encode("utf-8"), hashlib.sha256).hexdigest() 

Note the difference in the argument ordering.

All produce

0329a06b62cd16b33eb6792be8c60b158d89a2ee3a876fce9a881ebb488c0914 
Source Link
Artjom B.
  • 62k
  • 26
  • 137
  • 236

You can't simply send bytes to JavaScript. You need to convert this to a textual representation for it to be comparable. Hex encoding is supported by both python's hmac module and CryptoJS.

CryptoJS:

CryptoJS.HmacSHA256("test", "secret").toString(CryptoJS.enc.Hex) 

Python:

hmac.new("secret", "test", hashlib.sha256).hexdigest() 

Note the difference in the argument ordering.

Both produce

0329a06b62cd16b33eb6792be8c60b158d89a2ee3a876fce9a881ebb488c0914