CMSPBEKey.cs 2.6 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. [Obsolete("Use version taking 'char[]' instead")]
  21. public CmsPbeKey(
  22. string password,
  23. byte[] salt,
  24. int iterationCount)
  25. : this(password.ToCharArray(), salt, iterationCount)
  26. {
  27. }
  28. [Obsolete("Use version taking 'char[]' instead")]
  29. public CmsPbeKey(
  30. string password,
  31. AlgorithmIdentifier keyDerivationAlgorithm)
  32. : this(password.ToCharArray(), keyDerivationAlgorithm)
  33. {
  34. }
  35. public CmsPbeKey(
  36. char[] password,
  37. byte[] salt,
  38. int iterationCount)
  39. {
  40. this.password = (char[])password.Clone();
  41. this.salt = Arrays.Clone(salt);
  42. this.iterationCount = iterationCount;
  43. }
  44. public CmsPbeKey(
  45. char[] password,
  46. AlgorithmIdentifier keyDerivationAlgorithm)
  47. {
  48. if (!keyDerivationAlgorithm.Algorithm.Equals(PkcsObjectIdentifiers.IdPbkdf2))
  49. throw new ArgumentException("Unsupported key derivation algorithm: "
  50. + keyDerivationAlgorithm.Algorithm);
  51. Pbkdf2Params kdfParams = Pbkdf2Params.GetInstance(
  52. keyDerivationAlgorithm.Parameters.ToAsn1Object());
  53. this.password = (char[])password.Clone();
  54. this.salt = kdfParams.GetSalt();
  55. this.iterationCount = kdfParams.IterationCount.IntValue;
  56. }
  57. ~CmsPbeKey()
  58. {
  59. Array.Clear(this.password, 0, this.password.Length);
  60. }
  61. [Obsolete("Will be removed")]
  62. public string Password
  63. {
  64. get { return new string(password); }
  65. }
  66. public byte[] Salt
  67. {
  68. get { return Arrays.Clone(salt); }
  69. }
  70. [Obsolete("Use 'Salt' property instead")]
  71. public byte[] GetSalt()
  72. {
  73. return Salt;
  74. }
  75. public int IterationCount
  76. {
  77. get { return iterationCount; }
  78. }
  79. public string Algorithm
  80. {
  81. get { return "PKCS5S2"; }
  82. }
  83. public string Format
  84. {
  85. get { return "RAW"; }
  86. }
  87. public byte[] GetEncoded()
  88. {
  89. return null;
  90. }
  91. internal abstract KeyParameter GetEncoded(string algorithmOid);
  92. }
  93. }
  94. #pragma warning restore
  95. #endif