Exemplo n.º 1
0
  private static byte[] digest(InputStream input, String algorithm) throws IOException {
    try {
      MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
      int bufferLength = 8 * 1024;
      byte[] buffer = new byte[bufferLength];
      int read = input.read(buffer, 0, bufferLength);

      while (read > -1) {
        messageDigest.update(buffer, 0, read);
        read = input.read(buffer, 0, bufferLength);
      }

      return messageDigest.digest();
    } catch (GeneralSecurityException e) {
      throw Exceptions.unchecked(e);
    }
  }
Exemplo n.º 2
0
  /** 对字符串进行散列, 支持md5与sha1算法. */
  private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
      MessageDigest digest = MessageDigest.getInstance(algorithm);

      if (salt != null) {
        digest.update(salt);
      }

      byte[] result = digest.digest(input);

      for (int i = 1; i < iterations; i++) {
        digest.reset();
        result = digest.digest(result);
      }
      return result;
    } catch (GeneralSecurityException e) {
      throw Exceptions.unchecked(e);
    }
  }