PbeParametersGenerator.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Text;
  4. using Org.BouncyCastle.Utilities;
  5. namespace Org.BouncyCastle.Crypto
  6. {
  7. /**
  8. * super class for all Password Based Encyrption (Pbe) parameter generator classes.
  9. */
  10. public abstract class PbeParametersGenerator
  11. {
  12. protected byte[] mPassword;
  13. protected byte[] mSalt;
  14. protected int mIterationCount;
  15. /**
  16. * base constructor.
  17. */
  18. protected PbeParametersGenerator()
  19. {
  20. }
  21. /**
  22. * initialise the Pbe generator.
  23. *
  24. * @param password the password converted into bytes (see below).
  25. * @param salt the salt to be mixed with the password.
  26. * @param iterationCount the number of iterations the "mixing" function
  27. * is to be applied for.
  28. */
  29. public virtual void Init(
  30. byte[] password,
  31. byte[] salt,
  32. int iterationCount)
  33. {
  34. if (password == null)
  35. throw new ArgumentNullException("password");
  36. if (salt == null)
  37. throw new ArgumentNullException("salt");
  38. this.mPassword = Arrays.Clone(password);
  39. this.mSalt = Arrays.Clone(salt);
  40. this.mIterationCount = iterationCount;
  41. }
  42. public virtual byte[] Password
  43. {
  44. get { return Arrays.Clone(mPassword); }
  45. }
  46. /**
  47. * return the password byte array.
  48. *
  49. * @return the password byte array.
  50. */
  51. [Obsolete("Use 'Password' property")]
  52. public byte[] GetPassword()
  53. {
  54. return Password;
  55. }
  56. public virtual byte[] Salt
  57. {
  58. get { return Arrays.Clone(mSalt); }
  59. }
  60. /**
  61. * return the salt byte array.
  62. *
  63. * @return the salt byte array.
  64. */
  65. [Obsolete("Use 'Salt' property")]
  66. public byte[] GetSalt()
  67. {
  68. return Salt;
  69. }
  70. /**
  71. * return the iteration count.
  72. *
  73. * @return the iteration count.
  74. */
  75. public virtual int IterationCount
  76. {
  77. get { return mIterationCount; }
  78. }
  79. /**
  80. * Generate derived parameters for a key of length keySize.
  81. *
  82. * @param keySize the length, in bits, of the key required.
  83. * @return a parameters object representing a key.
  84. */
  85. [Obsolete("Use version with 'algorithm' parameter")]
  86. public abstract ICipherParameters GenerateDerivedParameters(int keySize);
  87. public abstract ICipherParameters GenerateDerivedParameters(string algorithm, int keySize);
  88. /**
  89. * Generate derived parameters for a key of length keySize, and
  90. * an initialisation vector (IV) of length ivSize.
  91. *
  92. * @param keySize the length, in bits, of the key required.
  93. * @param ivSize the length, in bits, of the iv required.
  94. * @return a parameters object representing a key and an IV.
  95. */
  96. [Obsolete("Use version with 'algorithm' parameter")]
  97. public abstract ICipherParameters GenerateDerivedParameters(int keySize, int ivSize);
  98. public abstract ICipherParameters GenerateDerivedParameters(string algorithm, int keySize, int ivSize);
  99. /**
  100. * Generate derived parameters for a key of length keySize, specifically
  101. * for use with a MAC.
  102. *
  103. * @param keySize the length, in bits, of the key required.
  104. * @return a parameters object representing a key.
  105. */
  106. public abstract ICipherParameters GenerateDerivedMacParameters(int keySize);
  107. /**
  108. * converts a password to a byte array according to the scheme in
  109. * Pkcs5 (ascii, no padding)
  110. *
  111. * @param password a character array representing the password.
  112. * @return a byte array representing the password.
  113. */
  114. public static byte[] Pkcs5PasswordToBytes(
  115. char[] password)
  116. {
  117. if (password == null)
  118. return new byte[0];
  119. return Strings.ToByteArray(password);
  120. }
  121. [Obsolete("Use version taking 'char[]' instead")]
  122. public static byte[] Pkcs5PasswordToBytes(
  123. string password)
  124. {
  125. if (password == null)
  126. return new byte[0];
  127. return Strings.ToByteArray(password);
  128. }
  129. /**
  130. * converts a password to a byte array according to the scheme in
  131. * PKCS5 (UTF-8, no padding)
  132. *
  133. * @param password a character array representing the password.
  134. * @return a byte array representing the password.
  135. */
  136. public static byte[] Pkcs5PasswordToUtf8Bytes(
  137. char[] password)
  138. {
  139. if (password == null)
  140. return new byte[0];
  141. return Encoding.UTF8.GetBytes(password);
  142. }
  143. [Obsolete("Use version taking 'char[]' instead")]
  144. public static byte[] Pkcs5PasswordToUtf8Bytes(
  145. string password)
  146. {
  147. if (password == null)
  148. return new byte[0];
  149. return Encoding.UTF8.GetBytes(password);
  150. }
  151. /**
  152. * converts a password to a byte array according to the scheme in
  153. * Pkcs12 (unicode, big endian, 2 zero pad bytes at the end).
  154. *
  155. * @param password a character array representing the password.
  156. * @return a byte array representing the password.
  157. */
  158. public static byte[] Pkcs12PasswordToBytes(
  159. char[] password)
  160. {
  161. return Pkcs12PasswordToBytes(password, false);
  162. }
  163. public static byte[] Pkcs12PasswordToBytes(
  164. char[] password,
  165. bool wrongPkcs12Zero)
  166. {
  167. if (password == null || password.Length < 1)
  168. {
  169. return new byte[wrongPkcs12Zero ? 2 : 0];
  170. }
  171. // +1 for extra 2 pad bytes.
  172. byte[] bytes = new byte[(password.Length + 1) * 2];
  173. Encoding.BigEndianUnicode.GetBytes(password, 0, password.Length, bytes, 0);
  174. return bytes;
  175. }
  176. }
  177. }
  178. #endif