在 .net 中计算 HMACSHA256 相当于 reactjs

Compute HMACSHA256 in .net equivalent to reactjs

我正在从 .net core 迁移到 reactjs,需要计算哈希值。我卡在了这段结果不相等的代码中。这里是.net的代码,_normKey和inputBytes是字节数组,hash中存储的结果也是字节数组。我怎样才能把它转换成 reactjs。

    _normkey = [150, 68, 231, 118, 4, 194, 153, 75, 25, 195, 123, 71, 133, 198, 103, 2, 169, 52, 8, 149, 68, 220, 132, 39, 183, 158, 132, 105, 77, 48, 18, 243, 211, 178, 144, 109, 73, 36, 254, 215, 175, 134, 92, 49, 5, 216, 170, 123, 75, 26, 232, 181, 129, 76, 22, 223, 167, 110, 52, 249, 189, 128, 66, 3];
inputBytes = [49, 46, 48, 53, 51, 46, 48, 46, 55, 52, 51, 48, 52, 50, 49, 54, 65, 45, 67, 51, 68, 70, 45, 52, 55, 48, 66, 45, 66, 68, 67, 48, 45, 67, 57, 57, 55, 48, 54, 67, 51, 48, 65, 65, 69, 49, 46, 48, 53, 73, 110, 116, 101, 103, 114, 97, 65, 80, 73, 48, 101, 112, 97, 108, 101, 110, 122, 117, 101, 108, 97, 50, 64, 105, 110, 116, 101, 103, 114, 97, 112, 97, 114, 107, 105, 110, 103, 46, 99, 111, 109, 49, 50, 51, 52, 53, 49, 48, 48, 48, 55, 49, 51, 54, 49, 51, 53, 46, 46]
        byte[] hash = new HMACSHA256(_normKey).ComputeHash(inputBytes);
        if (hash.Length >= 8)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = hash.Length - 8; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            strRes = sb.ToString();
        }

这里是Reactjs的代码

var hash = CryptoJS.HmacSHA256(_normKey.toString(), inputBytes.toString());
console.log(hash)

当我在没有 toString 的情况下传递 _normkey 和 inputBytes 时,它抛出错误 key.clamps is not a function

C#代码的输出是:

79E024C70ABA8B39

CryptoJS 不能直接处理数组,只能WordArrays:

  • _normkeyinputBytes 可以转换为类型化数组。类型化数组可以用作 WordArray.create() 的参数,从而转换为 WordArray.
  • HmacSHA256() returns 可以使用 hex encoder of CryptoJS. This encoding corresponds to the encoding used in the C# code. If needed the hex string can be converted to an array, see e.g. here.
  • 转换为十六进制字符串的 WordArray

一个可能的实现是:

var _normkey = [150, 68, 231, 118, 4, 194, 153, 75, 25, 195, 123, 71, 133, 198, 103, 2, 169, 52, 8, 149, 68, 220, 132, 39, 183, 158, 132, 105, 77, 48, 18, 243, 211, 178, 144, 109, 73, 36, 254, 215, 175, 134, 92, 49, 5, 216, 170, 123, 75, 26, 232, 181, 129, 76, 22, 223, 167, 110, 52, 249, 189, 128, 66, 3]
var inputBytes = [49, 46, 48, 53, 51, 46, 48, 46, 55, 52, 51, 48, 52, 50, 49, 54, 65, 45, 67, 51, 68, 70, 45, 52, 55, 48, 66, 45, 66, 68, 67, 48, 45, 67, 57, 57, 55, 48, 54, 67, 51, 48, 65, 65, 69, 49, 46, 48, 53, 73, 110, 116, 101, 103, 114, 97, 65, 80, 73, 48, 101, 112, 97, 108, 101, 110, 122, 117, 101, 108, 97, 50, 64, 105, 110, 116, 101, 103, 114, 97, 112, 97, 114, 107, 105, 110, 103, 46, 99, 111, 109, 49, 50, 51, 52, 53, 49, 48, 48, 48, 55, 49, 51, 54, 49, 51, 53, 46, 46]

var normKeyWA = CryptoJS.lib.WordArray.create(new Uint8Array(_normkey));          // Convert into WordArray via typed array
var inputBytesWA = CryptoJS.lib.WordArray.create(new Uint8Array(inputBytes));

var hash = CryptoJS.HmacSHA256(
      inputBytesWA,
      normKeyWA
);

var last8BytesWA = CryptoJS.lib.WordArray.create(hash.words.slice((32 - 8) / 4));
var last8BytesHex = last8BytesWA.toString(CryptoJS.enc.Hex).toUpperCase();        // Convert into hex encoded string using CryptoJS's Hex encoder
var last8BytesArray = hexToBytes(last8BytesHex);                                  // Convert into array

console.log(last8BytesHex);
console.log(last8BytesArray);

// from 
function hexToBytes(hex) {
    for (var bytes = [], c = 0; c < hex.length; c += 2)
        bytes.push(parseInt(hex.substr(c, 2), 16));
    return bytes;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

或者,数组和 WordArray 之间的转换可以显式完成,例如实施 here.