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
- getHash(String key)
- getHash(String message)
- getHash(String pass, String algorithm)
- getHash(String password, byte[] salt)
- getHash(String password, byte[] salt)
- getHash(String password, String salt)
- getHash(String password, String salt)
- getHash(String phrase, String algorithm)
- getHash(String s)