简体   繁体   English

与python相比,在javascript中编码哈希给出不同的结果

[英]Encoding Hash in javascript gives different result compared to python

I am trying to convert a javascript program into python but I am getting different results, after creating the hash they are the same but after encoding the results they are way off the python one as it is much longer then the javascript version.我正在尝试将 javascript 程序转换为 python,但我得到不同的结果,在创建哈希后它们是相同的,但在对结果进行编码后,它们与 python 程序相差甚远,因为它比 javascript 版本长得多。

Javascript result: 'DqnyGCG5qW5DPceKe1p7fFyFxqdXrqZhQiN2Ukp-2h0' Javascript 结果: 'DqnyGCG5qW5DPceKe1p7fFyFxqdXrqZhQiN2Ukp-2h0'

Python result: 'NGQ4MWNiYWY3NzgyZmIyZjE3YWEzNWQyNGE1Mjg4M2M2ZmI1MmQxNGE4MDFmNDMxM2FkZWRlOTE1NjVhZGU0YQ' Python 结果: 'NGQ4MWNiYWY3NzgyZmIyZjE3YWEzNWQyNGE1Mjg4M2M2ZmI1MmQxNGE4MDFmNDMxM2FkZWRlOTE1NjVhZGU0YQ'

Javascript Javascript

var hash = CryptoJS.HmacSHA256(token.join("."), secret);
// Same hash on both
var base64Hash = CryptoJS.enc.Base64.stringify(hash);
document.write(urlConvertBase64(base64Hash));
// Results are different now

function urlConvertBase64(input) {
  var output = input.replace(/=+$/, '');
  output = output.replace(/\+/g, '-');
  output = output.replace(/\//g, '_');

  return output;
}

Python Python

signatureHash = hmac.new(secret.encode(), tokenHB, hashlib.sha256).hexdigest()
// Same hash on both
signature = base64.urlsafe_b64encode(signatureHash.encode())
signature = signature.rstrip('='.encode())
// Results are different now

print(signature)

You encoded two different things between the Node.js and Python.您在 Node.js 和 Python 之间编码了两种不同的东西。 In Node.js you directly used the raw (bytes value of the) hash, while in Python you used the hex encoded value of the same hash.在 Node.js 中,您直接使用原始(字节值)散列,而在 Python 中,您使用相同散列的十六进制编码值。 Depending on which output you desire, you will need to either stringify the hex output in Node.js, or base64 encode the raw hash in Python.根据您想要的输出,您需要在 Node.js 中对十六进制输出进行字符串化,或者在 Python 中对原始哈希进行 base64 编码。

The Node.js code that will replicate both versions of the output (your urlConvertBase64 implementation omitted from example, but is used):将复制两个版本的输出的 Node.js 代码(示例中省略了您的urlConvertBase64实现,但已使用):

const CryptoJS = require('crypto-js');

var token = 'the token';
var data_to_hash = 'thesecret\x00\x00\x02';
var hashed = CryptoJS.HmacSHA256(data_to_hash, token);
var hexdigest = hashed.toString(CryptoJS.enc.Hex);
var raw_base64 = urlConvertBase64(CryptoJS.enc.Base64.stringify(hashed));
var hex_base64 = urlConvertBase64(Buffer.from(hexdigest).toString('base64'));

console.log(`raw + base64: ${raw_base64}`);
console.log(`hex + base64: ${hex_base64}`);

The Python code that will replicate the above:将复制上述内容的 Python 代码:

import hashlib
import hmac
from base64 import urlsafe_b64encode

token = b'the token'
data_to_hash = b'thesecret\x00\x00\x02'
hashed = hmac.new(token, data_to_hash, hashlib.sha256)
hexdigest = hashed.hexdigest().encode()
raw_base64 = urlsafe_b64encode(hashed.digest()).decode().rstrip('=')
hex_base64 = urlsafe_b64encode(hexdigest).decode().rstrip('=')
print(f'raw + base64: {raw_base64}')
print(f'hex + base64: {hex_base64}')

Both versions produce the following output:两个版本都产生以下输出:

raw + base64: R1oLspOvlQ6X_rzaudD8dk8Kqo-nu9pgij-f_-4tnxo
hex + base64: NDc1YTBiYjI5M2FmOTUwZTk3ZmViY2RhYjlkMGZjNzY0ZjBhYWE4ZmE3YmJkYTYwOGEzZjlmZmZlZTJkOWYxYQ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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