PbeParametersGenerator.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto
  7. {
  8. /**
  9. * super class for all Password Based Encyrption (Pbe) parameter generator classes.
  10. */
  11. public abstract class PbeParametersGenerator
  12. {
  13. protected byte[] mPassword;
  14. protected byte[] mSalt;
  15. protected int mIterationCount;
  16. /**
  17. * base constructor.
  18. */
  19. protected PbeParametersGenerator()
  20. {
  21. }
  22. /**
  23. * initialise the Pbe generator.
  24. *
  25. * @param password the password converted into bytes (see below).
  26. * @param salt the salt to be mixed with the password.
  27. * @param iterationCount the number of iterations the "mixing" function
  28. * is to be applied for.
  29. */
  30. public virtual void Init(
  31. byte[] password,
  32. byte[] salt,
  33. int iterationCount)
  34. {
  35. if (password == null)
  36. throw new ArgumentNullException("password");
  37. if (salt == null)
  38. throw new ArgumentNullException("salt");
  39. this.mPassword = Arrays.Clone(password);
  40. this.mSalt = Arrays.Clone(salt);
  41. this.mIterationCount = iterationCount;
  42. }
  43. public virtual byte[] Password
  44. {
  45. get { return Arrays.Clone(mPassword); }
  46. }
  47. public virtual byte[] Salt
  48. {
  49. get { return Arrays.Clone(mSalt); }
  50. }
  51. /**
  52. * return the iteration count.
  53. *
  54. * @return the iteration count.
  55. */
  56. public virtual int IterationCount
  57. {
  58. get { return mIterationCount; }
  59. }
  60. public abstract ICipherParameters GenerateDerivedParameters(string algorithm, int keySize);
  61. public abstract ICipherParameters GenerateDerivedParameters(string algorithm, int keySize, int ivSize);
  62. /**
  63. * Generate derived parameters for a key of length keySize, specifically
  64. * for use with a MAC.
  65. *
  66. * @param keySize the length, in bits, of the key required.
  67. * @return a parameters object representing a key.
  68. */
  69. public abstract ICipherParameters GenerateDerivedMacParameters(int keySize);
  70. /**
  71. * converts a password to a byte array according to the scheme in
  72. * Pkcs5 (ascii, no padding)
  73. *
  74. * @param password a character array representing the password.
  75. * @return a byte array representing the password.
  76. */
  77. public static byte[] Pkcs5PasswordToBytes(
  78. char[] password)
  79. {
  80. if (password == null)
  81. return new byte[0];
  82. return Strings.ToByteArray(password);
  83. }
  84. /**
  85. * converts a password to a byte array according to the scheme in
  86. * PKCS5 (UTF-8, no padding)
  87. *
  88. * @param password a character array representing the password.
  89. * @return a byte array representing the password.
  90. */
  91. public static byte[] Pkcs5PasswordToUtf8Bytes(
  92. char[] password)
  93. {
  94. if (password == null)
  95. return new byte[0];
  96. return Encoding.UTF8.GetBytes(password);
  97. }
  98. /**
  99. * converts a password to a byte array according to the scheme in
  100. * Pkcs12 (unicode, big endian, 2 zero pad bytes at the end).
  101. *
  102. * @param password a character array representing the password.
  103. * @return a byte array representing the password.
  104. */
  105. public static byte[] Pkcs12PasswordToBytes(
  106. char[] password)
  107. {
  108. return Pkcs12PasswordToBytes(password, false);
  109. }
  110. public static byte[] Pkcs12PasswordToBytes(
  111. char[] password,
  112. bool wrongPkcs12Zero)
  113. {
  114. if (password == null || password.Length < 1)
  115. {
  116. return new byte[wrongPkcs12Zero ? 2 : 0];
  117. }
  118. // +1 for extra 2 pad bytes.
  119. byte[] bytes = new byte[(password.Length + 1) * 2];
  120. Encoding.BigEndianUnicode.GetBytes(password, 0, password.Length, bytes, 0);
  121. return bytes;
  122. }
  123. }
  124. }
  125. #pragma warning restore
  126. #endif