如何将此 PHP 函数转换为 C#


How to convert this PHP function to C#?

我想转换这个函数:

function DoHashPassword($username, $clear_password)
{
    return strtoupper(bin2hex(strrev(hex2bin(strtoupper(hash("sha256",strtoupper(hash("sha256", strtoupper($username)).":".strtoupper($clear_password))))))));
}

到 C# 语言。

我该怎么做?

目前我处于这一点:

剩余(以PHP为单位)

strtoupper(bin2hex(strrev(hex2bin($password))));

我做了什么(在 C# 中)

public static string DoShaHashPassword256(string _email, string _password)
        {
            byte[] emailbyte = Encoding.ASCII.GetBytes(_email.ToUpper());
            var sha_email = SHA256.Create();
            byte[] bytehashemail = sha_email.ComputeHash(emailbyte);
            _email = HexStringFromBytes(bytehashemail);
            //now with password
            byte[] passbyte = Encoding.ASCII.GetBytes(_email.ToUpper() + ":" + _password.ToUpper());
            var sha_pass = SHA256.Create();
            byte[] bytehashpass = sha_pass.ComputeHash(passbyte);
            _password = HexStringFromBytes(bytehashpass).ToUpper();
            return /* hashed password */
        }
        private static string HexStringFromBytes(byte[] bytes)
        {
            var sb = new StringBuilder();
            foreach (byte b in bytes)
            {
                var hex = b.ToString("x2");
                sb.Append(hex);
            }
            return sb.ToString();
        }

我现在不知道该怎么继续。

这是问题的答案。感谢您对我的问题投了不好的票(你应该帮忙,而不是投坏票,没有答案。

public static string DoShaHashPassword256(string _email, string _password)
        {
            byte[] emailbyte = Encoding.ASCII.GetBytes(_email.ToUpper());
            var sha_email = SHA256.Create();
            byte[] bytehashemail = sha_email.ComputeHash(emailbyte);
            _email = HexStringFromBytes(bytehashemail);
            //now with password
            byte[] passbyte = Encoding.ASCII.GetBytes(_email.ToUpper() + ":" + _password.ToUpper());
            var sha_pass = SHA256.Create();
            byte[] bytehashpass = sha_pass.ComputeHash(passbyte);
            _password = HexStringFromBytes(bytehashpass).ToUpper();
            //hex2bin
            var bindata = hex2bin(_password);
            //strrev
            char[] chararray = bindata.ToCharArray();
            Array.Reverse(chararray);
            var reversedstring = new string(chararray);
            //bin2hex
            byte[] bytes = Encoding.GetEncoding(1252).GetBytes(reversedstring);
            string hexString = HexStringFromBytes(bytes);
            return hexString.ToUpper();
        }
        private static string HexStringFromBytes(byte[] bytes)
        {
            var sb = new StringBuilder();
            foreach (byte b in bytes)
            {
                var hex = b.ToString("x2");
                sb.Append(hex);
            }
            return sb.ToString();
        }
        private static string hex2bin(string hexdata)
        {
            if (hexdata == null)
                throw new ArgumentNullException("hexdata");
            if (hexdata.Length % 2 != 0)
                throw new ArgumentException("hexdata should have even length");
            byte[] bytes = new byte[hexdata.Length / 2];
            for (int i = 0; i < hexdata.Length; i += 2)
                bytes[i / 2] = (byte)(HexValue(hexdata[i]) * 0x10
                + HexValue(hexdata[i + 1]));
            return Encoding.GetEncoding(1252).GetString(bytes);
        }
        private static int HexValue(char c)
        {
            int ch = (int)c;
            if (ch >= (int)'0' && ch <= (int)'9')
                return ch - (int)'0';
            if (ch >= (int)'a' && ch <= (int)'f')
                return ch - (int)'a' + 10;
            if (ch >= (int)'A' && ch <= (int)'F')
                return ch - (int)'A' + 10;
            throw new ArgumentException("Not a hexadecimal digit.");
        }