Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将此PHP函数转换为C#?_C#_Php_Security_Cryptography_Sha - Fatal编程技术网

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

如何将此PHP函数转换为C#?,c#,php,security,cryptography,sha,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语言 我该怎么做 目前我正处于这一点: 剩余(

我想转换此函数:

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

我该怎么做

目前我正处于这一点:

剩余(PHP)

我所做的(用C#)


我不知道现在该怎么办。

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

公共静态字符串DoShaHashPassword256(字符串\u电子邮件,字符串\u密码)
{
byte[]emailbyte=Encoding.ASCII.GetBytes(_email.ToUpper());
var sha_email=SHA256.Create();
byte[]bytehashemail=sha_email.ComputeHash(emailbyte);
_email=HexStringFromBytes(bytehashemail);
//现在使用密码
byte[]passbyte=Encoding.ASCII.GetBytes(_email.ToUpper()+”:“+_password.ToUpper());
var sha_pass=SHA256.Create();
byte[]bytehashpass=sha_pass.ComputeHash(passbyte);
_password=hextStringFromBytes(bytehashpass).ToUpper();
//合二宾
var bindata=hex2bin(_密码);
//斯特列夫
char[]chararray=bindata.ToCharArray();
数组。反向(chararray);
var reversedstring=新字符串(chararray);
//bin2hex
byte[]bytes=Encoding.GetEncoding(1252).GetBytes(reversedstring);
字符串hexString=HexStringFromBytes(字节);
返回hexString.ToUpper();
}
私有静态字符串HexStringFromBytes(字节[]字节)
{
var sb=新的StringBuilder();
foreach(字节中的字节b)
{
var hex=b.ToString(“x2”);
某人附加(十六进制);
}
使某人返回字符串();
}
私有静态字符串hex2bin(字符串hexdata)
{
如果(hexdata==null)
抛出新ArgumentNullException(“hexdata”);
如果(hexdata.Length%2!=0)
抛出新ArgumentException(“hexdata应该具有偶数长度”);
字节[]字节=新字节[hexdata.Length/2];
对于(int i=0;i如果(ch>=(int)'0'和&ch=(int)'a'和&ch=(int)“A”和&chc#对所有这些函数都有等价物,因此不需要任何帮助。请你用简单的英语描述函数应该接受什么和返回什么?这个问题有点可疑。我们可以在上面问这样的问题吗?编辑了主要帖子。我不知道其余函数的等价物。
strtoupper(bin2hex(strrev(hex2bin($password))));
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.");
        }