CMSPBEKey.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  9. //import javax.crypto.interfaces.PBEKey;
  10. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Cms
  11. {
  12. public abstract class CmsPbeKey
  13. // TODO Create an equivalent interface somewhere?
  14. // : PBEKey
  15. : ICipherParameters
  16. {
  17. internal readonly char[] password;
  18. internal readonly byte[] salt;
  19. internal readonly int iterationCount;
  20. public CmsPbeKey(
  21. char[] password,
  22. byte[] salt,
  23. int iterationCount)
  24. {
  25. this.password = (char[])password.Clone();
  26. this.salt = Arrays.Clone(salt);
  27. this.iterationCount = iterationCount;
  28. }
  29. public CmsPbeKey(
  30. char[] password,
  31. AlgorithmIdentifier keyDerivationAlgorithm)
  32. {
  33. if (!keyDerivationAlgorithm.Algorithm.Equals(PkcsObjectIdentifiers.IdPbkdf2))
  34. throw new ArgumentException("Unsupported key derivation algorithm: "
  35. + keyDerivationAlgorithm.Algorithm);
  36. Pbkdf2Params kdfParams = Pbkdf2Params.GetInstance(
  37. keyDerivationAlgorithm.Parameters.ToAsn1Object());
  38. this.password = (char[])password.Clone();
  39. this.salt = kdfParams.GetSalt();
  40. this.iterationCount = kdfParams.IterationCount.IntValue;
  41. }
  42. ~CmsPbeKey()
  43. {
  44. Array.Clear(this.password, 0, this.password.Length);
  45. }
  46. public byte[] Salt
  47. {
  48. get { return Arrays.Clone(salt); }
  49. }
  50. public int IterationCount
  51. {
  52. get { return iterationCount; }
  53. }
  54. public string Algorithm
  55. {
  56. get { return "PKCS5S2"; }
  57. }
  58. public string Format
  59. {
  60. get { return "RAW"; }
  61. }
  62. public byte[] GetEncoded()
  63. {
  64. return null;
  65. }
  66. internal abstract KeyParameter GetEncoded(string algorithmOid);
  67. }
  68. }
  69. #pragma warning restore
  70. #endif