[英]Convert HMAC-SHA384 function from Java to JavaScript
This is the java function that does the job.这是完成这项工作的 java function。
static byte[] HmacSHA384(String data, byte[] key) {
try {
mac.init(new SecretKeySpec(key, HMAC_ALGORITHM));
} catch (InvalidKeyException e) {
//throw new PWAINUnRecoverableException("Invalid key exception while mac init", e);
throw new RuntimeException("Invalid key exception while mac init", e);
}
return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
}
I've already tried this answer with no luck.我已经尝试过这个答案,但没有运气。
Used crypto-js
module also as:使用crypto-js
模块也作为:
const result = crypto_js.HmacSHA384(key, data).toString();
console.log(result.split('').map(getBytes)); // used this to match byte array from java output. No luck!
function getBytes(x) {return x.charCodeAt(0);}
Note that I'm using key
as a String in JavaScript code as crypto_js.HmacSHA384
accepts String only and not a byte[]
.请注意,我在 JavaScript 代码中将key
用作字符串,因为crypto_js.HmacSHA384
仅接受字符串而不接受byte[]
。 But I've compared my string's byte[]
with java code's byte[]
.但是我已经将我的字符串的byte[]
与 java 代码的byte[]
进行了比较。
Output from JS code: Output 来自 JS 代码:
[97,104,69,80,106,49,121,83,100,87,120,56,76,65,101,71,54,52,81,110,104,108,72,55,54,56,112,104,56,109,72,101,68,43,83,70,73,89,54,81,71,53,49,98,48,119,115,117,72,83,112,117,99,110,107,76,113,78,105,73,76,107,120,82]
Output from Java code: Output 来自 Java 代码:
[-29, 76, -16, -110, -35, -87, 18, -53, -1, -105, -77, -96, -49, -110, 102, -74, -110, 31, -87, 115, 102, -43, 101, -89, -82, -8, 96, -99, -89, 103, -128, 104, -121, -107, -98, 18, -18, 85, 97, -121, 30, 91, -42, -11, -6, -58, -7, 113]
Both takes same input.两者都采用相同的输入。
Any help will be appreciated.任何帮助将不胜感激。 Thanks!谢谢!
In JavaScript it's not so easy to output raw bytes.在 JavaScript 中,output 原始字节并不容易。 See the answer https://stackoverflow.com/a/29433028/7873775 for the details.有关详细信息,请参阅答案https://stackoverflow.com/a/29433028/7873775 。
But the byte arrays will still be different in Java and JavaScript because in JavaScript the Uint8Array
represents an array of 8-bit unsigned integers while in Java byte
range is from -128 to 127. But the byte arrays will still be different in Java and JavaScript because in JavaScript the Uint8Array
represents an array of 8-bit unsigned integers while in Java byte
range is from -128 to 127.
So Uint8Array
[132, 179]
in Java byte[]
is [-124, -77]
.所以Uint8Array
byte[]
中的 Uint8Array [132, 179]
是[-124, -77]
。
To output the HMAC value in JavaScript you need to encode it in either Hex or Base64.对于 output JavaScript 中的 HMAC 值,您需要将其编码为十六进制或 Base64。
Here is Java code using commons-codec library:这是使用commons-codec库的 Java 代码:
byte[] key = "Secret Passphrase".getBytes(StandardCharsets.UTF_8);
String valueToDigest = "Message";
byte[] hmac = new HmacUtils(HMAC_SHA_384, key).hmac(valueToDigest);
System.out.println(asList(hmac));
System.out.println(Hex.encodeHexString(hmac));
System.out.println(Base64.encodeBase64String(hmac));
The result:结果:
84b318cc0232a370c1f8b8746afcb575fc2debc680122c7422fd425638896d0dcf9e905b8cd9c1d7aed8d5439a2a2328
十六进制: 84b318cc0232a370c1f8b8746afcb575fc2debc680122c7422fd425638896d0dcf9e905b8cd9c1d7aed8d5439a2a2328
hLMYzAIyo3DB+Lh0avy1dfwt68aAEix0Iv1CVjiJbQ3PnpBbjNnB167Y1UOaKiMo
Base64: hLMYzAIyo3DB+Lh0avy1dfwt68aAEix0Iv1CVjiJbQ3PnpBbjNnB167Y1UOaKiMo
Here is code with CryptoJS library:这是CryptoJS库的代码:
const CryptoJS = require("crypto-js");
const hash = CryptoJS.HmacSHA384("Message", "Secret Passphrase");
console.log(hash.toString(CryptoJS.enc.Hex));
console.log(hash.toString(CryptoJS.enc.Base64));
And the result is identical :结果是相同的:
84b318cc0232a370c1f8b8746afcb575fc2debc680122c7422fd425638896d0dcf9e905b8cd9c1d7aed8d5439a2a2328
十六进制: 84b318cc0232a370c1f8b8746afcb575fc2debc680122c7422fd425638896d0dcf9e905b8cd9c1d7aed8d5439a2a2328
hLMYzAIyo3DB+Lh0avy1dfwt68aAEix0Iv1CVjiJbQ3PnpBbjNnB167Y1UOaKiMo
Base64: hLMYzAIyo3DB+Lh0avy1dfwt68aAEix0Iv1CVjiJbQ3PnpBbjNnB167Y1UOaKiMo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.