@NotNull private static String generateSalt(int saltSize) { SecureRandom random = new SecureRandom(); byte[] salt = new byte[saltSize]; random.nextBytes(salt); return convertBytesToHex(salt); }
@Nonnull private static String generateSalt(int saltSize) { SecureRandom random = new SecureRandom(); byte[] salt = new byte[saltSize]; random.nextBytes(salt); return convertBytesToHex(salt); }
@NotNull private static String generateDigest(@NotNull String data, @NotNull String algorithm, int iterations) throws UnsupportedEncodingException, NoSuchAlgorithmException { byte[] bytes = data.getBytes(ENCODING); MessageDigest md = MessageDigest.getInstance(algorithm); for (int i = 0; i < iterations; i++) { md.reset(); bytes = md.digest(bytes); } return convertBytesToHex(bytes); }
@Nonnull private static String generateDigest(@Nonnull String data, @Nonnull String algorithm, int iterations) throws UnsupportedEncodingException, NoSuchAlgorithmException { byte[] bytes = data.getBytes(ENCODING); MessageDigest md = MessageDigest.getInstance(algorithm); for (int i = 0; i < iterations; i++) { md.reset(); bytes = md.digest(bytes); } return convertBytesToHex(bytes); }
@NotNull private static String generatePBKDF2(@NotNull String pwd, @NotNull String salt, @NotNull String algorithm, int iterations, int keyLength) throws NoSuchAlgorithmException { // for example PBKDF2WithHmacSHA1 SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm); byte[] saltBytes = convertHexToBytes(salt); KeySpec keyspec = new PBEKeySpec(pwd.toCharArray(), saltBytes, iterations, keyLength); try { Key key = factory.generateSecret(keyspec); byte[] bytes = key.getEncoded(); return convertBytesToHex(bytes); } catch (InvalidKeySpecException e) { throw new NoSuchAlgorithmException(algorithm, e); } }
@Nonnull private static String generatePBKDF2(@Nonnull String pwd, @Nonnull String salt, @Nonnull String algorithm, int iterations, int keyLength) throws NoSuchAlgorithmException { // for example PBKDF2WithHmacSHA1 SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm); byte[] saltBytes = convertHexToBytes(salt); KeySpec keyspec = new PBEKeySpec(pwd.toCharArray(), saltBytes, iterations, keyLength); try { Key key = factory.generateSecret(keyspec); byte[] bytes = key.getEncoded(); return convertBytesToHex(bytes); } catch (InvalidKeySpecException e) { throw new NoSuchAlgorithmException(algorithm, e); } }