1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 * Copyright (c) 2003-2012 by AG-Software * 3 * All Rights Reserved. * 4 * Contact information for AG-Software is available at http://www.ag-software.de * 5 * * 6 * Licence: * 7 * The agsXMPP SDK is released under a dual licence * 8 * agsXMPP can be used under either of two licences * 9 * * 10 * A commercial licence which is probably the most appropriate for commercial * 11 * corporate use and closed source projects. * 12 * * 13 * The GNU Public License (GPL) is probably most appropriate for inclusion in * 14 * other open source projects. * 15 * * 16 * See README.html for details. * 17 * * 18 * For general enquiries visit our website at: * 19 * http://www.ag-software.de * 20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 21 22 using System.Text; 23 #if !CF 24 using System.Security.Cryptography; 25 #endif 26 27 namespace agsXMPP.Util 28 { 29 /// <summary> 30 /// Helper class for hashing. 31 /// </summary> 32 public class Hash 33 { 34 35 #region << SHA1 Hash Desktop Framework and Mono >> 36 #if !CF Sha1Hash(string pass)37 public static string Sha1Hash(string pass) 38 { 39 SHA1 sha = SHA1.Create(); 40 byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(pass)); 41 return HexToString(hash); 42 } 43 Sha1HashBytes(string pass)44 public static byte[] Sha1HashBytes(string pass) 45 { 46 SHA1 sha = SHA1.Create(); 47 return sha.ComputeHash(Encoding.UTF8.GetBytes(pass)); 48 } 49 Sha1HashBytes(byte[] pass)50 public static byte[] Sha1HashBytes(byte[] pass) 51 { 52 using (var sha = new SHA1Managed()) 53 { 54 return sha.ComputeHash(pass); 55 } 56 } 57 #endif 58 59 /// <summary> 60 /// Converts all bytes in the Array to a string representation. 61 /// </summary> 62 /// <param name="buf"></param> 63 /// <returns>string representation</returns> HexToString(byte[] buf)64 public static string HexToString(byte[] buf) 65 { 66 StringBuilder sb = new StringBuilder(); 67 foreach (byte b in buf) 68 { 69 sb.Append(b.ToString("x2")); 70 } 71 return sb.ToString(); 72 } 73 74 #endregion 75 76 77 #region << SHA1 Hash Compact Framework >> 78 #if CF 79 80 81 /// <summary> 82 /// return a SHA1 Hash on PPC and Smartphone 83 /// </summary> 84 /// <param name="pass"></param> 85 /// <returns></returns> Sha1Hash(byte[] pass)86 public static byte[] Sha1Hash(byte[] pass) 87 { 88 IntPtr hProv; 89 bool retVal = WinCeApi.CryptAcquireContext( out hProv, null, null, (int) WinCeApi.SecurityProviderType.RSA_FULL, 0 ); 90 IntPtr hHash; 91 retVal = WinCeApi.CryptCreateHash( hProv, (int) WinCeApi.SecurityProviderType.CALG_SHA1, IntPtr.Zero, 0, out hHash ); 92 93 byte [] publicKey = pass; 94 int publicKeyLen = publicKey.Length; 95 retVal = WinCeApi.CryptHashData( hHash, publicKey, publicKeyLen, 0 ); 96 int bufferLen = 20; //SHA1 size 97 byte [] buffer = new byte[bufferLen]; 98 retVal = WinCeApi.CryptGetHashParam( hHash, (int) WinCeApi.SecurityProviderType.HP_HASHVAL, buffer, ref bufferLen, 0 ); 99 retVal = WinCeApi.CryptDestroyHash( hHash ); 100 retVal = WinCeApi.CryptReleaseContext( hProv, 0 ); 101 102 return buffer; 103 } 104 105 /// <summary> 106 /// return a SHA1 Hash on PPC and Smartphone 107 /// </summary> 108 /// <param name="pass"></param> 109 /// <returns></returns> Sha1Hash(string pass)110 public static string Sha1Hash(string pass) 111 { 112 return HexToString(Sha1Hash(System.Text.Encoding.ASCII.GetBytes(pass))); 113 } 114 115 /// <summary> 116 /// return a SHA1 Hash on PPC and Smartphone 117 /// </summary> 118 /// <param name="pass"></param> 119 /// <returns></returns> Sha1HashBytes(string pass)120 public static byte[] Sha1HashBytes(string pass) 121 { 122 return Sha1Hash(System.Text.Encoding.UTF8.GetBytes(pass)); 123 } 124 125 /// <summary> 126 /// omputes the MD5 hash value for the specified byte array. 127 /// </summary> 128 /// <param name="pass">The input for which to compute the hash code.</param> 129 /// <returns>The computed hash code.</returns> MD5Hash(byte[] pass)130 public static byte[] MD5Hash(byte[] pass) 131 { 132 IntPtr hProv; 133 bool retVal = WinCeApi.CryptAcquireContext( out hProv, null, null, (int) WinCeApi.SecurityProviderType.RSA_FULL, 0 ); 134 IntPtr hHash; 135 retVal = WinCeApi.CryptCreateHash( hProv, (int) WinCeApi.SecurityProviderType.CALG_MD5, IntPtr.Zero, 0, out hHash ); 136 137 byte [] publicKey = pass; 138 int publicKeyLen = publicKey.Length; 139 retVal = WinCeApi.CryptHashData( hHash, publicKey, publicKeyLen, 0 ); 140 int bufferLen = 16; //SHA1 size 141 byte [] buffer = new byte[bufferLen]; 142 retVal = WinCeApi.CryptGetHashParam( hHash, (int) WinCeApi.SecurityProviderType.HP_HASHVAL, buffer, ref bufferLen, 0 ); 143 retVal = WinCeApi.CryptDestroyHash( hHash ); 144 retVal = WinCeApi.CryptReleaseContext( hProv, 0 ); 145 146 return buffer; 147 } 148 149 #endif 150 #endregion 151 152 #if !(CF || CF_2) HMAC(byte[] key, byte[] data)153 public static byte[] HMAC(byte[] key, byte[] data) 154 { 155 using (var hmacsha1 = new HMACSHA1(key, true)) 156 { 157 byte[] bytes = hmacsha1.ComputeHash(data); 158 return bytes; 159 } 160 } 161 HMAC(string key, byte[] data)162 public static byte[] HMAC(string key, byte[] data) 163 { 164 return HMAC(Encoding.UTF8.GetBytes(key), data); 165 } 166 HMAC(byte[] key, string data)167 public static byte[] HMAC(byte[] key, string data) 168 { 169 return HMAC(key, Encoding.UTF8.GetBytes(data)); 170 } 171 HMAC(string key, string data)172 public static byte[] HMAC(string key, string data) 173 { 174 return HMAC(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(data)); 175 } 176 177 #endif 178 179 } 180 }