DigestInfo.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using Org.BouncyCastle.Utilities;
  5. namespace Org.BouncyCastle.Asn1.X509
  6. {
  7. /**
  8. * The DigestInfo object.
  9. * <pre>
  10. * DigestInfo::=Sequence{
  11. * digestAlgorithm AlgorithmIdentifier,
  12. * digest OCTET STRING }
  13. * </pre>
  14. */
  15. public class DigestInfo
  16. : Asn1Encodable
  17. {
  18. private readonly byte[] digest;
  19. private readonly AlgorithmIdentifier algID;
  20. public static DigestInfo GetInstance(
  21. Asn1TaggedObject obj,
  22. bool explicitly)
  23. {
  24. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  25. }
  26. public static DigestInfo GetInstance(
  27. object obj)
  28. {
  29. if (obj is DigestInfo)
  30. {
  31. return (DigestInfo) obj;
  32. }
  33. if (obj is Asn1Sequence)
  34. {
  35. return new DigestInfo((Asn1Sequence) obj);
  36. }
  37. throw new ArgumentException("unknown object in factory: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  38. }
  39. public DigestInfo(
  40. AlgorithmIdentifier algID,
  41. byte[] digest)
  42. {
  43. this.digest = digest;
  44. this.algID = algID;
  45. }
  46. private DigestInfo(
  47. Asn1Sequence seq)
  48. {
  49. if (seq.Count != 2)
  50. throw new ArgumentException("Wrong number of elements in sequence", "seq");
  51. algID = AlgorithmIdentifier.GetInstance(seq[0]);
  52. digest = Asn1OctetString.GetInstance(seq[1]).GetOctets();
  53. }
  54. public AlgorithmIdentifier AlgorithmID
  55. {
  56. get { return algID; }
  57. }
  58. public byte[] GetDigest()
  59. {
  60. return digest;
  61. }
  62. public override Asn1Object ToAsn1Object()
  63. {
  64. return new DerSequence(algID, new DerOctetString(digest));
  65. }
  66. }
  67. }
  68. #endif