简体   繁体   中英

Convert code to C#?

I convert this code:

strtoupper(bin2hex(mhash(mhash_sha512, "$ico{$product[0]}{$user[1]}")));

into C#:

byte[] data = SHA512.Create().ComputeHash(Encoding.ASCII.GetBytes(args[0]));
string result = "";
for(int index = 0; index < data.Length; index++)
    result += data[index].ToString("X2");

and it works very good. But if I try convert input into UTF16:

strtoupper(bin2hex(mhash(mhash_sha512, iconv('UTF-8', 'UTF-16', "$ico{$product[0]}{$user[1]}"))));

and in C# convert using Encoding class:

SHA512.Create().ComputeHash(Encoding.Convert(Encoding.ASCII/UTF8, Encoding.GetEncoding("UTF-16"), Encoding.ASCII/UTF8.GetBytes(args[0])));

it not working. I need to get the same output from both languages. How can I correctly convert string ("bbb") into UTF16 in C# language? Thank you...

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 (eg 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 eg 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 = "A91A64DD4DF1880651CB6B919BE02C4363ED6D4B07EA246CF47FFB509918E4AA4C294FF8BA9F73E5‌​CD1CE463BB3E66F84A6C294D70C781CD0610345BCADEEDA7";
        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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM