MacData.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs
  8. {
  9. public class MacData
  10. : Asn1Encodable
  11. {
  12. internal DigestInfo digInfo;
  13. internal byte[] salt;
  14. internal BigInteger iterationCount;
  15. public static MacData GetInstance(
  16. object obj)
  17. {
  18. if (obj is MacData)
  19. {
  20. return (MacData) obj;
  21. }
  22. if (obj is Asn1Sequence)
  23. {
  24. return new MacData((Asn1Sequence) obj);
  25. }
  26. throw new ArgumentException("Unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  27. }
  28. private MacData(
  29. Asn1Sequence seq)
  30. {
  31. this.digInfo = DigestInfo.GetInstance(seq[0]);
  32. this.salt = ((Asn1OctetString) seq[1]).GetOctets();
  33. if (seq.Count == 3)
  34. {
  35. this.iterationCount = ((DerInteger) seq[2]).Value;
  36. }
  37. else
  38. {
  39. this.iterationCount = BigInteger.One;
  40. }
  41. }
  42. public MacData(
  43. DigestInfo digInfo,
  44. byte[] salt,
  45. int iterationCount)
  46. {
  47. this.digInfo = digInfo;
  48. this.salt = (byte[]) salt.Clone();
  49. this.iterationCount = BigInteger.ValueOf(iterationCount);
  50. }
  51. public DigestInfo Mac
  52. {
  53. get { return digInfo; }
  54. }
  55. public byte[] GetSalt()
  56. {
  57. return (byte[]) salt.Clone();
  58. }
  59. public BigInteger IterationCount
  60. {
  61. get { return iterationCount; }
  62. }
  63. /**
  64. * <pre>
  65. * MacData ::= SEQUENCE {
  66. * mac DigestInfo,
  67. * macSalt OCTET STRING,
  68. * iterations INTEGER DEFAULT 1
  69. * -- Note: The default is for historic reasons and its use is deprecated. A
  70. * -- higher value, like 1024 is recommended.
  71. * </pre>
  72. * @return the basic DERObject construction.
  73. */
  74. public override Asn1Object ToAsn1Object()
  75. {
  76. Asn1EncodableVector v = new Asn1EncodableVector(digInfo, new DerOctetString(salt));
  77. if (!iterationCount.Equals(BigInteger.One))
  78. {
  79. v.Add(new DerInteger(iterationCount));
  80. }
  81. return new DerSequence(v);
  82. }
  83. }
  84. }
  85. #pragma warning restore
  86. #endif