2

I have done a script in Python which is:

hashed_string = hashlib.sha1(str(string_to_hash).encode('utf-8')).hexdigest()

and it works as I want, but I can't figure out how to do it in JavaScript. I have done this in JS:

const crypto = require('crypto') 
let shasum = crypto.createHash('sha1') 
let hashed_string = shasum.update(JSON.stringify(string_to_hash).digest('hex'))

But the result is not the same.

1
  • Can you pls share an example of the results of each and their differences? Commented Feb 19, 2019 at 8:47

1 Answer 1

4

You are calling hash.digest() inside hash.update(), but digest() needs to be called after update()

e.g.

const crypto = require('crypto') 
let shasum = crypto.createHash('sha1')
shasum.update(JSON.stringify(string_to_hash)) 
let hashed_string = shasum.digest('hex'))

or

const crypto = require('crypto') 
let shasum = crypto.createHash('sha1')
let hashed_string = shasum.update(JSON.stringify(string_to_hash)).digest('hex'))

or

const crypto = require('crypto') 
let hashed_string = crypto.createHash('sha1').update(JSON.stringify(string_to_hash)).digest('hex'))

Provided you are using the exact same string in Python that the JSON.stringify() method returns you will get the same result. Any additional characters will affect the result.

For example, here are the generated SHA1 hashes for some similar strings.

#1: {a:1,b:2}          // ca681fb779d3b6f82af9b243c480ce4fb07e7af4
#2: {a:1, b:2}         // 6327727c37c8d1893d9e341453dd1b8c7e72ffe8
#3: {"a":1,"b":2}      // 4acc71e0547112eb432f0a36fb1924c4a738cb49
#4: {"a":1, "b":2}     // 98e0e65ec27728cd01356be19e354d92fb2f4b46
#5: {"a":"1", "b":"2"} // a89dd0ae872ef448a6ddafc23b0752b799fe0de1

Javascript:

d = {a:1, b:2}  // Simple object
JSON.stringify(d) // {"a":1,"b":2} : #3 Above

Python:

d = {"a":1, "b":2}
str(d)
"{'a': 1, 'b': 2}"

The string created in Python uses single quotes and is formatted with additional space characters so the resulting hash will not be the same.

#6: {'a': 1, 'b': 2} // 326a92518b2b2bd864ff2d88eab7c12ca44d3fd3
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your complex answer. I leaned two new things about hashing from your answer. But I had to send the code to my Laravel backend and hash the code from there. It worked as expected. So I cant say what the problem has been in my case. But thanks again.
this is not working for me, I have the exact same problem as asked here python msg = "6NmByERB9ZDJX9OKDtIzpGl8ei7KBiYI"+"\n"+"1623776851607"+"\n"+"/api2/v2/users/me"+"\n"+"0" ; message = msg.encode('utf-8') ; hash_object = hashlib.sha1(message) ; sha_1_sign = hash_object.hexdigest() ; print('sha_1_sign', sha_1_sign) ; # 4ff521a9b87ddd0dea00a842f8f5d72819f9df0a but I cannot get the same hashed in anyway in JS, your approach gives me js // c838ca6f79551d828d6e4a810bd49c1df07b54a3 any thoughts @Dan ?
@Jumper Create a new question please and include both the Python and JavaScript code and I'll take a look at it.

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.