Java Digest digest(byte[] input, String algorithm, byte[] salt, int iterations)

Here you can find the source of digest(byte[] input, String algorithm, byte[] salt, int iterations)

Description

digest

License

Apache License

Declaration

private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) 

Method Source Code

//package com.java2s;
/**/*w w w.  jav  a  2  s  . co  m*/
 * Copyright (c) 2005-2012 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;

public class Main {

    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 new RuntimeException(e);
        }
    }

    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 new RuntimeException(e);
        }
    }
}

Related

  1. digest(byte[] data)
  2. digest(byte[] data, String algorithm)
  3. digest(byte[] data, String algorithm)
  4. digest(byte[] input)
  5. digest(byte[] input, final String algorithm)
  6. digest(byte[] message, String hash_alg)
  7. digest(final @Nullable String[] tokens, @Nullable final Date[] dates)
  8. digest(final byte[] data)
  9. digest(final InputStream inputStream, final MessageDigest digester)