Reputation: 61
I am migrating from .net core to reactjs and need to calculate hash. I stuck in this code where results are not equal. Here is the code of .net, _normKey and inputBytes are byte arrays and result stored in hash is also byte array. How can i convert this into 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();
}
Here is the code of Reactjs
var hash = CryptoJS.HmacSHA256(_normKey.toString(), inputBytes.toString());
console.log(hash)
When I pass _normkey and inputBytes without toString, It throws error key.clamps is not a function
Upvotes: 1
Views: 897
Reputation: 49131
The output of the C# code is:
79E024C70ABA8B39
CryptoJS cannot process arrays directly, only WordArray
s:
_normkey
and inputBytes
can be converted to typed arrays. A typed array can be used as an argument of WordArray.create()
and thus converted into a WordArray
.HmacSHA256()
returns a WordArray
that can be converted to a hex string using the 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.A possible implementation is:
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 https://stackoverflow.com/a/34356351/9014097
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>
Alternatively, the conversion between array and WordArray
can be done explicitly, e.g. with the implementation here.
Upvotes: 1