PBES2Parameters.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.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. public PbeS2Parameters(
  26. Asn1Sequence seq)
  27. {
  28. if (seq.Count != 2)
  29. throw new ArgumentException("Wrong number of elements in sequence", "seq");
  30. Asn1Sequence funcSeq = (Asn1Sequence)seq[0].ToAsn1Object();
  31. // TODO Not sure if this special case is really necessary/appropriate
  32. if (funcSeq[0].Equals(PkcsObjectIdentifiers.IdPbkdf2))
  33. {
  34. func = new KeyDerivationFunc(PkcsObjectIdentifiers.IdPbkdf2,
  35. Pbkdf2Params.GetInstance(funcSeq[1]));
  36. }
  37. else
  38. {
  39. func = new KeyDerivationFunc(funcSeq);
  40. }
  41. scheme = EncryptionScheme.GetInstance(seq[1].ToAsn1Object());
  42. }
  43. public KeyDerivationFunc KeyDerivationFunc
  44. {
  45. get { return func; }
  46. }
  47. public EncryptionScheme EncryptionScheme
  48. {
  49. get { return scheme; }
  50. }
  51. public override Asn1Object ToAsn1Object()
  52. {
  53. return new DerSequence(func, scheme);
  54. }
  55. }
  56. }
  57. #pragma warning restore
  58. #endif