Hello,
I’m stuck with a piece of code in a prerequest script. i need to sha256 crypt a string (a password) the same way of my c# methode. i tried all i know but can’t succeed .
result excepted :
"Azerty123!" ==> "6e+FpD3ksBVgmMDgPLOPr98ZQGyhORmUARKxjX9mlT8="
C# code
string encryptedPassWord = "";
using (var sha = SHA256.Create())
{
var computedHash = sha.ComputeHash(Encoding.Unicode.GetBytes(password));
encryptedPassWord = Convert.ToBase64String(computedHash);
}
return Ok(encryptedPassWord);
The result is the same as on SHA256 - Online Tools , with input type =“UTF-16LE” and output type =“Base64”
what i tried :
function strToUtf16Bytes(str) {
const bytes = [];
for (ii = 0; ii < str.length; ii++) {
const code = str.charCodeAt(ii); // x00-xFFFF
bytes.push(code & 255, code >> 8); // low, high
//bytes.push('0'); // low, high
console.log("ii = " + ii + " " + code + ' ' + bytes.length); // 'My Message or Password'
}
for (ii = 0; ii < bytes.length; ii++) {
console.log("ii bytes = " + bytes[ii]); // 'My Message or Password'
}
console.log(bytes.length)
return bytes;
}
console.clear();
--pm.globals.set("email", encodeURIComponent("[email protected]"));
var clearP = 'Azerty123!';
// Azerty123!
// 6e+FpD3ksBVgmMDgPLOPr98ZQGyhORmUARKxjX9mlT8=
//var te = TextEncoder.Unicode();
//var rr = strToUtf16Bytes("abc");
var clearPutf = strToUtf16Bytes(clearP.toString(CryptoJS.enc.Utf16LE))
console.log(clearPutf[18])
var sha256Hash = CryptoJS.SHA256(clearPutf);
console.log("sha256Hash :: " + sha256Hash)
var p64 = sha256Hash.toString(CryptoJS.enc.Base64);
console.log("p64 = " + p64); // 'My Message or Password'