· 6 years ago · Jul 01, 2019, 11:58 AM
1package com.stefanovskyi.password;
2
3import org.apache.commons.codec.binary.Hex;
4
5import javax.crypto.SecretKey;
6import javax.crypto.SecretKeyFactory;
7import javax.crypto.spec.PBEKeySpec;
8
9public class HashingPasswordUtil {
10 private static final int ITERATIONS = 10000;
11 private static final int KEY_LENGTH = 512;
12 private static final String SALT = "saltKey";
13
14 public String convertStringToHash(String password) {
15 return Hex.encodeHexString(hashPassword(password.toCharArray()));
16 }
17
18 private byte[] hashPassword(final char[] password) {
19 SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
20 PBEKeySpec spec = new PBEKeySpec(password, SALT.getBytes(), ITERATIONS, KEY_LENGTH);
21 SecretKey key = secretKeyFactory.generateSecret(spec);
22
23 return key.getEncoded();
24 }
25}