Java Hash Code Calculate generateHash(char[] password, byte[] salt)

Here you can find the source of generateHash(char[] password, byte[] salt)

Description

generate Hash

License

Open Source License

Declaration

public static byte[] generateHash(char[] password, byte[] salt) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.security.NoSuchAlgorithmException;

import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

public class Main {
    public static final int KEY_LENGTH = 256;
    public static final int HASH_ITERATIONS = 1500;

    public static byte[] generateHash(char[] password, byte[] salt) {
        PBEKeySpec spec = new PBEKeySpec(password, salt, HASH_ITERATIONS, KEY_LENGTH);
        Arrays.fill(password, Character.MIN_VALUE);
        try {//from w  w w.  j  a v  a 2 s .c  o m
            SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
            return skf.generateSecret(spec).getEncoded();
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            throw new AssertionError("Error while hashing a password: " + e.getMessage(), e);
        } finally {
            spec.clearPassword();
        }
    }
}

Related

  1. combineHashesBad(int hash1, int hash2)
  2. combineHashesMurmur(int hash2, int hash1)
  3. combineHashesOld(int hash1, int hash2)
  4. combineHashesUnsorted(final int a, final int b)
  5. generateHash(byte[] data)
  6. generateHash(File file)
  7. generateHash(final String data)
  8. generateHash(final String input)
  9. generateHash(final String msg, final String hashAlgorithm)