I need to reproduce a JS function that hashes a string with SHA-256 in r.
The said function is:
function hashPhrase (phrase) {
const buf = new ArrayBuffer(phrase.length * 2)
const bufView = new Uint16Array(buf)
const strLen = phrase.length
for (let i = 0; i < strLen; i++) {
bufView[i] = phrase.charCodeAt(i)
}
return window.crypto.subtle.digest('SHA-256', buf)
.then(hashArrayBuffer => {
let binary = ''
const bytes = new Uint8Array(hashArrayBuffer)
const len = bytes.byteLength
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i])
}
return Promise.resolve(window.btoa(binary))
})
}
Calling the function:
hashPhrase('test').then(x => { console.log(x) })
gives me:
/lIGdrGh2T2rqyMZ7qA2dPNjLq7rFj0eiCRPXrHeEOs=
as output.
I load openssl and try to use sha256 as function to hash the string.
library(openssl)
phrase = "test"
phraseraw = charToRaw(phrase)
base64_encode(sha256(phraseraw))
and the output is:
[1] "n4bQgYhMfWWaL+qgxVrQFaO/TxsrC4Is0V1sFbDwCgg="
Don't know if the problem is the uint16 because in both cases I guess that the variable is being passed as raw.
I'll appreciate very much any help.