我在node.js中有这段代码:
crypto = require('crypto');
secret = 'e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35';
secret2 = 'E8F32E723DECF4051AEFAC8E2C93C9C5B214313817CDB01A1494B917C8436B35'; // upper case
theString = "800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D"; // upper case
theString2 = "800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d";
hmac = crypto.createHmac('sha512', secret)
.update(theString, 'hex')
.digest('hex');
console.log("HMAC : " + hmac);
使用secret和theString
HMAC = 588cf3e244ae6a6fa3db9761a32f715dc50e080b1b427229654af67e453c3f0d6456975095e32e3c8e68af386e19cb1ef3c1d8b546a8af0279be2fe43bf91c08
使用secret和theString2
HMAC = 588cf3e244ae6a6fa3db9761a32f715dc50e080b1b427229654af67e453c3f0d6456975095e32e3c8e68af386e19cb1ef3c1d8b546a8af0279be2fe43bf91c08
hmac是相同的=> theString被解释为十六进制
使用secret2和theString
HMAC = 1cc29c3b964ac964a960e3d9b82b9db6b4df3cc3675d60e25fdd9dee64672a9ce2dfa86afb25c8684416f88b47f6e16981029574fcc144e4be05114e2c059e23
hmac不同=> secret被解释为字符串(ASCII或unicode ...)
问题:有没有办法将秘密指定为十六进制?
答案 0 :(得分:3)
答案 1 :(得分:0)
更新:备注
根据文档,Hmac#update没有编码 选项,所以不要使用未记录的功能。
- 如果我不在更新
中使用'hex'选项withString
crypto.createHmac('sha512', new Buffer(secret, 'hex'))
.update(theString)
hmac = 8df515ab391531d68e5bc29c91fc21771b10dbdf29f24d0eaa92d40a48d8faa2ee432356d72d771d2eb97b2be071b235056df8707327869f0c9830ea5814158b
使用theString2
crypto.createHmac('sha512', new Buffer(secret, 'hex'))
.update(theString2)
hmac = 5ba6e9eaea4e28a9ad197b519ba72d8f5070ac8208dd135ec6adc17cbaa70ed6279c698c95feeaf55b2b936d02f92c05901ff7c786b70c74fb2bb8beefdb6e65
hmac是differents => theString和theString2被解释为字符串
- 如果我在更新
中使用'hex'选项withString
crypto.createHmac('sha512', new Buffer(secret, 'hex'))
.update(theString, 'hex')
hmac = 4135e2908192c404f776a273b0877437417da0605b385ced1cdf0ffe9446877436896baf8f5439848c1c6fa095d1a2fb01ef2a077db58b15fb4ef63dbc92a6e5
使用theString2
crypto.createHmac('sha512', new Buffer(secret, 'hex'))
.update(theString2, 'hex')
hmac = 4135e2908192c404f776a273b0877437417da0605b385ced1cdf0ffe9446877436896baf8f5439848c1c6fa095d1a2fb01ef2a077db58b15fb4ef63dbc92a6e5
hmac是相同的=> theString和theString2被解释为十六进制 =>选项似乎有效!!! 但我会使用缓冲区