Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jsSHA and Python hashlib give different results for same input

The following snippets use Nodejs and Python to calculate a hash from the same input content, but they give different results. It’s weird.

// npm install jssha
const jssha = require("jssha");
var s = new jssha("SHA-1", "TEXT");
s.setHMACKey("abc", "TEXT")
s.update("123")
console.log(s.getHMAC("B64"))

The result is vpEGplDt4B9KMf3iOB0G9ftz5hI=

import hmac
import hashlib
import base64

hashed = hmac.new(b"abc", b"TEXT", hashlib.sha1)
hashed.update("123".encode('utf-8'))
print(base64.b64encode(hashed.digest()).decode())

The result is f1/O4xLhhZgwtm6IMAwLDmjzQgg=

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

this will get the same hmac as javascript:

import hmac
import hashlib
import base64

hashed = hmac.new(key=b"abc", msg=b"", digestmod=hashlib.sha1)
hashed.update("123".encode('utf-8'))
print(base64.b64encode(hashed.digest()).decode())
# vpEGplDt4B9KMf3iOB0G9ftz5hI=

i do not know anything about this javascript api, but i guess "TEXT" tells javascript how the data/key is encoded.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading