I have a C# function as below:
using System;
using System.Text;
using System.Globalization;
using System.Security.Cryptography;
public class Program
{
public static int SaltValueSize = 4;
public static string HashPassword(string clearData, string saltValue, HashAlgorithm hash)
{
var encoding = new UnicodeEncoding();
var binarySaltValue = new byte[SaltValueSize];
binarySaltValue[0] = byte.Parse(saltValue.Substring(0, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[1] = byte.Parse(saltValue.Substring(2, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[2] = byte.Parse(saltValue.Substring(4, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[3] = byte.Parse(saltValue.Substring(6, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
var valueToHash = new byte[SaltValueSize + encoding.GetByteCount(clearData)];
var binaryPassword = encoding.GetBytes(clearData);
binarySaltValue.CopyTo(valueToHash, 0);
binaryPassword.CopyTo(valueToHash, SaltValueSize);
// The result of valueToHash is
// 1861251472347701210800970115011501190111011401000640490500510520530330
var hashValue = hash.ComputeHash(valueToHash);
var hashedPassword = saltValue;
return BitConverter.ToString(hashValue);
}
public static void Main()
{
var data = HashPassword("MyPassword@12345!", "BA7D93EA", SHA256.Create());
Console.WriteLine("HashedPassword {0}", data);
}
}
I've seen similar questions in Stackoverflow like this:
C# SHA256 ComputeHash result different with CryptoJS SHA256 function
MD5.ComputeHash(Encoding.Unicode.GetBytes(value)) into javascript
But this answer can't help me because they're not using salt, and I can't find a way to apply this in JavaScript
I've almost rewritten this function in JavaScript but I'm stuck with the last step with function ComputeHash
const hashPassword = (password, salt) => {
const binarySaltValue = [];
binarySaltValue[0] = parseInt(salt.substring(0, 2), 16);
binarySaltValue[1] = parseInt(salt.substring(2, 4), 16);
binarySaltValue[2] = parseInt(salt.substring(4, 6), 16);
binarySaltValue[3] = parseInt(salt.substring(6, 8), 16);
const binaryPassword = [];
Buffer.from(password)
.toJSON()
.data.forEach((v) => {
binaryPassword.push(v);
binaryPassword.push(0);
});
const valueToHash = [...binarySaltValue, ...binaryPassword].join('');
// The result of valueToHash is the same as in C# example
// 1861251472347701210800970115011501190111011401000640490500510520530330
return valueToHash;
};
console.log(hashPassword('MyPassword@12345!', 'BA7D93EA'));
As you can see I've got the same array of bytes, but I'm missing the last piece with the ComputeHash function, could someone please help me with this