CMSPBEKey.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  9. //import javax.crypto.interfaces.PBEKey;
  10. namespace BestHTTP.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. string password,
  22. byte[] salt,
  23. int iterationCount)
  24. : this(password.ToCharArray(), salt, iterationCount)
  25. {
  26. }
  27. public CmsPbeKey(
  28. string password,
  29. AlgorithmIdentifier keyDerivationAlgorithm)
  30. : this(password.ToCharArray(), keyDerivationAlgorithm)
  31. {
  32. }
  33. public CmsPbeKey(
  34. char[] password,
  35. byte[] salt,
  36. int iterationCount)
  37. {
  38. this.password = (char[])password.Clone();
  39. this.salt = Arrays.Clone(salt);
  40. this.iterationCount = iterationCount;
  41. }
  42. public CmsPbeKey(
  43. char[] password,
  44. AlgorithmIdentifier keyDerivationAlgorithm)
  45. {
  46. if (!keyDerivationAlgorithm.Algorithm.Equals(PkcsObjectIdentifiers.IdPbkdf2))
  47. throw new ArgumentException("Unsupported key derivation algorithm: "
  48. + keyDerivationAlgorithm.Algorithm);
  49. Pbkdf2Params kdfParams = Pbkdf2Params.GetInstance(
  50. keyDerivationAlgorithm.Parameters.ToAsn1Object());
  51. this.password = (char[])password.Clone();
  52. this.salt = kdfParams.GetSalt();
  53. this.iterationCount = kdfParams.IterationCount.IntValue;
  54. }
  55. ~CmsPbeKey()
  56. {
  57. Array.Clear(this.password, 0, this.password.Length);
  58. }
  59. public string Password
  60. {
  61. get { return new string(password); }
  62. }
  63. public byte[] Salt
  64. {
  65. get { return Arrays.Clone(salt); }
  66. }
  67. public byte[] GetSalt()
  68. {
  69. return Salt;
  70. }
  71. public int IterationCount
  72. {
  73. get { return iterationCount; }
  74. }
  75. public string Algorithm
  76. {
  77. get { return "PKCS5S2"; }
  78. }
  79. public string Format
  80. {
  81. get { return "RAW"; }
  82. }
  83. public byte[] GetEncoded()
  84. {
  85. return null;
  86. }
  87. internal abstract KeyParameter GetEncoded(string algorithmOid);
  88. }
  89. }
  90. #pragma warning restore
  91. #endif