0

I've been stuck in this for a while, for the life of me.

I have this thing in python3

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)
# 4ff521a9b87ddd0dea00a842f8f5d72819f9df0a

but I cannot GET THIS SAME HASH in NodeJS, I've tried a lot of solutions; at first I though it was the first part, the encode to utf-8, because printing that was returning different results, but it seems like that's not it, it was just different representation of the same string.

My approach in JS:

const crypto = require('crypto') // maybe another library that works in browser? 
const msg = "6NmByERB9ZDJX9OKDtIzpGl8ei7KBiYI"+"\n"+"1623776851607"+"\n"+"/api2/v2/users/me"+"\n"+"0"
let shasum = crypto.createHash('sha1')
shasum.update(JSON.stringify(msg))
let hashed_string = shasum.digest('hex')
console.log(hashed_string)
// c838ca6f79551d828d6e4a810bd49c1df07b54a3

thanks for any help :)

4
  • Why JSON.stringify(msg)? Commented Jun 21, 2021 at 22:05
  • msg !== JSON.stringify(msg). It adds " at the start and end Commented Jun 21, 2021 at 22:07
  • 1
    @Samathingamajig It does even more. It replaces newline with \n. msg has length 66 and JSON.stringify(msg) has length 71. Commented Jun 21, 2021 at 22:08
  • OMG, I really though I had to do something equivalent to the python's encode("utf-8") , that's why I added the stringify and encodeURI and other methods before, but no, without that, just works, what a journey, thanks for commenting, another dumb error that took me days to resolve. Commented Jun 21, 2021 at 22:10

1 Answer 1

1

Don't JSON.stringify the message. JSON.stringify adds double quotes at the beginning and end and it replaces all newlines (and most other whitespace characters) with two characters \n. msg has length 66 and JSON.stringify(msg) has length 71. This code generates the expected SHA1 value:

const crypto = require('crypto') // maybe another library that works in browser? 
const msg = "6NmByERB9ZDJX9OKDtIzpGl8ei7KBiYI"+"\n"+"1623776851607"+"\n"+"/api2/v2/users/me"+"\n"+"0"
let shasum = crypto.createHash('sha1')
shasum.update(msg)
let hashed_string = shasum.digest('hex')
console.log(hashed_string)

Here you can see the differences between msg and JSON.stringify(msg):

const msg = "6NmByERB9ZDJX9OKDtIzpGl8ei7KBiYI"+"\n"+"1623776851607"+"\n"+"/api2/v2/users/me"+"\n"+"0"

console.log(msg)
console.log(JSON.stringify(msg));
console.log(msg.length)
console.log(JSON.stringify(msg).length);
console.log(JSON.stringify(msg) === msg);

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

Comments

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.