简体   繁体   中英

Equivalent CryptoJS SHA512 hash of content Hex encoded for python?

I am trying to make an authenticated API call. Documentation example in JS states that contentHash header has to be populated with:

content = {currencySymbol:symbol};
var contentHash = CryptoJS.SHA512(JSON.stringify(content)).toString(CryptoJS.enc.Hex); 

I am writing my request in Python and I keep getting INVALID_CONTENT_HASH. This is what I have so far:

content = {"currencySymbol":"BTC"}
contentHash = hashlib.sha512(json.dumps(content, separators=(',',':')).encode('latin1')).hexdigest()

Hash: 18aa13a42b5efe3c45b37050626f23fcc4f3b4acf5e479f2f50212722cdcd80baa0ba6ddf8dcbd0f1b88008aaf9ec33874e4be9ab6ec2dece611c2dbdca87560

Expected Hash: e6e0b5e9941269e61fb41bd4f6a0dc5be0da1c011087a057ff88fe3de98f41c6681983dd14662c88b6293663d942c93511c3fd7f9cc5250e686b16de53a18f57

I don't know what is wrong with my request. Please advice.

Your problem is that json.dumps applies some minor pretty-printing by default but JSON.stringify does not.

Try this:

content = {'currencySymbol':'BTC'}
contentHash = hashlib.sha512(json.dumps(content, separators=(',',':')).encode('latin1')).hexdigest()

By adding the separators=(',',':') to json.dumps , it will remove the extra whitespace that is added by default

documentation for json.dumps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM