PBKDF2Params.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 Pbkdf2Params
  10. : Asn1Encodable
  11. {
  12. private static AlgorithmIdentifier algid_hmacWithSHA1 = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdHmacWithSha1, DerNull.Instance);
  13. private readonly Asn1OctetString octStr;
  14. private readonly DerInteger iterationCount, keyLength;
  15. private readonly AlgorithmIdentifier prf;
  16. public static Pbkdf2Params GetInstance(
  17. object obj)
  18. {
  19. if (obj == null || obj is Pbkdf2Params)
  20. return (Pbkdf2Params)obj;
  21. if (obj is Asn1Sequence)
  22. return new Pbkdf2Params((Asn1Sequence)obj);
  23. throw new ArgumentException("Unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  24. }
  25. public Pbkdf2Params(
  26. Asn1Sequence seq)
  27. {
  28. if (seq.Count < 2 || seq.Count > 4)
  29. throw new ArgumentException("Wrong number of elements in sequence", "seq");
  30. this.octStr = (Asn1OctetString)seq[0];
  31. this.iterationCount = (DerInteger)seq[1];
  32. Asn1Encodable kl = null, d = null;
  33. if (seq.Count > 3)
  34. {
  35. kl = seq[2];
  36. d = seq[3];
  37. }
  38. else if (seq.Count > 2)
  39. {
  40. if (seq[2] is DerInteger)
  41. {
  42. kl = seq[2];
  43. }
  44. else
  45. {
  46. d = seq[2];
  47. }
  48. }
  49. if (kl != null)
  50. {
  51. keyLength = (DerInteger)kl;
  52. }
  53. if (d != null)
  54. {
  55. prf = AlgorithmIdentifier.GetInstance(d);
  56. }
  57. }
  58. public Pbkdf2Params(
  59. byte[] salt,
  60. int iterationCount)
  61. {
  62. this.octStr = new DerOctetString(salt);
  63. this.iterationCount = new DerInteger(iterationCount);
  64. }
  65. public Pbkdf2Params(
  66. byte[] salt,
  67. int iterationCount,
  68. int keyLength)
  69. : this(salt, iterationCount)
  70. {
  71. this.keyLength = new DerInteger(keyLength);
  72. }
  73. public Pbkdf2Params(
  74. byte[] salt,
  75. int iterationCount,
  76. int keyLength,
  77. AlgorithmIdentifier prf)
  78. : this(salt, iterationCount, keyLength)
  79. {
  80. this.prf = prf;
  81. }
  82. public Pbkdf2Params(
  83. byte[] salt,
  84. int iterationCount,
  85. AlgorithmIdentifier prf)
  86. : this(salt, iterationCount)
  87. {
  88. this.prf = prf;
  89. }
  90. public byte[] GetSalt()
  91. {
  92. return octStr.GetOctets();
  93. }
  94. public BigInteger IterationCount
  95. {
  96. get { return iterationCount.Value; }
  97. }
  98. public BigInteger KeyLength
  99. {
  100. get { return keyLength == null ? null : keyLength.Value; }
  101. }
  102. public bool IsDefaultPrf
  103. {
  104. get { return prf == null || prf.Equals(algid_hmacWithSHA1); }
  105. }
  106. public AlgorithmIdentifier Prf
  107. {
  108. get { return prf != null ? prf : algid_hmacWithSHA1; }
  109. }
  110. public override Asn1Object ToAsn1Object()
  111. {
  112. Asn1EncodableVector v = new Asn1EncodableVector(octStr, iterationCount);
  113. v.AddOptional(keyLength);
  114. if (!IsDefaultPrf)
  115. {
  116. v.Add(prf);
  117. }
  118. return new DerSequence(v);
  119. }
  120. }
  121. }
  122. #pragma warning restore
  123. #endif