I'm trying to port a Python script to Node and I've become stuck on SHA1 hashes.
The following Python code:
user = 'test'
ret = hashlib.sha1(user.encode('utf-8')).digest()
var crypto = require('crypto');
var generator = crypto.createHash('sha1');
> var crypto = require('crypto'); > var generator = crypto.createHash('sha1'); > generator.update(new Buffer('test')); > var digest = generator.digest('binary'); > var lst = ; > for (let i = 0;i < digest.length;i++) st.push(digest.charCodeAt(i)); > console.log(JSON.stringify(lst)); [169,74,143,229,204,177,155,166,28,76,8,115,211,145,233,135,152,47,187,211]
Same result in python:
>>> import hashlib, base64 >>> ret = hashlib.sha1('test'.encode('utf-8')).digest() >>> print(list(ret)) [169, 74, 143, 229, 204, 177, 155, 166, 28, 76, 8, 115, 211, 145, 233, 135, 152, 47, 187, 211]
But there's nothing wrong with the bytes; again, it's just the string representation that's different. For instance, if you write both bytes to a file, then both files will be totally identical.