2

I'm trying to port a Python script to Node and I've become stuck on SHA1 hashes.

The following Python code:

import hashlib

user = 'test'

ret = hashlib.sha1(user.encode('utf-8')).digest()
print(ret);

Prints out:

b'\xa9J\x8f\xe5\xcc\xb1\x9b\xa6\x1cL\x08s\xd3\x91\xe9\x87\x98/\xbb\xd3'

I need a SHA1 hash in this format in Node. This Javascript:

var crypto = require('crypto');

var generator = crypto.createHash('sha1');
generator.update(new Buffer('test'));
console.log(generator.digest('binary'));

prints

©Jå̱sÓé/»Ó

How can I get Node to produce the output in the same style as Python does? It's clearly not binary or hex, what format is the python output in?

3
  • The Python output is a byte string, which uses ASCII to represent bytes in the printable 7 bit ASCII range and hex escape codes for other byte values. In hex, it looks like a94a8fe5ccb19ba61c4c0873d391e987982fbbd3. The JavaScript output is the Latin-1 encoding of those bytes. Commented Jul 31, 2016 at 14:06
  • You see two different representation of the same data \xa9 is © in iso-8859-1 encoding. You second string just seems to be truncated. Commented Jul 31, 2016 at 14:06
  • python is fine, and it is an exact binary string, nodejs output is converted to non binary when you use console.log Commented Jul 31, 2016 at 14:06

1 Answer 1

3

Both results are the same already. The string representation of bytes (the stuff you see on the console) differs in JavaScript and Python though. To prove that they're identical, convert both to an integer list:

> 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]

You can use hexdigest/digest('hex') to get a hexadecimal string ('a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'), which may be easier to handle.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, my problem must lie elsewhere. I'm communicating a server that needs a sha1 hash and it's not authenticating me so I must be sending the data wrong.
Yes, that sounds likely. If all else fails, compare pcap dumps to see which exact bytes are differing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.