Rfc2898DeriveBytes.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.zdd.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.InvalidKeyException;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.util.Random;
  6. import javax.crypto.Mac;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import org.apache.commons.codec.binary.Base64;
  9. /**
  10. * This implementation follows RFC 2898 recommendations. See
  11. * http://www.ietf.org/rfc/Rfc2898.txt
  12. */
  13. public class Rfc2898DeriveBytes {
  14. private static final int BLOCK_SIZE = 20;
  15. private static Random random = new Random();
  16. private Mac hmacsha1;
  17. private byte[] salt;
  18. private int iterations;
  19. private byte[] buffer = new byte[BLOCK_SIZE];
  20. private int startIndex = 0;
  21. private int endIndex = 0;
  22. private int block = 1;
  23. /**
  24. * Creates new instance.
  25. *
  26. * @param password
  27. * The password used to derive the key.
  28. * @param salt
  29. * The key salt used to derive the key.
  30. * @param iterations
  31. * The number of iterations for the operation.
  32. * @throws NoSuchAlgorithmException
  33. * HmacSHA1 algorithm cannot be found.
  34. * @throws InvalidKeyException
  35. * Salt must be 8 bytes or more. -or- Password cannot be null.
  36. */
  37. public Rfc2898DeriveBytes(byte[] password, byte[] salt, int iterations) throws NoSuchAlgorithmException,
  38. InvalidKeyException {
  39. this.salt = salt;
  40. this.iterations = iterations;
  41. this.hmacsha1 = Mac.getInstance("HmacSHA1");
  42. this.hmacsha1.init(new SecretKeySpec(password, "HmacSHA1"));
  43. }
  44. /**
  45. * Creates new instance.
  46. *
  47. * @param password
  48. * The password used to derive the key.
  49. * @param salt
  50. * The key salt used to derive the key.
  51. * @param iterations
  52. * The number of iterations for the operation.
  53. * @throws NoSuchAlgorithmException
  54. * HmacSHA1 algorithm cannot be found.
  55. * @throws InvalidKeyException
  56. * Salt must be 8 bytes or more. -or- Password cannot be null.
  57. * @throws UnsupportedEncodingException
  58. */
  59. public Rfc2898DeriveBytes(String password, int saltSize, int iterations) throws NoSuchAlgorithmException,
  60. InvalidKeyException, UnsupportedEncodingException {
  61. this.salt = randomSalt(saltSize);
  62. this.iterations = iterations;
  63. this.hmacsha1 = Mac.getInstance("HmacSHA1");
  64. this.hmacsha1.init(new SecretKeySpec(password.getBytes("UTF-8"), "HmacSHA1"));
  65. this.buffer = new byte[BLOCK_SIZE];
  66. this.block = 1;
  67. this.startIndex = this.endIndex = 1;
  68. }
  69. /**
  70. * Creates new instance.
  71. *
  72. * @param password
  73. * The password used to derive the key.
  74. * @param salt
  75. * The key salt used to derive the key.
  76. * @param iterations
  77. * The number of iterations for the operation.
  78. * @throws NoSuchAlgorithmException
  79. * HmacSHA1 algorithm cannot be found.
  80. * @throws InvalidKeyException
  81. * Salt must be 8 bytes or more. -or- Password cannot be null.
  82. * @throws UnsupportedEncodingException
  83. */
  84. public Rfc2898DeriveBytes(String password, int saltSize) throws NoSuchAlgorithmException, InvalidKeyException,
  85. UnsupportedEncodingException {
  86. this(password, saltSize, 1000);
  87. }
  88. /**
  89. * Creates new instance.
  90. *
  91. * @param password
  92. * The password used to derive the key.
  93. * @param salt
  94. * The key salt used to derive the key.
  95. * @param iterations
  96. * The number of iterations for the operation.
  97. * @throws NoSuchAlgorithmException
  98. * HmacSHA1 algorithm cannot be found.
  99. * @throws InvalidKeyException
  100. * Salt must be 8 bytes or more. -or- Password cannot be null.
  101. * @throws UnsupportedEncodingException
  102. * UTF-8 encoding is not supported.
  103. */
  104. public Rfc2898DeriveBytes(String password, byte[] salt, int iterations) throws InvalidKeyException,
  105. NoSuchAlgorithmException, UnsupportedEncodingException {
  106. this(password.getBytes("UTF8"), salt, iterations);
  107. }
  108. public byte[] getSalt() {
  109. return this.salt;
  110. }
  111. public String getSaltAsString() {
  112. return Base64.encodeBase64String(this.salt);
  113. }
  114. /**
  115. * Returns a pseudo-random key from a data, salt and iteration count.
  116. *
  117. * @param cb
  118. * Number of bytes to return.
  119. * @return Byte array.
  120. */
  121. public byte[] getBytes(int cb) {
  122. byte[] result = new byte[cb];
  123. int offset = 0;
  124. int size = this.endIndex - this.startIndex;
  125. if (size > 0) { // if there is some data in buffer
  126. if (cb >= size) { // if there is enough data in buffer
  127. System.arraycopy(this.buffer, this.startIndex, result, 0, size);
  128. this.startIndex = this.endIndex = 0;
  129. offset += size;
  130. } else {
  131. System.arraycopy(this.buffer, this.startIndex, result, 0, cb);
  132. startIndex += cb;
  133. return result;
  134. }
  135. }
  136. while (offset < cb) {
  137. byte[] block = this.func();
  138. int remainder = cb - offset;
  139. if (remainder > BLOCK_SIZE) {
  140. System.arraycopy(block, 0, result, offset, BLOCK_SIZE);
  141. offset += BLOCK_SIZE;
  142. } else {
  143. System.arraycopy(block, 0, result, offset, remainder);
  144. offset += remainder;
  145. System.arraycopy(block, remainder, this.buffer, startIndex, BLOCK_SIZE - remainder);
  146. endIndex += (BLOCK_SIZE - remainder);
  147. return result;
  148. }
  149. }
  150. return result;
  151. }
  152. public static byte[] randomSalt(int size) {
  153. byte[] salt = new byte[size];
  154. random.nextBytes(salt);
  155. return salt;
  156. }
  157. /**
  158. * Generate random Salt
  159. *
  160. * @param size
  161. * @return
  162. */
  163. public static String generateSalt(int size) {
  164. byte[] salt = randomSalt(size);
  165. return Base64.encodeBase64String(salt);
  166. }
  167. private byte[] func() {
  168. this.hmacsha1.update(this.salt, 0, this.salt.length);
  169. byte[] tempHash = this.hmacsha1.doFinal(getBytesFromInt(this.block));
  170. this.hmacsha1.reset();
  171. byte[] finalHash = tempHash;
  172. for (int i = 2; i <= this.iterations; i++) {
  173. tempHash = this.hmacsha1.doFinal(tempHash);
  174. for (int j = 0; j < 20; j++) {
  175. finalHash[j] = (byte) (finalHash[j] ^ tempHash[j]);
  176. }
  177. }
  178. if (this.block == 2147483647) {
  179. this.block = -2147483648;
  180. } else {
  181. this.block += 1;
  182. }
  183. return finalHash;
  184. }
  185. private static byte[] getBytesFromInt(int i) {
  186. return new byte[] { (byte) (i >>> 24), (byte) (i >>> 16), (byte) (i >>> 8), (byte) i };
  187. }
  188. }