Wednesday 17 May 2017

Difference between NodeJS and Python Hashing

I'm attempting to port some NodeJS code to Python 3, but am having trouble getting a SHA1 hash to behave identically.

This code in each language shows the problem:

NodeJS

var crypto = require('crypto');
crypto.createHash('sha1');
var h = crypto.createHash('sha1');
h.update(new Buffer('pXHepU2vIdYJuIAN', 'base64').toString('binary'));
console.log(h.digest('hex')); // ea70f5b1ec762290cefd37bc0f9a7421dcc93466

Python 3

import hashlib
import base64
h = hashlib.sha1()
h.update(base64.b64decode(b'pXHepU2vIdYJuIAN'))
print(h.hexdigest()) # 74161d2d37d9fff312dd396d5f779133c4bfd88d

I think I'm passing the Python input in the wrong format as if I remove the .toString('binary') from the NodeJS code it matches.

Could anyone explain to me what the Python code should be, and why they behave differently?



via gacrux

No comments:

Post a Comment