Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

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

import java.security.NoSuchAlgorithmException;

import java.security.spec.InvalidKeySpecException;

public class Main {
    public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA1";
    public static final int HASH_LENGTH = 256;
    public static final int PBKDF2_ITERATIONS = 1000;

    public static byte[] createSaltedHash(String input, byte[] salt)
            throws InvalidKeySpecException, NoSuchAlgorithmException {
        char[] inputToCharArray = input.toCharArray();

        return doPBKDF2(inputToCharArray, salt, PBKDF2_ITERATIONS, HASH_LENGTH);
    }

    private static byte[] doPBKDF2(char[] password, byte[] salt, int iterations, int bits)
            throws NoSuchAlgorithmException, InvalidKeySpecException {
        PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bits);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);
        return skf.generateSecret(spec).getEncoded();
    }
}