Asn1DigestFactory.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.IO;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  9. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators
  10. {
  11. public class Asn1DigestFactory
  12. : IDigestFactory
  13. {
  14. public static Asn1DigestFactory Get(DerObjectIdentifier oid)
  15. {
  16. return new Asn1DigestFactory(DigestUtilities.GetDigest(oid), oid);
  17. }
  18. public static Asn1DigestFactory Get(string mechanism)
  19. {
  20. DerObjectIdentifier oid = DigestUtilities.GetObjectIdentifier(mechanism);
  21. return new Asn1DigestFactory(DigestUtilities.GetDigest(oid), oid);
  22. }
  23. private readonly IDigest mDigest;
  24. private readonly DerObjectIdentifier mOid;
  25. public Asn1DigestFactory(IDigest digest, DerObjectIdentifier oid)
  26. {
  27. this.mDigest = digest;
  28. this.mOid = oid;
  29. }
  30. public virtual object AlgorithmDetails
  31. {
  32. get { return new AlgorithmIdentifier(mOid); }
  33. }
  34. public virtual int DigestLength
  35. {
  36. get { return mDigest.GetDigestSize(); }
  37. }
  38. public virtual IStreamCalculator<IBlockResult> CreateCalculator()
  39. {
  40. return new DfDigestStream(mDigest);
  41. }
  42. }
  43. internal class DfDigestStream
  44. : IStreamCalculator<SimpleBlockResult>
  45. {
  46. private readonly DigestSink mStream;
  47. public DfDigestStream(IDigest digest)
  48. {
  49. this.mStream = new DigestSink(digest);
  50. }
  51. public Stream Stream
  52. {
  53. get { return mStream; }
  54. }
  55. public SimpleBlockResult GetResult()
  56. {
  57. byte[] result = new byte[mStream.Digest.GetDigestSize()];
  58. mStream.Digest.DoFinal(result, 0);
  59. return new SimpleBlockResult(result);
  60. }
  61. }
  62. }
  63. #pragma warning restore
  64. #endif