Java Hash Calculate getHash(int iterationNb, String password, byte[] salt)

Here you can find the source of getHash(int iterationNb, String password, byte[] salt)

Description

From a password, a number of iterations and a salt, returns the corresponding digest

License

Apache License

Parameter

Parameter Description
iterationNb int The number of iterations of the algorithm
password String The password to encrypt
salt byte[] The salt

Exception

Parameter Description
NoSuchAlgorithmException If the algorithm doesn't exist

Return

byte[] The digested password

Declaration

private static byte[] getHash(int iterationNb, String password, byte[] salt)
        throws NoSuchAlgorithmException, UnsupportedEncodingException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**//from  ww w. j a v  a 2s  .  c o  m
     * From a password, a number of iterations and a salt,
     * returns the corresponding digest
     * @param iterationNb int The number of iterations of the algorithm
     * @param password String The password to encrypt
     * @param salt byte[] The salt
     * @return byte[] The digested password
     * @throws NoSuchAlgorithmException If the algorithm doesn't exist
     */
    private static byte[] getHash(int iterationNb, String password, byte[] salt)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        digest.reset();
        digest.update(salt);
        byte[] input = digest.digest(password.getBytes("UTF-8"));
        for (int i = 0; i < iterationNb; i++) {
            digest.reset();
            input = digest.digest(input);
        }
        return input;
    }
}

Related

  1. getHash(File file, String hashType)
  2. getHash(final byte[] data, final String hashAlgorithm)
  3. getHash(final String text, final Charset charset, final MessageDigest algorithm)
  4. getHash(InputStream in, String algorithm)
  5. getHash(InputStream is, String algorithm)
  6. getHash(String algorithm, int i)
  7. getHash(String credentials)
  8. getHash(String Data)
  9. getHash(String fileName, String hashType)