I am migrating function using Google's CryptoJS to browser native WebCryptography API.
With CryptoJS, I use the following code to get the words array of SHA1 hash
CryptoJS.SHA1('1w0g6Ixf2VHvOc+6pGBqDHItFYQ=:9590467').words
and it returns [888149035, -1762573935, 1178769020, -1914057363, 481296384]
With WebCrypto API, I use
const msgBuffer = new TextEncoder().encode('1w0g6Ixf2VHvOc+6pGBqDHItFYQ=:9590467')
const hashBuffer = await crypto.subtle.digest('SHA-1', msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
and hashArray
is [52, 240, 20, 43, 150, 241, 65, 145, 70, 66, 150, 124, 141, 233, 205, 109, 28, 176, 0, 0]
How can I transform this result to the same array as CyptoJS?
Would've been nice if were as simple as
const msgBuffer = new TextEncoder().encode('1w0g6Ixf2VHvOc+6pGBqDHItFYQ=:9590467')
const hashBuffer = await crypto.subtle.digest('SHA-1', msgBuffer);
const hashArray = Array.from(new Int32Array(hashBuffer));
except for endianness - so, it's just a little more complex
async function test() {
const msgBuffer = new TextEncoder().encode('1w0g6Ixf2VHvOc+6pGBqDHItFYQ=:9590467')
const hashBuffer = await crypto.subtle.digest('SHA-1', msgBuffer);
const dv = new DataView(hashBuffer);
const hashArray = [];
for(let i = 0; i < dv.byteLength; i+=4) {
hashArray.push(dv.getInt32(i, false));
}
return hashArray;
};
test().then(console.log);