PbeParametersGenerator.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.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. /**
  48. * return the password byte array.
  49. *
  50. * @return the password byte array.
  51. */
  52. [Obsolete("Use 'Password' property")]
  53. public byte[] GetPassword()
  54. {
  55. return Password;
  56. }
  57. public virtual byte[] Salt
  58. {
  59. get { return Arrays.Clone(mSalt); }
  60. }
  61. /**
  62. * return the salt byte array.
  63. *
  64. * @return the salt byte array.
  65. */
  66. [Obsolete("Use 'Salt' property")]
  67. public byte[] GetSalt()
  68. {
  69. return Salt;
  70. }
  71. /**
  72. * return the iteration count.
  73. *
  74. * @return the iteration count.
  75. */
  76. public virtual int IterationCount
  77. {
  78. get { return mIterationCount; }
  79. }
  80. /**
  81. * Generate derived parameters for a key of length keySize.
  82. *
  83. * @param keySize the length, in bits, of the key required.
  84. * @return a parameters object representing a key.
  85. */
  86. [Obsolete("Use version with 'algorithm' parameter")]
  87. public abstract ICipherParameters GenerateDerivedParameters(int keySize);
  88. public abstract ICipherParameters GenerateDerivedParameters(string algorithm, int keySize);
  89. /**
  90. * Generate derived parameters for a key of length keySize, and
  91. * an initialisation vector (IV) of length ivSize.
  92. *
  93. * @param keySize the length, in bits, of the key required.
  94. * @param ivSize the length, in bits, of the iv required.
  95. * @return a parameters object representing a key and an IV.
  96. */
  97. [Obsolete("Use version with 'algorithm' parameter")]
  98. public abstract ICipherParameters GenerateDerivedParameters(int keySize, int ivSize);
  99. public abstract ICipherParameters GenerateDerivedParameters(string algorithm, int keySize, int ivSize);
  100. /**
  101. * Generate derived parameters for a key of length keySize, specifically
  102. * for use with a MAC.
  103. *
  104. * @param keySize the length, in bits, of the key required.
  105. * @return a parameters object representing a key.
  106. */
  107. public abstract ICipherParameters GenerateDerivedMacParameters(int keySize);
  108. /**
  109. * converts a password to a byte array according to the scheme in
  110. * Pkcs5 (ascii, no padding)
  111. *
  112. * @param password a character array representing the password.
  113. * @return a byte array representing the password.
  114. */
  115. public static byte[] Pkcs5PasswordToBytes(
  116. char[] password)
  117. {
  118. if (password == null)
  119. return new byte[0];
  120. return Strings.ToByteArray(password);
  121. }
  122. [Obsolete("Use version taking 'char[]' instead")]
  123. public static byte[] Pkcs5PasswordToBytes(
  124. string password)
  125. {
  126. if (password == null)
  127. return new byte[0];
  128. return Strings.ToByteArray(password);
  129. }
  130. /**
  131. * converts a password to a byte array according to the scheme in
  132. * PKCS5 (UTF-8, no padding)
  133. *
  134. * @param password a character array representing the password.
  135. * @return a byte array representing the password.
  136. */
  137. public static byte[] Pkcs5PasswordToUtf8Bytes(
  138. char[] password)
  139. {
  140. if (password == null)
  141. return new byte[0];
  142. return Encoding.UTF8.GetBytes(password);
  143. }
  144. [Obsolete("Use version taking 'char[]' instead")]
  145. public static byte[] Pkcs5PasswordToUtf8Bytes(
  146. string password)
  147. {
  148. if (password == null)
  149. return new byte[0];
  150. return Encoding.UTF8.GetBytes(password);
  151. }
  152. /**
  153. * converts a password to a byte array according to the scheme in
  154. * Pkcs12 (unicode, big endian, 2 zero pad bytes at the end).
  155. *
  156. * @param password a character array representing the password.
  157. * @return a byte array representing the password.
  158. */
  159. public static byte[] Pkcs12PasswordToBytes(
  160. char[] password)
  161. {
  162. return Pkcs12PasswordToBytes(password, false);
  163. }
  164. public static byte[] Pkcs12PasswordToBytes(
  165. char[] password,
  166. bool wrongPkcs12Zero)
  167. {
  168. if (password == null || password.Length < 1)
  169. {
  170. return new byte[wrongPkcs12Zero ? 2 : 0];
  171. }
  172. // +1 for extra 2 pad bytes.
  173. byte[] bytes = new byte[(password.Length + 1) * 2];
  174. Encoding.BigEndianUnicode.GetBytes(password, 0, password.Length, bytes, 0);
  175. return bytes;
  176. }
  177. }
  178. }
  179. #pragma warning restore
  180. #endif