2
0

HashAlgorithmFactory.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System.Security.Cryptography;
  2. namespace Renci.SshNet.Security.Cryptography
  3. {
  4. internal static class HashAlgorithmFactory
  5. {
  6. public static RandomNumberGenerator CreateRandomNumberGenerator()
  7. {
  8. #if FEATURE_RNG_CRYPTO
  9. return new RNGCryptoServiceProvider();
  10. #else
  11. return RandomNumberGenerator.Create();
  12. #endif
  13. }
  14. #if FEATURE_HASH_MD5
  15. public static System.Security.Cryptography.MD5 CreateMD5()
  16. {
  17. return System.Security.Cryptography.MD5.Create();
  18. }
  19. #else
  20. public static Renci.Security.Cryptography.MD5 CreateMD5()
  21. {
  22. return new Renci.Security.Cryptography.MD5();
  23. }
  24. #endif // FEATURE_HASH_MD5
  25. #if FEATURE_HASH_SHA1
  26. public static System.Security.Cryptography.SHA1 CreateSHA1()
  27. {
  28. return new System.Security.Cryptography.SHA1Managed();
  29. }
  30. #else
  31. public static Renci.Security.Cryptography.SHA1 CreateSHA1()
  32. {
  33. return new Renci.Security.Cryptography.SHA1();
  34. }
  35. #endif
  36. #if FEATURE_HASH_SHA256
  37. public static System.Security.Cryptography.SHA256 CreateSHA256()
  38. {
  39. return new System.Security.Cryptography.SHA256Managed();
  40. }
  41. #else
  42. public static Renci.Security.Cryptography.SHA256 CreateSHA256()
  43. {
  44. return new Renci.Security.Cryptography.SHA256();
  45. }
  46. #endif
  47. #if FEATURE_HASH_SHA384
  48. public static System.Security.Cryptography.SHA384 CreateSHA384()
  49. {
  50. return new System.Security.Cryptography.SHA384Managed();
  51. }
  52. #else
  53. public static Renci.Security.Cryptography.SHA384 CreateSHA384()
  54. {
  55. return new Renci.Security.Cryptography.SHA384();
  56. }
  57. #endif
  58. #if FEATURE_HASH_SHA512
  59. public static System.Security.Cryptography.SHA512 CreateSHA512()
  60. {
  61. return new System.Security.Cryptography.SHA512Managed();
  62. }
  63. #else
  64. public static Renci.Security.Cryptography.SHA512 CreateSHA512()
  65. {
  66. return new Renci.Security.Cryptography.SHA512();
  67. }
  68. #endif
  69. #if FEATURE_HASH_RIPEMD160
  70. public static System.Security.Cryptography.RIPEMD160 CreateRIPEMD160()
  71. {
  72. return new System.Security.Cryptography.RIPEMD160Managed();
  73. }
  74. #else
  75. public static Renci.Security.Cryptography.RIPEMD160 CreateRIPEMD160()
  76. {
  77. return new Renci.Security.Cryptography.RIPEMD160();
  78. }
  79. #endif // FEATURE_HASH_RIPEMD160
  80. #if FEATURE_HMAC_MD5
  81. public static System.Security.Cryptography.HMACMD5 CreateHMACMD5(byte[] key)
  82. {
  83. return new System.Security.Cryptography.HMACMD5(key);
  84. }
  85. public static HMACMD5 CreateHMACMD5(byte[] key, int hashSize)
  86. {
  87. return new HMACMD5(key, hashSize);
  88. }
  89. #else
  90. public static Renci.Security.Cryptography.HMACMD5 CreateHMACMD5(byte[] key)
  91. {
  92. return new Renci.Security.Cryptography.HMACMD5(key);
  93. }
  94. public static Renci.Security.Cryptography.HMACMD5 CreateHMACMD5(byte[] key, int hashSize)
  95. {
  96. return new Renci.Security.Cryptography.HMACMD5(key, hashSize);
  97. }
  98. #endif // FEATURE_HMAC_MD5
  99. #if FEATURE_HMAC_SHA1
  100. public static System.Security.Cryptography.HMACSHA1 CreateHMACSHA1(byte[] key)
  101. {
  102. return new System.Security.Cryptography.HMACSHA1(key);
  103. }
  104. public static HMACSHA1 CreateHMACSHA1(byte[] key, int hashSize)
  105. {
  106. return new HMACSHA1(key, hashSize);
  107. }
  108. #else
  109. public static Renci.Security.Cryptography.HMACSHA1 CreateHMACSHA1(byte[] key)
  110. {
  111. return new Renci.Security.Cryptography.HMACSHA1(key);
  112. }
  113. public static Renci.Security.Cryptography.HMACSHA1 CreateHMACSHA1(byte[] key, int hashSize)
  114. {
  115. return new Renci.Security.Cryptography.HMACSHA1(key, hashSize);
  116. }
  117. #endif // FEATURE_HMAC_SHA1
  118. #if FEATURE_HMAC_SHA256
  119. public static System.Security.Cryptography.HMACSHA256 CreateHMACSHA256(byte[] key)
  120. {
  121. return new System.Security.Cryptography.HMACSHA256(key);
  122. }
  123. public static HMACSHA256 CreateHMACSHA256(byte[] key, int hashSize)
  124. {
  125. return new HMACSHA256(key, hashSize);
  126. }
  127. #else
  128. public static Renci.Security.Cryptography.HMACSHA256 CreateHMACSHA256(byte[] key)
  129. {
  130. return new Renci.Security.Cryptography.HMACSHA256(key);
  131. }
  132. public static Renci.Security.Cryptography.HMACSHA256 CreateHMACSHA256(byte[] key, int hashSize)
  133. {
  134. return new Renci.Security.Cryptography.HMACSHA256(key, hashSize);
  135. }
  136. #endif // FEATURE_HMAC_SHA256
  137. #if FEATURE_HMAC_SHA384
  138. public static System.Security.Cryptography.HMACSHA384 CreateHMACSHA384(byte[] key)
  139. {
  140. return new System.Security.Cryptography.HMACSHA384(key);
  141. }
  142. public static HMACSHA384 CreateHMACSHA384(byte[] key, int hashSize)
  143. {
  144. return new HMACSHA384(key, hashSize);
  145. }
  146. #else
  147. public static Renci.Security.Cryptography.HMACSHA384 CreateHMACSHA384(byte[] key)
  148. {
  149. return new Renci.Security.Cryptography.HMACSHA384(key);
  150. }
  151. public static Renci.Security.Cryptography.HMACSHA384 CreateHMACSHA384(byte[] key, int hashSize)
  152. {
  153. return new Renci.Security.Cryptography.HMACSHA384(key, hashSize);
  154. }
  155. #endif // FEATURE_HMAC_SHA384
  156. #if FEATURE_HMAC_SHA512
  157. public static System.Security.Cryptography.HMACSHA512 CreateHMACSHA512(byte[] key)
  158. {
  159. return new System.Security.Cryptography.HMACSHA512(key);
  160. }
  161. public static HMACSHA512 CreateHMACSHA512(byte[] key, int hashSize)
  162. {
  163. return new HMACSHA512(key, hashSize);
  164. }
  165. #else
  166. public static Renci.Security.Cryptography.HMACSHA512 CreateHMACSHA512(byte[] key)
  167. {
  168. return new Renci.Security.Cryptography.HMACSHA512(key);
  169. }
  170. public static Renci.Security.Cryptography.HMACSHA512 CreateHMACSHA512(byte[] key, int hashSize)
  171. {
  172. return new Renci.Security.Cryptography.HMACSHA512(key, hashSize);
  173. }
  174. #endif // FEATURE_HMAC_SHA512
  175. #if FEATURE_HMAC_RIPEMD160
  176. public static System.Security.Cryptography.HMACRIPEMD160 CreateHMACRIPEMD160(byte[] key)
  177. {
  178. return new System.Security.Cryptography.HMACRIPEMD160(key);
  179. }
  180. #else
  181. public static Renci.Security.Cryptography.HMACRIPEMD160 CreateHMACRIPEMD160(byte[] key)
  182. {
  183. return new Renci.Security.Cryptography.HMACRIPEMD160(key);
  184. }
  185. #endif // FEATURE_HMAC_RIPEMD160
  186. }
  187. }