In case of little endian UTF-16 encoding, change Encoding.GetEncoding("UTF-16")
to Encoding.Unicode
in case of big endian UTF-16, change it to 'Encoding.BigEndianUnicode`.
Upon further inspection (e.g. UTF-8 is not the same as Encoding.ASCII), would this be a good translation of your PHP code?
var bytesToHash = Encoding.Convert(Encoding.UTF8, Encoding.BigEndianUnicode, Encoding.UTF8.GetBytes(args[0]));
var result = string.Concat(SHA512.Create().ComputeHash(bytesToHash).Select(b => b.ToString("X2")));
I am not familiar with the expected output of this php mhash function, but perhaps you could try this:
var hash = new System.Security.Cryptography.SHA512CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(data));
var hashString = BitConverter.ToString(hash);
var phpLikeHash = hashString.Replace("-", String.Empty).ToUpper();
UPDATE
Ok, so based upon your new information the first example I included would work if no encoding conversion of the input is required. So this at least confirms that we can rely on the SHA512 producing the same output as PHP's mhash in the way you use it, and that any differences are related to the bytes provided as input.
I would suggest you experiment a bit with different encodings using the same literal string as input in both PHP code and c#. Like e.g. below:
static bool HashIt(Encoding source, Encoding dest, string input, string expectedOutput)
{
byte[] bytes = source.GetBytes(input);
if (source != dest && dest != null)
bytes = Encoding.Convert(source, dest, bytes);
var hash = SHA512.Create().ComputeHash(bytes);
var hashString = string.Concat(hash.Select(b => b.ToString("X2")));
if (hashString.Equals(expectedOutput))
{
Console.WriteLine("Match found");
Console.WriteLine("Source encoding: {0}", source.WebName);
if (source != dest && dest != null)
Console.WriteLine("Converted to: {0}", dest.WebName);
return true;
}
return false;
}
static void Main(string[] args)
{
var inputs = new [] { "13338170AS875HEO49F8Sam-PC", @"13338170AS875HEO49F8Sam-PC" };
var expectedOutput = "A91A64DD4DF1880651CB6B919BE02C4363ED6D4B07EA246CF47FFB509918E4AA4C294FF8BA9F73E5CD1CE463BB3E66F84A6C294D70C781CD0610345BCADEEDA7";
var encodings = Encoding.GetEncodings().Select(e => e.GetEncoding());
var matchFound = false;
foreach (var srcEncoding in encodings)
{
foreach (var input in inputs)
{
if (HashIt(srcEncoding, null, input, expectedOutput))
matchFound = true;
foreach (var destEncoding in encodings)
{
if (HashIt(srcEncoding, destEncoding, input, expectedOutput))
matchFound = true;
}
}
}
if (!matchFound)
Console.WriteLine("No matches found");
Console.ReadLine();
}
Which produces the following output on my system:
No matches found
So you may be out of luck. I don't see at this time what else to try.