C# SHA-1 vs. PHP SHA-1…Different Results?

前端 未结 6 1593
醉话见心
醉话见心 2020-11-29 04:11

I am trying to calculate a SHA-1 Hash from a string, but when I calculate the string using php\'s sha1 function I get something different than when I try it in C#. I need C#

6条回答
  •  情歌与酒
    2020-11-29 04:53

    Try The following! I think it will work great:

    public static string SHA1Encodeb64(string toEncrypt)
        {
            //Produce an array of bytes which is the SHA1 hash
            byte[] sha1Signature = new byte[40];
    
            byte[] sha = System.Text.Encoding.Default.GetBytes(toEncrypt);
            SHA1 sha1 = SHA1Managed.Create();
            sha1Signature = sha1.ComputeHash(sha);
    
    
            /**
            * The BASE64 encoding standard's 6-bit alphabet, from RFC 1521,
            * plus the padding character at the end.
            */
            char[] Base64Chars = {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                'w', 'x', 'y', 'z', '0', '1', '2', '3',
                '4', '5', '6', '7', '8', '9', '+', '/',
                '='
                };
            //Algorithm to encode the SHA1 hash using Base64
            StringBuilder sb = new StringBuilder();
            int len = sha1Signature.Length;
            int i = 0;
            int ival;
            while (len >= 3)
            {
                ival = ((int)sha1Signature[i++] + 256) & 0xff;
                ival <<= 8;
                ival += ((int)sha1Signature[i++] + 256) & 0xff;
                ival <<= 8;
                ival += ((int)sha1Signature[i++] + 256) & 0xff;
                len -= 3;
                sb.Append(Base64Chars[(ival >> 18) & 63]);
                sb.Append(Base64Chars[(ival >> 12) & 63]);
                sb.Append(Base64Chars[(ival >> 6) & 63]);
                sb.Append(Base64Chars[ival & 63]);
            }
            switch (len)
            {
                case 0: // No pads needed.
                    break;
                case 1: // Two more output bytes and two pads.
                    ival = ((int)sha1Signature[i++] + 256) & 0xff;
                    ival <<= 16;
                    sb.Append(Base64Chars[(ival >> 18) & 63]);
                    sb.Append(Base64Chars[(ival >> 12) & 63]);
                    sb.Append(Base64Chars[64]);
                    sb.Append(Base64Chars[64]);
                    break;
                case 2: // Three more output bytes and one pad.
                    ival = ((int)sha1Signature[i++] + 256) & 0xff;
                    ival <<= 8;
                    ival += ((int)sha1Signature[i] + 256) & 0xff;
                    ival <<= 8;
                    sb.Append(Base64Chars[(ival >> 18) & 63]);
                    sb.Append(Base64Chars[(ival >> 12) & 63]);
                    sb.Append(Base64Chars[(ival >> 6) & 63]);
                    sb.Append(Base64Chars[64]);
                    break;
            }
            //Encode the signature using Base64
            string base64Sha1Signature = sb.ToString();
            return base64Sha1Signature;
        }
    

提交回复
热议问题