Java Hash Calculate getHash(String password, String bsalt)

Here you can find the source of getHash(String password, String bsalt)

Description

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

License

Apache License

Parameter

Parameter Description
password String The password to encrypt

Exception

Parameter Description
NoSuchAlgorithmException If the algorithm doesn't exist
IOException if it fails to convert

Return

String, The digested password

Declaration

public static String getHash(String password, String bsalt) throws NoSuchAlgorithmException, IOException 

Method Source Code


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

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.IOException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static final int ITERATION_NUMBER = 1337;

    /**//from  w  w w. j ava  2s  .c  o m
     * From a password, a number of iterations and a provided salt,
     * returns the corresponding digest
     * @param password String The password to encrypt
     * @return String, The digested password
     * @throws NoSuchAlgorithmException If the algorithm doesn't exist
     * @throws IOException if it fails to convert
     */
    public static String getHash(String password, String bsalt) throws NoSuchAlgorithmException, IOException {
        byte[] salt = base64ToByte(bsalt);
        byte[] input = calculateHash(password, salt);
        return byteToBase64(input);
    }

    /**
     * From a base 64 representation, returns the corresponding byte[]
     * @param data String The base64 representation
     * @return byte[]
     * @throws IOException
     */
    public static byte[] base64ToByte(String data) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        return decoder.decodeBuffer(data);
    }

    /**
     * Helper method that calculates the SHA-512 hash||salt
     * From a password, a number of iterations and a generated salt,
     * returns the corresponding digest and salt
     * @param password String The password to encrypt
     * @param salt byte[] The salt used in the hashing
     * @return byte[], the digested password
     * @throws NoSuchAlgorithmException If the algorithm doesn't exist
     * @throws IOException if it fails to convert
     */
    private static byte[] calculateHash(String password, byte[] salt) throws NoSuchAlgorithmException, IOException {
        MessageDigest digest = MessageDigest.getInstance("SHA-512");
        digest.reset();
        digest.update(salt);
        byte[] input = digest.digest(password.getBytes("UTF-8"));
        for (int i = 0; i < ITERATION_NUMBER; i++) {
            digest.reset();
            input = digest.digest(input);
        }
        return input;
    }

    /**
     * From a byte[] returns a base 64 representation
     * @param data byte[]
     * @return String
     * @throws IOException
     */
    public static String byteToBase64(byte[] data) {
        BASE64Encoder endecoder = new BASE64Encoder();
        return endecoder.encode(data);
    }
}

Related

  1. getHash(String key)
  2. getHash(String message)
  3. getHash(String pass, String algorithm)
  4. getHash(String password, byte[] salt)
  5. getHash(String password, byte[] salt)
  6. getHash(String password, String salt)
  7. getHash(String password, String salt)
  8. getHash(String phrase, String algorithm)
  9. getHash(String s)