DigestUtilities.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using Org.BouncyCastle.Asn1;
  5. using Org.BouncyCastle.Asn1.CryptoPro;
  6. using Org.BouncyCastle.Asn1.Nist;
  7. using Org.BouncyCastle.Asn1.Pkcs;
  8. using Org.BouncyCastle.Asn1.Oiw;
  9. using Org.BouncyCastle.Asn1.TeleTrust;
  10. using Org.BouncyCastle.Security;
  11. using Org.BouncyCastle.Crypto.Digests;
  12. using Org.BouncyCastle.Crypto;
  13. using Org.BouncyCastle.Utilities;
  14. namespace Org.BouncyCastle.Security
  15. {
  16. /// <remarks>
  17. /// Utility class for creating IDigest objects from their names/Oids
  18. /// </remarks>
  19. public sealed class DigestUtilities
  20. {
  21. private enum DigestAlgorithm {
  22. GOST3411,
  23. KECCAK_224, KECCAK_256, KECCAK_288, KECCAK_384, KECCAK_512,
  24. MD2, MD4, MD5,
  25. RIPEMD128, RIPEMD160, RIPEMD256, RIPEMD320,
  26. SHA_1, SHA_224, SHA_256, SHA_384, SHA_512,
  27. SHA_512_224, SHA_512_256,
  28. SHA3_224, SHA3_256, SHA3_384, SHA3_512,
  29. SHAKE128, SHAKE256,
  30. TIGER,
  31. WHIRLPOOL,
  32. };
  33. private DigestUtilities()
  34. {
  35. }
  36. private static readonly IDictionary algorithms = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  37. private static readonly IDictionary oids = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  38. static DigestUtilities()
  39. {
  40. // Signal to obfuscation tools not to change enum constants
  41. ((DigestAlgorithm)Enums.GetArbitraryValue(typeof(DigestAlgorithm))).ToString();
  42. algorithms[PkcsObjectIdentifiers.MD2.Id] = "MD2";
  43. algorithms[PkcsObjectIdentifiers.MD4.Id] = "MD4";
  44. algorithms[PkcsObjectIdentifiers.MD5.Id] = "MD5";
  45. algorithms["SHA1"] = "SHA-1";
  46. algorithms[OiwObjectIdentifiers.IdSha1.Id] = "SHA-1";
  47. algorithms["SHA224"] = "SHA-224";
  48. algorithms[NistObjectIdentifiers.IdSha224.Id] = "SHA-224";
  49. algorithms["SHA256"] = "SHA-256";
  50. algorithms[NistObjectIdentifiers.IdSha256.Id] = "SHA-256";
  51. algorithms["SHA384"] = "SHA-384";
  52. algorithms[NistObjectIdentifiers.IdSha384.Id] = "SHA-384";
  53. algorithms["SHA512"] = "SHA-512";
  54. algorithms[NistObjectIdentifiers.IdSha512.Id] = "SHA-512";
  55. algorithms["SHA512/224"] = "SHA-512/224";
  56. algorithms[NistObjectIdentifiers.IdSha512_224.Id] = "SHA-512/224";
  57. algorithms["SHA512/256"] = "SHA-512/256";
  58. algorithms[NistObjectIdentifiers.IdSha512_256.Id] = "SHA-512/256";
  59. algorithms["RIPEMD-128"] = "RIPEMD128";
  60. algorithms[TeleTrusTObjectIdentifiers.RipeMD128.Id] = "RIPEMD128";
  61. algorithms["RIPEMD-160"] = "RIPEMD160";
  62. algorithms[TeleTrusTObjectIdentifiers.RipeMD160.Id] = "RIPEMD160";
  63. algorithms["RIPEMD-256"] = "RIPEMD256";
  64. algorithms[TeleTrusTObjectIdentifiers.RipeMD256.Id] = "RIPEMD256";
  65. algorithms["RIPEMD-320"] = "RIPEMD320";
  66. // algorithms[TeleTrusTObjectIdentifiers.RipeMD320.Id] = "RIPEMD320";
  67. algorithms[CryptoProObjectIdentifiers.GostR3411.Id] = "GOST3411";
  68. algorithms[NistObjectIdentifiers.IdSha3_224.Id] = "SHA3-224";
  69. algorithms[NistObjectIdentifiers.IdSha3_256.Id] = "SHA3-256";
  70. algorithms[NistObjectIdentifiers.IdSha3_384.Id] = "SHA3-384";
  71. algorithms[NistObjectIdentifiers.IdSha3_512.Id] = "SHA3-512";
  72. algorithms[NistObjectIdentifiers.IdShake128.Id] = "SHAKE128";
  73. algorithms[NistObjectIdentifiers.IdShake256.Id] = "SHAKE256";
  74. oids["MD2"] = PkcsObjectIdentifiers.MD2;
  75. oids["MD4"] = PkcsObjectIdentifiers.MD4;
  76. oids["MD5"] = PkcsObjectIdentifiers.MD5;
  77. oids["SHA-1"] = OiwObjectIdentifiers.IdSha1;
  78. oids["SHA-224"] = NistObjectIdentifiers.IdSha224;
  79. oids["SHA-256"] = NistObjectIdentifiers.IdSha256;
  80. oids["SHA-384"] = NistObjectIdentifiers.IdSha384;
  81. oids["SHA-512"] = NistObjectIdentifiers.IdSha512;
  82. oids["SHA-512/224"] = NistObjectIdentifiers.IdSha512_224;
  83. oids["SHA-512/256"] = NistObjectIdentifiers.IdSha512_256;
  84. oids["SHA3-224"] = NistObjectIdentifiers.IdSha3_224;
  85. oids["SHA3-256"] = NistObjectIdentifiers.IdSha3_256;
  86. oids["SHA3-384"] = NistObjectIdentifiers.IdSha3_384;
  87. oids["SHA3-512"] = NistObjectIdentifiers.IdSha3_512;
  88. oids["SHAKE128"] = NistObjectIdentifiers.IdShake128;
  89. oids["SHAKE256"] = NistObjectIdentifiers.IdShake256;
  90. oids["RIPEMD128"] = TeleTrusTObjectIdentifiers.RipeMD128;
  91. oids["RIPEMD160"] = TeleTrusTObjectIdentifiers.RipeMD160;
  92. oids["RIPEMD256"] = TeleTrusTObjectIdentifiers.RipeMD256;
  93. oids["GOST3411"] = CryptoProObjectIdentifiers.GostR3411;
  94. }
  95. /// <summary>
  96. /// Returns a ObjectIdentifier for a given digest mechanism.
  97. /// </summary>
  98. /// <param name="mechanism">A string representation of the digest meanism.</param>
  99. /// <returns>A DerObjectIdentifier, null if the Oid is not available.</returns>
  100. public static DerObjectIdentifier GetObjectIdentifier(
  101. string mechanism)
  102. {
  103. if (mechanism == null)
  104. throw new System.ArgumentNullException("mechanism");
  105. mechanism = Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(mechanism);
  106. string aliased = (string) algorithms[mechanism];
  107. if (aliased != null)
  108. mechanism = aliased;
  109. return (DerObjectIdentifier) oids[mechanism];
  110. }
  111. public static ICollection Algorithms
  112. {
  113. get { return oids.Keys; }
  114. }
  115. public static IDigest GetDigest(
  116. DerObjectIdentifier id)
  117. {
  118. return GetDigest(id.Id);
  119. }
  120. public static IDigest GetDigest(
  121. string algorithm)
  122. {
  123. string upper = Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithm);
  124. string mechanism = (string) algorithms[upper];
  125. if (mechanism == null)
  126. {
  127. mechanism = upper;
  128. }
  129. try
  130. {
  131. DigestAlgorithm digestAlgorithm = (DigestAlgorithm)Enums.GetEnumValue(
  132. typeof(DigestAlgorithm), mechanism);
  133. switch (digestAlgorithm)
  134. {
  135. case DigestAlgorithm.GOST3411: return new Gost3411Digest();
  136. case DigestAlgorithm.KECCAK_224: return new KeccakDigest(224);
  137. case DigestAlgorithm.KECCAK_256: return new KeccakDigest(256);
  138. case DigestAlgorithm.KECCAK_288: return new KeccakDigest(288);
  139. case DigestAlgorithm.KECCAK_384: return new KeccakDigest(384);
  140. case DigestAlgorithm.KECCAK_512: return new KeccakDigest(512);
  141. case DigestAlgorithm.MD2: return new MD2Digest();
  142. case DigestAlgorithm.MD4: return new MD4Digest();
  143. case DigestAlgorithm.MD5: return new MD5Digest();
  144. case DigestAlgorithm.RIPEMD128: return new RipeMD128Digest();
  145. case DigestAlgorithm.RIPEMD160: return new RipeMD160Digest();
  146. case DigestAlgorithm.RIPEMD256: return new RipeMD256Digest();
  147. case DigestAlgorithm.RIPEMD320: return new RipeMD320Digest();
  148. case DigestAlgorithm.SHA_1: return new Sha1Digest();
  149. case DigestAlgorithm.SHA_224: return new Sha224Digest();
  150. case DigestAlgorithm.SHA_256: return new Sha256Digest();
  151. case DigestAlgorithm.SHA_384: return new Sha384Digest();
  152. case DigestAlgorithm.SHA_512: return new Sha512Digest();
  153. case DigestAlgorithm.SHA_512_224: return new Sha512tDigest(224);
  154. case DigestAlgorithm.SHA_512_256: return new Sha512tDigest(256);
  155. case DigestAlgorithm.SHA3_224: return new Sha3Digest(224);
  156. case DigestAlgorithm.SHA3_256: return new Sha3Digest(256);
  157. case DigestAlgorithm.SHA3_384: return new Sha3Digest(384);
  158. case DigestAlgorithm.SHA3_512: return new Sha3Digest(512);
  159. case DigestAlgorithm.SHAKE128: return new ShakeDigest(128);
  160. case DigestAlgorithm.SHAKE256: return new ShakeDigest(256);
  161. case DigestAlgorithm.TIGER: return new TigerDigest();
  162. case DigestAlgorithm.WHIRLPOOL: return new WhirlpoolDigest();
  163. }
  164. }
  165. catch (ArgumentException)
  166. {
  167. }
  168. throw new SecurityUtilityException("Digest " + mechanism + " not recognised.");
  169. }
  170. public static string GetAlgorithmName(
  171. DerObjectIdentifier oid)
  172. {
  173. return (string) algorithms[oid.Id];
  174. }
  175. public static byte[] CalculateDigest(string algorithm, byte[] input)
  176. {
  177. IDigest digest = GetDigest(algorithm);
  178. digest.BlockUpdate(input, 0, input.Length);
  179. return DoFinal(digest);
  180. }
  181. public static byte[] DoFinal(
  182. IDigest digest)
  183. {
  184. byte[] b = new byte[digest.GetDigestSize()];
  185. digest.DoFinal(b, 0);
  186. return b;
  187. }
  188. public static byte[] DoFinal(
  189. IDigest digest,
  190. byte[] input)
  191. {
  192. digest.BlockUpdate(input, 0, input.Length);
  193. return DoFinal(digest);
  194. }
  195. }
  196. }
  197. #endif