I need to generate a hash in PHP that matches the output from the HASHBYTES function in T-SQL.
Here is the code that was originally used to generate the hash in T-SQL:
declare @input varchar(15) = [this is the incoming value entered by the user]
declare @salt binary(32) = CRYPT_GEN_RANDOM(32)
declare @saltchars nvarchar(32) = cast(@salt as nvarchar(32) )
declare @inputchars nvarchar(50) = cast(@input as nvarchar(50) )
declare @sha binary(32) = HASHBYTES('SHA2_256', @passwordchars + @saltchars)
For the input "pete1234matt", we get an output of:
salt = 0x75D6215CA44339B645E86BE790FF562D3EE4BC51034FDC6A9697F767BB961B5B
sha = 0x979EEAC93B5F6624269634202D9F3B5B8008113B03A83D104784184E0F9361F8
Now here's the PHP code that I've written, I take the provided and input, then encode those as UTF-16LE as I expect the cast to nvarchar does. Is that correct?
$password = 'pete1234matt';
$salt = 0x75D6215CA44339B645E86BE790FF562D3EE4BC51034FDC6A9697F767BB961B5B;
$saltchars = mb_convert_encoding($salt, 'UTF-16LE');
$passwordchars = mb_convert_encoding($password, 'UTF-16LE');
$sha = hash('sha256', $passwordchars . $saltchars);
//$sha == 17b87bd0491f32f1e474ad9293e2fe5db56ed0a5c9939435f0984fdc04806dc5
//Expected: 979EEAC93B5F6624269634202D9F3B5B8008113B03A83D104784184E0F9361F8
I'm leaning towards there being an encoding issue with the salt but can't quite figure out where i'm going wrong. Can anyone with more knowledge on character encoding & T-SQl lend me a hand here!!