Java XML Hash hash(String data, String salt)

Here you can find the source of hash(String data, String salt)

Description

Hashes a string.

License

Open Source License

Parameter

Parameter Description
data data to hash
salt random salt

Exception

Parameter Description

Return

hashed string

Declaration

public static String hash(String data, String salt) throws Exception 

Method Source Code

//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);
    }
}

Related

  1. calculateHash(String password, String salt)
  2. createHash(String password)
  3. getHash(final byte[] data, MessageDigest algo)
  4. hash(byte[] bytes)
  5. hash(final String text, final String algorithm)
  6. hash512(byte[] data)
  7. hashPass(String plaintext)
  8. hashPassword(char[] password, String salt, String hashAlgo)
  9. hashPassword(String password)