The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Crypto.cs 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System.Text;
  2. using SecurityDriven.Inferno.Hash;
  3. using SecurityDriven.Inferno.Mac;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using Org.BouncyCastle.Utilities.Encoders;
  7. using Org.BouncyCastle.Bcpg.OpenPgp;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO.MemoryMappedFiles;
  11. namespace Teknik.Utilities
  12. {
  13. public class MD5
  14. {
  15. public static string Hash(string value)
  16. {
  17. byte[] valBytes = Encoding.ASCII.GetBytes(value);
  18. System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
  19. byte[] hashBytes = md5.ComputeHash(valBytes);
  20. StringBuilder sBuilder = new StringBuilder();
  21. // Loop through each byte of the hashed data
  22. // and format each one as a hexadecimal string.
  23. for (int i = 0; i < hashBytes.Length; i++)
  24. {
  25. sBuilder.Append(hashBytes[i].ToString("x2"));
  26. }
  27. // Return the hexadecimal string.
  28. return sBuilder.ToString();
  29. }
  30. public static string FileHash(string filename)
  31. {
  32. try
  33. {
  34. using (var md5 = System.Security.Cryptography.MD5.Create())
  35. {
  36. using (var stream = File.OpenRead(filename))
  37. {
  38. return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
  39. }
  40. }
  41. }
  42. catch (Exception)
  43. {
  44. return string.Empty;
  45. }
  46. }
  47. public static string DataHash(string data)
  48. {
  49. try
  50. {
  51. using (var md5 = System.Security.Cryptography.MD5.Create())
  52. {
  53. // convert string to stream
  54. byte[] byteArray = Encoding.UTF8.GetBytes(data);
  55. using (MemoryStream stream = new MemoryStream(byteArray))
  56. {
  57. return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
  58. }
  59. }
  60. }
  61. catch (Exception)
  62. {
  63. return string.Empty;
  64. }
  65. }
  66. }
  67. public class SHA384
  68. {
  69. public static byte[] Hash(string key, string value)
  70. {
  71. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  72. byte[] data = Encoding.UTF8.GetBytes(value);
  73. byte[] result = new HMAC2(HashFactories.SHA384, keyBytes).ComputeHash(data);
  74. return result;
  75. }
  76. }
  77. public class SHA256
  78. {
  79. public static string Hash(string value)
  80. {
  81. byte[] valueBytes = Encoding.Unicode.GetBytes(value);
  82. return Hash(valueBytes);
  83. }
  84. public static string Hash(byte[] value)
  85. {
  86. HashAlgorithm hash = new SHA256CryptoServiceProvider();
  87. byte[] hashBytes = hash.ComputeHash(value);
  88. return Convert.ToBase64String(hashBytes);
  89. }
  90. public static byte[] Hash(Stream value)
  91. {
  92. HashAlgorithm hash = new SHA256CryptoServiceProvider();
  93. return hash.ComputeHash(value);
  94. }
  95. public static string Hash(string value, string salt1, string salt2)
  96. {
  97. SHA256Managed hash = new SHA256Managed();
  98. SHA1 sha1 = new SHA1Managed();
  99. // gen salt2 hash
  100. byte[] dataSalt2 = Encoding.UTF8.GetBytes(salt2);
  101. byte[] salt2Bytes = hash.ComputeHash(dataSalt2);
  102. string salt2Str = string.Empty;
  103. foreach (byte x in salt2Bytes)
  104. {
  105. salt2Str += String.Format("{0:x2}", x);
  106. }
  107. string dataStr = salt1 + value + salt2Str;
  108. byte[] dataStrBytes = Encoding.UTF8.GetBytes(dataStr);
  109. byte[] shaBytes = sha1.ComputeHash(dataStrBytes);
  110. string sha1Str = string.Empty;
  111. foreach (byte x in shaBytes)
  112. {
  113. sha1Str += String.Format("{0:x2}", x);
  114. }
  115. byte[] sha1Bytes = Encoding.UTF8.GetBytes(sha1Str);
  116. byte[] valueBytes = hash.ComputeHash(sha1Bytes);
  117. string hashString = string.Empty;
  118. foreach (byte x in valueBytes)
  119. {
  120. hashString += String.Format("{0:x2}", x);
  121. }
  122. return hashString;
  123. }
  124. public static System.Security.Cryptography.SHA256 CreateHashAlgorithm()
  125. {
  126. if (CryptoConfig.AllowOnlyFipsAlgorithms)
  127. {
  128. return new SHA256CryptoServiceProvider();
  129. }
  130. return new SHA256Managed();
  131. }
  132. }
  133. public static class PGP
  134. {
  135. public static bool IsPublicKey(string key)
  136. {
  137. bool isValid = false;
  138. try
  139. {
  140. byte[] byteArray = Encoding.ASCII.GetBytes(key);
  141. using (MemoryStream stream = new MemoryStream(byteArray))
  142. {
  143. using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
  144. {
  145. PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
  146. PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
  147. if (foundKey != null)
  148. {
  149. isValid = true;
  150. }
  151. }
  152. }
  153. }
  154. catch (Exception ex)
  155. {
  156. isValid = false;
  157. }
  158. return isValid;
  159. }
  160. public static string GetFingerprint(string key)
  161. {
  162. string hexString = string.Empty;
  163. byte[] byteArray = Encoding.ASCII.GetBytes(key);
  164. using (MemoryStream stream = new MemoryStream(byteArray))
  165. {
  166. using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
  167. {
  168. PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
  169. PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
  170. if (foundKey != null)
  171. {
  172. byte[] fing = foundKey.GetFingerprint();
  173. hexString = Hex.ToHexString(fing);
  174. }
  175. }
  176. }
  177. return hexString;
  178. }
  179. public static string GetFingerprint64(string key)
  180. {
  181. string fingerprint = GetFingerprint(key);
  182. if (fingerprint.Length > 16)
  183. fingerprint = fingerprint.Substring(fingerprint.Length - 16);
  184. return fingerprint;
  185. }
  186. private static PgpPublicKey GetFirstPublicKey(PgpPublicKeyRingBundle publicKeyRingBundle)
  187. {
  188. foreach (PgpPublicKeyRing kRing in publicKeyRingBundle.GetKeyRings())
  189. {
  190. var keys = kRing.GetPublicKeys();
  191. foreach (var key in keys)
  192. {
  193. PgpPublicKey foundKey = (PgpPublicKey)key;
  194. //PgpPublicKey key = kRing.GetPublicKeys()
  195. //.Cast<PgpPublicKey>()
  196. // .Where(k => k.IsEncryptionKey)
  197. // .FirstOrDefault();
  198. if (foundKey != null && foundKey.IsEncryptionKey)
  199. return foundKey;
  200. }
  201. }
  202. return null;
  203. }
  204. }
  205. }