/** * Generates a crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one will be used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final byte[] original) { return crypt(original, null); }
/** * Generates a crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one is used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final String original) { return crypt(original.getBytes(Charsets.UTF_8)); }
/** * Generates a crypt(3) compatible hash using the DES algorithm. * * @param original * plaintext password * @param salt * a two character string drawn from [a-zA-Z0-9./] or null for a random one * @return a 13 character string starting with the salt string * @throws IllegalArgumentException * if the salt does not match the allowed pattern */ public static String crypt(final String original, final String salt) { return crypt(original.getBytes(Charsets.UTF_8), salt); }
/** * Unimplemented "$foo$" salt prefixes would be threated as UnixCrypt salt. */ @Test(expected = IllegalArgumentException.class) public void testUnicCryptInvalidSalt() { UnixCrypt.crypt("secret", "$a"); }
@Test(expected = IllegalArgumentException.class) public void testUnixCryptWithEmptySalt() { UnixCrypt.crypt("secret", ""); }
/** * Single character salts are illegal! * E.g. with glibc 2.13, crypt("secret", "x") = "xxZREZpkHZpkI" but * crypt("secret", "xx") = "xxWAum7tHdIUw" which makes it unverifyable. */ @Test(expected = IllegalArgumentException.class) public void testUnixCryptWithHalfSalt() { UnixCrypt.crypt("secret", "x"); }
@Test(expected = NullPointerException.class) public void testUnixCryptNullData() { UnixCrypt.crypt((byte[]) null); }
/** * Some salts are invalid for crypt(3) but not for unixCrypt(). */ @Test public void testUnixCryptExplicitCall() { // A call to crypt() with an empty salt would result in a "$6$" hash. // Using unixCrypt() explicitly results in a random salt. assertTrue(UnixCrypt.crypt("secret".getBytes()).matches("^[a-zA-Z0-9./]{13}$")); assertTrue(UnixCrypt.crypt("secret".getBytes(), null).matches("^[a-zA-Z0-9./]{13}$")); }
/** * 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 crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one will be used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final byte[] original) { return crypt(original, null); }
/** * Generates a crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one will be used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final byte[] original) { return crypt(original, null); }
/** * Generates a crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one will be used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final byte[] original) { return crypt(original, null); }
/** * Generates a crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one will be used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final byte[] original) { return crypt(original, null); }
/** * Generates a crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one will be used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final byte[] original) { return crypt(original, null); }
/** * Generates a crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one is used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final String original) { return crypt(original.getBytes(Charsets.UTF_8)); }
/** * Generates a crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one is used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final String original) { return crypt(original.getBytes(Charsets.UTF_8)); }
/** * Generates a crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one is used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final String original) { return crypt(original.getBytes(Charsets.UTF_8)); }
/** * Generates a crypt(3) compatible hash using the DES algorithm. * <p> * As no salt is given, a random one is used. * * @param original * plaintext password * @return a 13 character string starting with the salt string */ public static String crypt(final String original) { return crypt(original.getBytes(Charsets.UTF_8)); }
@Override public String hash(HashString hashString, String password) { // htpasswd uses the first 2 bytes as salt final String cryptSalt = hashString.hash().substring(0, 2); return UnixCrypt.crypt(password, cryptSalt); } }