Here you can find the source of hash(String data, String salt)
Parameter | Description |
---|---|
data | data to hash |
salt | random salt |
Parameter | Description |
---|
public static String hash(String data, String salt) throws Exception
//package com.java2s; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.xml.bind.DatatypeConverter; import java.security.NoSuchAlgorithmException; import java.security.spec.KeySpec; public class Main { private final static int ITERATIONS = 10000; private final static int KEY_LENGTH = 128; private final static String SECRET_KEY_FACTORY_ALGORITHM = "PBKDF2WithHmacSHA1"; /**//from w w w . j a va 2 s . c om * Hashes a string. * * @param data data to hash * @param salt random salt * @return hashed string * @throws java.security.NoSuchAlgorithmException, InvalidKeySpecException */ public static String hash(String data, String salt) throws Exception { KeySpec keySpec = new PBEKeySpec(data.toCharArray(), DatatypeConverter.parseBase64Binary(salt), ITERATIONS, KEY_LENGTH); SecretKeyFactory secretKeyFactory = getSecretKeyFactory(); return DatatypeConverter.printBase64Binary(secretKeyFactory.generateSecret(keySpec).getEncoded()); } /** * Generates SecretKeyFactory. * * @return SecretKeyFactory * @throws java.security.NoSuchAlgorithmException */ private static SecretKeyFactory getSecretKeyFactory() throws NoSuchAlgorithmException { return SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_ALGORITHM); } }