wp7上MD5加密類

很好的工具類 ,本身wp7 sdk沒有自帶加密類
  1. using System; 
  2. using System.Net; 
  3. using System.Text; 
  4. using System.Windows; 
  5. using System.Windows.Controls; 
  6. using System.Windows.Documents; 
  7. using System.Windows.Ink; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12.  
  13. namespace Test.Utils 
  14.     struct ABCDStruct 
  15.     { 
  16.         public uint A; 
  17.         public uint B; 
  18.         public uint C; 
  19.         public uint D; 
  20.     } 
  21.     /// <summary> 
  22.     /// md5加密類 
  23.     /// </summary> 
  24.     public sealed class MD5Core 
  25.     { 
  26.         //Prevent CSC from adding a default public constructor   
  27.         public MD5Core() { } 
  28.         public static byte[] GetHash(string input, Encoding encoding) 
  29.         { 
  30.             if (null == input) 
  31.                 throw new System.ArgumentNullException("input", "Unable to calculate hash over null input data"); 
  32.             if (null == encoding) 
  33.                 throw new System.ArgumentNullException("encoding", "Unable to calculate hash over a string without a default encoding. Consider using the GetHash(string) overload to use UTF8 Encoding"); 
  34.             byte[] target = encoding.GetBytes(input); 
  35.             return GetHash(target); 
  36.         } 
  37.         public static byte[] GetHash(string input) 
  38.         { 
  39.             return GetHash(input, new UTF8Encoding()); 
  40.         } 
  41.         public static string GetHashString(byte[] input) 
  42.         { 
  43.             if (null == input) 
  44.                 throw new System.ArgumentNullException("input", "Unable to calculate hash over null input data"); 
  45.             string retval = BitConverter.ToString(GetHash(input)); 
  46.             retvalretval = retval.Replace("-", ""); 
  47.             return retval; 
  48.         } 
  49.         public static string GetHashString(string input, Encoding encoding) 
  50.         { 
  51.             if (null == input) 
  52.                 throw new System.ArgumentNullException("input", "Unable to calculate hash over null input data"); 
  53.             if (null == encoding) 
  54.                 throw new System.ArgumentNullException("encoding", "Unable to calculate hash over a string without a default encoding. Consider using the GetHashString(string) overload to use UTF8 Encoding"); 
  55.  
  56.             byte[] target = encoding.GetBytes(input); 
  57.             return GetHashString(target); 
  58.         } 
  59.         public static string GetHashString(string input) 
  60.         { 
  61.             return GetHashString(input, new UTF8Encoding()); 
  62.         } 
  63.         public static byte[] GetHash(byte[] input) 
  64.         { 
  65.             if (null == input) 
  66.                 throw new System.ArgumentNullException("input", "Unable to calculate hash over null input data"); 
  67.             //Intitial values defined in RFC 1321   
  68.             ABCDStruct abcd = new ABCDStruct(); 
  69.             abcd.A = 0x67452301
  70.             abcd.B = 0xefcdab89
  71.             abcd.C = 0x98badcfe
  72.             abcd.D = 0x10325476
  73.             //We pass in the input array by block, the final block of data must be handled specialy for padding & length embeding   
  74.             int startIndex = 0
  75.             while (startIndex <= input.Length - 64) 
  76.             { 
  77.                 MD5Core.GetHashBlock(input, ref abcd, startIndex); 
  78.                 startIndex += 64; 
  79.             } 
  80.             // The final data block.    
  81.             return MD5Core.GetHashFinalBlock(input, startIndex, input.Length - startIndex, abcd, (Int64)input.Length * 8); 
  82.         } 
  83.         internal static byte[] GetHashFinalBlock(byte[] input, int ibStart, int cbSize, ABCDStruct ABCD, Int64 len) 
  84.         { 
  85.             byte[] working = new byte[64]; 
  86.             byte[] length = BitConverter.GetBytes(len); 
  87.             //Padding is a single bit 1, followed by the number of 0s required to make size congruent to 448 modulo 512. Step 1 of RFC 1321     
  88.             //The CLR ensures that our buffer is 0-assigned, we don't need to explicitly set it. This is why it ends up being quicker to just   
  89.             //use a temporary array rather then doing in-place assignment (5% for small inputs)   
  90.             Array.Copy(input, ibStart, working, 0, cbSize); 
  91.             working[cbSize] = 0x80; 
  92.             //We have enough room to store the length in this chunk   
  93.             if (cbSize <= 56) 
  94.             { 
  95.                 Array.Copy(length, 0, working, 56, 8); 
  96.                 GetHashBlock(working, ref ABCD, 0); 
  97.             } 
  98.             else  //We need an aditional chunk to store the length   
  99.             { 
  100.                 GetHashBlock(working, ref ABCD, 0); 
  101.                 //Create an entirely new chunk due to the 0-assigned trick mentioned above, to avoid an extra function call clearing the array   
  102.                 working = new byte[64]; 
  103.                 Array.Copy(length, 0, working, 56, 8); 
  104.                 GetHashBlock(working, ref ABCD, 0); 
  105.             } 
  106.             byte[] output = new byte[16]; 
  107.             Array.Copy(BitConverter.GetBytes(ABCD.A), 0, output, 0, 4); 
  108.             Array.Copy(BitConverter.GetBytes(ABCD.B), 0, output, 4, 4); 
  109.             Array.Copy(BitConverter.GetBytes(ABCD.C), 0, output, 8, 4); 
  110.             Array.Copy(BitConverter.GetBytes(ABCD.D), 0, output, 12, 4); 
  111.             return output; 
  112.         } 
  113.         // Performs a single block transform of MD5 for a given set of ABCD inputs   
  114.         /* If implementing your own hashing framework, be sure to set the initial ABCD correctly according to RFC 1321:  
  115.         //    A = 0x67452301;  
  116.         //    B = 0xefcdab89;  
  117.         //    C = 0x98badcfe;  
  118.         //    D = 0x10325476;  
  119.         */ 
  120.         internal static void GetHashBlock(byte[] input, ref ABCDStruct ABCDValue, int ibStart) 
  121.         { 
  122.             uint[] temp = Converter(input, ibStart); 
  123.             uint a = ABCDValue.A; 
  124.             uint b = ABCDValue.B; 
  125.             uint c = ABCDValue.C; 
  126.             uint d = ABCDValue.D; 
  127.             a = r1(a, b, c, d, temp[0], 7, 0xd76aa478); 
  128.             d = r1(d, a, b, c, temp[1], 12, 0xe8c7b756); 
  129.             c = r1(c, d, a, b, temp[2], 17, 0x242070db); 
  130.             b = r1(b, c, d, a, temp[3], 22, 0xc1bdceee); 
  131.             a = r1(a, b, c, d, temp[4], 7, 0xf57c0faf); 
  132.             d = r1(d, a, b, c, temp[5], 12, 0x4787c62a); 
  133.             c = r1(c, d, a, b, temp[6], 17, 0xa8304613); 
  134.             b = r1(b, c, d, a, temp[7], 22, 0xfd469501); 
  135.             a = r1(a, b, c, d, temp[8], 7, 0x698098d8); 
  136.             d = r1(d, a, b, c, temp[9], 12, 0x8b44f7af); 
  137.             c = r1(c, d, a, b, temp[10], 17, 0xffff5bb1); 
  138.             b = r1(b, c, d, a, temp[11], 22, 0x895cd7be); 
  139.             a = r1(a, b, c, d, temp[12], 7, 0x6b901122); 
  140.             d = r1(d, a, b, c, temp[13], 12, 0xfd987193); 
  141.             c = r1(c, d, a, b, temp[14], 17, 0xa679438e); 
  142.             b = r1(b, c, d, a, temp[15], 22, 0x49b40821); 
  143.             a = r2(a, b, c, d, temp[1], 5, 0xf61e2562); 
  144.             d = r2(d, a, b, c, temp[6], 9, 0xc040b340); 
  145.             c = r2(c, d, a, b, temp[11], 14, 0x265e5a51); 
  146.             b = r2(b, c, d, a, temp[0], 20, 0xe9b6c7aa); 
  147.             a = r2(a, b, c, d, temp[5], 5, 0xd62f105d); 
  148.             d = r2(d, a, b, c, temp[10], 9, 0x02441453); 
  149.             c = r2(c, d, a, b, temp[15], 14, 0xd8a1e681); 
  150.             b = r2(b, c, d, a, temp[4], 20, 0xe7d3fbc8); 
  151.             a = r2(a, b, c, d, temp[9], 5, 0x21e1cde6); 
  152.             d = r2(d, a, b, c, temp[14], 9, 0xc33707d6); 
  153.             c = r2(c, d, a, b, temp[3], 14, 0xf4d50d87); 
  154.             b = r2(b, c, d, a, temp[8], 20, 0x455a14ed); 
  155.             a = r2(a, b, c, d, temp[13], 5, 0xa9e3e905); 
  156.             d = r2(d, a, b, c, temp[2], 9, 0xfcefa3f8); 
  157.             c = r2(c, d, a, b, temp[7], 14, 0x676f02d9); 
  158.             b = r2(b, c, d, a, temp[12], 20, 0x8d2a4c8a); 
  159.             a = r3(a, b, c, d, temp[5], 4, 0xfffa3942); 
  160.             d = r3(d, a, b, c, temp[8], 11, 0x8771f681); 
  161.             c = r3(c, d, a, b, temp[11], 16, 0x6d9d6122); 
  162.             b = r3(b, c, d, a, temp[14], 23, 0xfde5380c); 
  163.             a = r3(a, b, c, d, temp[1], 4, 0xa4beea44); 
  164.             d = r3(d, a, b, c, temp[4], 11, 0x4bdecfa9); 
  165.             c = r3(c, d, a, b, temp[7], 16, 0xf6bb4b60); 
  166.             b = r3(b, c, d, a, temp[10], 23, 0xbebfbc70); 
  167.             a = r3(a, b, c, d, temp[13], 4, 0x289b7ec6); 
  168.             d = r3(d, a, b, c, temp[0], 11, 0xeaa127fa); 
  169.             c = r3(c, d, a, b, temp[3], 16, 0xd4ef3085); 
  170.             b = r3(b, c, d, a, temp[6], 23, 0x04881d05); 
  171.             a = r3(a, b, c, d, temp[9], 4, 0xd9d4d039); 
  172.             d = r3(d, a, b, c, temp[12], 11, 0xe6db99e5); 
  173.             c = r3(c, d, a, b, temp[15], 16, 0x1fa27cf8); 
  174.             b = r3(b, c, d, a, temp[2], 23, 0xc4ac5665); 
  175.             a = r4(a, b, c, d, temp[0], 6, 0xf4292244); 
  176.             d = r4(d, a, b, c, temp[7], 10, 0x432aff97); 
  177.             c = r4(c, d, a, b, temp[14], 15, 0xab9423a7); 
  178.             b = r4(b, c, d, a, temp[5], 21, 0xfc93a039); 
  179.             a = r4(a, b, c, d, temp[12], 6, 0x655b59c3); 
  180.             d = r4(d, a, b, c, temp[3], 10, 0x8f0ccc92); 
  181.             c = r4(c, d, a, b, temp[10], 15, 0xffeff47d); 
  182.             b = r4(b, c, d, a, temp[1], 21, 0x85845dd1); 
  183.             a = r4(a, b, c, d, temp[8], 6, 0x6fa87e4f); 
  184.             d = r4(d, a, b, c, temp[15], 10, 0xfe2ce6e0); 
  185.             c = r4(c, d, a, b, temp[6], 15, 0xa3014314); 
  186.             b = r4(b, c, d, a, temp[13], 21, 0x4e0811a1); 
  187.             a = r4(a, b, c, d, temp[4], 6, 0xf7537e82); 
  188.             d = r4(d, a, b, c, temp[11], 10, 0xbd3af235); 
  189.             c = r4(c, d, a, b, temp[2], 15, 0x2ad7d2bb); 
  190.             b = r4(b, c, d, a, temp[9], 21, 0xeb86d391); 
  191.             ABCDValue.A = unchecked(a + ABCDValue.A); 
  192.             ABCDValue.B = unchecked(b + ABCDValue.B); 
  193.             ABCDValue.C = unchecked(c + ABCDValue.C); 
  194.             ABCDValue.D = unchecked(d + ABCDValue.D); 
  195.             return; 
  196.         } 
  197.         //Manually unrolling these equations nets us a 20% performance improvement   
  198.         private static uint r1(uint a, uint b, uint c, uint d, uint x, int s, uint t) 
  199.         { 
  200.             //                  (b + LSR((a + F(b, c, d) + x + t), s))   
  201.             //F(x, y, z)        ((x & y) | ((x ^ 0xFFFFFFFF) & z))   
  202.             return unchecked(b + LSR((a + ((b & c) | ((b ^ 0xFFFFFFFF) & d)) + x + t), s)); 
  203.         } 
  204.         private static uint r2(uint a, uint b, uint c, uint d, uint x, int s, uint t) 
  205.         { 
  206.             //                  (b + LSR((a + G(b, c, d) + x + t), s))   
  207.             //G(x, y, z)        ((x & z) | (y & (z ^ 0xFFFFFFFF)))   
  208.             return unchecked(b + LSR((a + ((b & d) | (c & (d ^ 0xFFFFFFFF))) + x + t), s)); 
  209.         } 
  210.         private static uint r3(uint a, uint b, uint c, uint d, uint x, int s, uint t) 
  211.         { 
  212.             //                  (b + LSR((a + H(b, c, d) + k + i), s))   
  213.             //H(x, y, z)        (x ^ y ^ z)   
  214.             return unchecked(b + LSR((a + (b ^ c ^ d) + x + t), s)); 
  215.         } 
  216.         private static uint r4(uint a, uint b, uint c, uint d, uint x, int s, uint t) 
  217.         { 
  218.             //                  (b + LSR((a + I(b, c, d) + k + i), s))   
  219.             //I(x, y, z)        (y ^ (x | (z ^ 0xFFFFFFFF)))   
  220.             return unchecked(b + LSR((a + (c ^ (b | (d ^ 0xFFFFFFFF))) + x + t), s)); 
  221.         } 
  222.         // Implementation of left rotate   
  223.         // s is an int instead of a uint becuase the CLR requires the argument passed to >>/<< is of    
  224.         // type int. Doing the demoting inside this function would add overhead.   
  225.         private static uint LSR(uint i, int s) 
  226.         { 
  227.             return ((i << s) | (i >> (32 - s))); 
  228.         } 
  229.         //Convert input array into array of UInts   
  230.         private static uint[] Converter(byte[] input, int ibStart) 
  231.         { 
  232.             if (null == input) 
  233.                 throw new System.ArgumentNullException("input", "Unable convert null array to array of uInts"); 
  234.  
  235.             uint[] result = new uint[16]; 
  236.  
  237.             for (int i = 0; i < 16; i++) 
  238.             { 
  239.                 result[i] = (uint)input[ibStart + i * 4]; 
  240.                 result[i] += (uint)input[ibStart + i * 4 + 1] << 8
  241.                 result[i] += (uint)input[ibStart + i * 4 + 2] << 16
  242.                 result[i] += (uint)input[ibStart + i * 4 + 3] << 24
  243.             } 
  244.  
  245.             return result; 
  246.         } 
  247.     } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章