如何将这个PHP函数转换为C#?

时间:2016-02-11 10:04:52

标签: c# php security cryptography sha

我想转换这个功能:

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();
        }

我现在不知道怎么回事。

1 个答案:

答案 0 :(得分:0)

这是问题的答案。感谢你对我的问题投票不好(你应该帮忙,不要投不好票,也不要回答。)。

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.");
        }