/** * Generates a libc crypt() compatible "$1$" MD5 based hash value. * <p> * See {@link Crypt#crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @param salt * salt string including the prefix and optionally garbage at the end. Will be generated randomly if * null. * @return the hash value * @throws IllegalArgumentException * if the salt does not match the allowed pattern * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String md5Crypt(final byte[] keyBytes, final String salt) { return md5Crypt(keyBytes, salt, MD5_PREFIX); }
/** * See {@link #apr1Crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @param salt An APR1 salt. * @return the hash value * @throws IllegalArgumentException * if the salt does not match the allowed pattern * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String apr1Crypt(final byte[] keyBytes, String salt) { // to make the md5Crypt regex happy if (salt != null && !salt.startsWith(APR1_PREFIX)) { salt = APR1_PREFIX + salt; } return Md5Crypt.md5Crypt(keyBytes, salt, APR1_PREFIX); }
/** * Generates a libc6 crypt() compatible "$1$" hash value. * <p> * See {@link Crypt#crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @return the hash value * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String md5Crypt(final byte[] keyBytes) { return md5Crypt(keyBytes, MD5_PREFIX + B64.getRandomSalt(8)); }
@Test(expected = IllegalArgumentException.class) public void testMd5CryptWithEmptySalt() { Md5Crypt.md5Crypt("secret".getBytes(), ""); } }
@Test(expected = NullPointerException.class) public void testMd5CryptNullData() { Md5Crypt.md5Crypt((byte[]) null); }
/** * Encrypts a password in a crypt(3) compatible way. * <p> * If no salt is provided, a random salt and the default algorithm (currently SHA-512) will be used. See * {@link #crypt(String, String)} for details. * * @param keyBytes * plaintext password * @param salt * salt value * @return hash value * @throws IllegalArgumentException * if the salt does not match the allowed pattern * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String crypt(final byte[] keyBytes, final String salt) { if (salt == null) { return Sha2Crypt.sha512Crypt(keyBytes); } else if (salt.startsWith(Sha2Crypt.SHA512_PREFIX)) { return Sha2Crypt.sha512Crypt(keyBytes, salt); } else if (salt.startsWith(Sha2Crypt.SHA256_PREFIX)) { return Sha2Crypt.sha256Crypt(keyBytes, salt); } else if (salt.startsWith(Md5Crypt.MD5_PREFIX)) { return Md5Crypt.md5Crypt(keyBytes, salt); } else { return UnixCrypt.crypt(keyBytes, salt); } }
/** * Generates a libc crypt() compatible "$1$" MD5 based hash value. * <p> * See {@link Crypt#crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @param salt * salt string including the prefix and optionally garbage at the end. Will be generated randomly if * null. * @return the hash value * @throws IllegalArgumentException * if the salt does not match the allowed pattern * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String md5Crypt(final byte[] keyBytes, final String salt) { return md5Crypt(keyBytes, salt, MD5_PREFIX); }
/** * Generates a libc crypt() compatible "$1$" MD5 based hash value. * <p> * See {@link Crypt#crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @param salt * salt string including the prefix and optionally garbage at the end. Will be generated randomly if * null. * @return the hash value * @throws IllegalArgumentException * if the salt does not match the allowed pattern * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String md5Crypt(final byte[] keyBytes, final String salt) { return md5Crypt(keyBytes, salt, MD5_PREFIX); }
/** * See {@link #apr1Crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @param salt An APR1 salt. * @return the hash value * @throws IllegalArgumentException * if the salt does not match the allowed pattern * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String apr1Crypt(final byte[] keyBytes, String salt) { // to make the md5Crypt regex happy if (salt != null && !salt.startsWith(APR1_PREFIX)) { salt = APR1_PREFIX + salt; } return Md5Crypt.md5Crypt(keyBytes, salt, APR1_PREFIX); }
/** * See {@link #apr1Crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @param salt An APR1 salt. * @return the hash value * @throws IllegalArgumentException * if the salt does not match the allowed pattern * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String apr1Crypt(final byte[] keyBytes, String salt) { // to make the md5Crypt regex happy if (salt != null && !salt.startsWith(APR1_PREFIX)) { salt = APR1_PREFIX + salt; } return Md5Crypt.md5Crypt(keyBytes, salt, APR1_PREFIX); }
/** * See {@link #apr1Crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @param salt An APR1 salt. * @return the hash value * @throws IllegalArgumentException * if the salt does not match the allowed pattern * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String apr1Crypt(final byte[] keyBytes, String salt) { // to make the md5Crypt regex happy if (salt != null && !salt.startsWith(APR1_PREFIX)) { salt = APR1_PREFIX + salt; } return Md5Crypt.md5Crypt(keyBytes, salt, APR1_PREFIX); }
/** * Creates a new random salt that can be passed to {@link #hash(String, String)}. * * @return A new random salt. */ public static String newSalt() { return getSalt(md5Crypt(EMPTY_KEY_BYTES)); }
/** * Generates a libc6 crypt() compatible "$1$" hash value. * <p> * See {@link Crypt#crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @return the hash value * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String md5Crypt(final byte[] keyBytes) { return md5Crypt(keyBytes, MD5_PREFIX + B64.getRandomSalt(8)); }
/** * Generates a libc6 crypt() compatible "$1$" hash value. * <p> * See {@link Crypt#crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @return the hash value * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String md5Crypt(final byte[] keyBytes) { return md5Crypt(keyBytes, MD5_PREFIX + B64.getRandomSalt(8)); }
/** * Generates a libc6 crypt() compatible "$1$" hash value. * <p> * See {@link Crypt#crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @return the hash value * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String md5Crypt(final byte[] keyBytes) { return md5Crypt(keyBytes, MD5_PREFIX + B64.getRandomSalt(8)); }
/** * Generates a libc6 crypt() compatible "$1$" hash value. * <p> * See {@link Crypt#crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @return the hash value * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String md5Crypt(final byte[] keyBytes) { return md5Crypt(keyBytes, MD5_PREFIX + B64.getRandomSalt(8)); }
/** * Generates a libc6 crypt() compatible "$1$" hash value. * <p> * See {@link Crypt#crypt(String, String)} for details. * * @param keyBytes * plaintext string to hash. * @return the hash value * @throws RuntimeException * when a {@link java.security.NoSuchAlgorithmException} is caught. */ public static String md5Crypt(final byte[] keyBytes) { return md5Crypt(keyBytes, MD5_PREFIX + B64.getRandomSalt(8)); }
/** * Hashes the specified value using the specified salt. * * <p> * <b>WARNING:</b> This method should not be used to hash sensitive information. * </p> * * @param value The value to be hashed. * @param salt The salt. May begin with {@code $1$} and end with {@code $} followed by any number of characters. * No more than eight characters will be used. If empty, a new random salt will be used. * * @return The hashed value. */ public static String hash(final String value, final String salt) { checkNotNull(value); checkNotNull(salt); return md5Crypt(value.getBytes(StandardCharsets.UTF_8), MAGIC + normalizeSalt(salt)); }
public PasswordEncrypt(final String key) { final byte[] keyBytes = key.getBytes(US_ASCII); this.md5 = Md5Crypt.md5Crypt(keyBytes.clone()); this.apr1 = Md5Crypt.apr1Crypt(keyBytes.clone()); this.sha256 = Sha2Crypt.sha256Crypt(keyBytes.clone()); this.sha512 = Sha2Crypt.sha512Crypt(keyBytes.clone()); Arrays.fill(keyBytes, (byte) 0); }