PbeParametersGenerator.cs 6.0 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. 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. public byte[] GetSalt()
  66. {
  67. return Salt;
  68. }
  69. /**
  70. * return the iteration count.
  71. *
  72. * @return the iteration count.
  73. */
  74. public virtual int IterationCount
  75. {
  76. get { return mIterationCount; }
  77. }
  78. /**
  79. * Generate derived parameters for a key of length keySize.
  80. *
  81. * @param keySize the length, in bits, of the key required.
  82. * @return a parameters object representing a key.
  83. */
  84. public abstract ICipherParameters GenerateDerivedParameters(int keySize);
  85. public abstract ICipherParameters GenerateDerivedParameters(string algorithm, int keySize);
  86. /**
  87. * Generate derived parameters for a key of length keySize, and
  88. * an initialisation vector (IV) of length ivSize.
  89. *
  90. * @param keySize the length, in bits, of the key required.
  91. * @param ivSize the length, in bits, of the iv required.
  92. * @return a parameters object representing a key and an IV.
  93. */
  94. public abstract ICipherParameters GenerateDerivedParameters(int keySize, int ivSize);
  95. public abstract ICipherParameters GenerateDerivedParameters(string algorithm, int keySize, int ivSize);
  96. /**
  97. * Generate derived parameters for a key of length keySize, specifically
  98. * for use with a MAC.
  99. *
  100. * @param keySize the length, in bits, of the key required.
  101. * @return a parameters object representing a key.
  102. */
  103. public abstract ICipherParameters GenerateDerivedMacParameters(int keySize);
  104. /**
  105. * converts a password to a byte array according to the scheme in
  106. * Pkcs5 (ascii, no padding)
  107. *
  108. * @param password a character array representing the password.
  109. * @return a byte array representing the password.
  110. */
  111. public static byte[] Pkcs5PasswordToBytes(
  112. char[] password)
  113. {
  114. if (password == null)
  115. return new byte[0];
  116. return Strings.ToByteArray(password);
  117. }
  118. public static byte[] Pkcs5PasswordToBytes(
  119. string password)
  120. {
  121. if (password == null)
  122. return new byte[0];
  123. return Strings.ToByteArray(password);
  124. }
  125. /**
  126. * converts a password to a byte array according to the scheme in
  127. * PKCS5 (UTF-8, no padding)
  128. *
  129. * @param password a character array representing the password.
  130. * @return a byte array representing the password.
  131. */
  132. public static byte[] Pkcs5PasswordToUtf8Bytes(
  133. char[] password)
  134. {
  135. if (password == null)
  136. return new byte[0];
  137. return Encoding.UTF8.GetBytes(password);
  138. }
  139. public static byte[] Pkcs5PasswordToUtf8Bytes(
  140. string password)
  141. {
  142. if (password == null)
  143. return new byte[0];
  144. return Encoding.UTF8.GetBytes(password);
  145. }
  146. /**
  147. * converts a password to a byte array according to the scheme in
  148. * Pkcs12 (unicode, big endian, 2 zero pad bytes at the end).
  149. *
  150. * @param password a character array representing the password.
  151. * @return a byte array representing the password.
  152. */
  153. public static byte[] Pkcs12PasswordToBytes(
  154. char[] password)
  155. {
  156. return Pkcs12PasswordToBytes(password, false);
  157. }
  158. public static byte[] Pkcs12PasswordToBytes(
  159. char[] password,
  160. bool wrongPkcs12Zero)
  161. {
  162. if (password == null || password.Length < 1)
  163. {
  164. return new byte[wrongPkcs12Zero ? 2 : 0];
  165. }
  166. // +1 for extra 2 pad bytes.
  167. byte[] bytes = new byte[(password.Length + 1) * 2];
  168. Encoding.BigEndianUnicode.GetBytes(password, 0, password.Length, bytes, 0);
  169. return bytes;
  170. }
  171. }
  172. }
  173. #pragma warning restore
  174. #endif