PBES2Parameters.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs
  5. {
  6. public class PbeS2Parameters
  7. : Asn1Encodable
  8. {
  9. private readonly KeyDerivationFunc func;
  10. private readonly EncryptionScheme scheme;
  11. public static PbeS2Parameters GetInstance(object obj)
  12. {
  13. if (obj == null)
  14. return null;
  15. PbeS2Parameters existing = obj as PbeS2Parameters;
  16. if (existing != null)
  17. return existing;
  18. return new PbeS2Parameters(Asn1Sequence.GetInstance(obj));
  19. }
  20. public PbeS2Parameters(KeyDerivationFunc keyDevFunc, EncryptionScheme encScheme)
  21. {
  22. this.func = keyDevFunc;
  23. this.scheme = encScheme;
  24. }
  25. private PbeS2Parameters(Asn1Sequence seq)
  26. {
  27. if (seq.Count != 2)
  28. throw new ArgumentException("Wrong number of elements in sequence", "seq");
  29. Asn1Sequence funcSeq = (Asn1Sequence)seq[0].ToAsn1Object();
  30. // TODO Not sure if this special case is really necessary/appropriate
  31. if (funcSeq[0].Equals(PkcsObjectIdentifiers.IdPbkdf2))
  32. {
  33. func = new KeyDerivationFunc(PkcsObjectIdentifiers.IdPbkdf2,
  34. Pbkdf2Params.GetInstance(funcSeq[1]));
  35. }
  36. else
  37. {
  38. func = new KeyDerivationFunc(funcSeq);
  39. }
  40. scheme = EncryptionScheme.GetInstance(seq[1].ToAsn1Object());
  41. }
  42. public KeyDerivationFunc KeyDerivationFunc
  43. {
  44. get { return func; }
  45. }
  46. public EncryptionScheme EncryptionScheme
  47. {
  48. get { return scheme; }
  49. }
  50. public override Asn1Object ToAsn1Object()
  51. {
  52. return new DerSequence(func, scheme);
  53. }
  54. }
  55. }
  56. #pragma warning restore
  57. #endif