You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2012/08/17 22:42:19 UTC
svn commit: r1374430 - in /commons/proper/codec/trunk/src:
main/java/org/apache/commons/codec/digest/
test/java/org/apache/commons/codec/digest/
Author: tn
Date: Fri Aug 17 20:42:18 2012
New Revision: 1374430
URL: http://svn.apache.org/viewvc?rev=1374430&view=rev
Log:
Cleanup of throws clauses in new crypt classes. Additional javadoc fixes.
Modified:
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Crypt.java
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Md5Crypt.java
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Sha2Crypt.java
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/UnixCrypt.java
commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Apr1CryptTest.java
commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/CryptTest.java
commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Md5CryptTest.java
commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Sha256CryptTest.java
commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Sha512CryptTest.java
commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/UnixCryptTest.java
Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Crypt.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Crypt.java?rev=1374430&r1=1374429&r2=1374430&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Crypt.java (original)
+++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Crypt.java Fri Aug 17 20:42:18 2012
@@ -16,14 +16,16 @@
*/
package org.apache.commons.codec.digest;
+import java.security.NoSuchAlgorithmException;
+
import org.apache.commons.codec.Charsets;
/**
* GNU libc crypt(3) compatible hash method.
* <p>
* See {@link #crypt(String, String)} for further details.
- *
- * <p>This class is immutable and thread-safe.</p>
+ * <p>
+ * This class is immutable and thread-safe.
*
* @version $Id$
* @since 1.7
@@ -39,24 +41,27 @@ public class Crypt {
* @param keyBytes
* plaintext password
* @return hash value
+ * @throws NoSuchAlgorithmException if no algorithm implementation is available
*/
- public static String crypt(byte[] keyBytes) throws Exception {
+ public static String crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
return crypt(keyBytes, null);
}
/**
* Encrypts a password in a crypt(3) compatible way.
* <p>
- * A random salt and the default algorithm (currently SHA-512) are used. See
- * {@link #crypt(String, String)} for details.
+ * 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 NoSuchAlgorithmException if no algorithm implementation is available
*/
- public static String crypt(byte[] keyBytes, String salt) throws Exception {
+ public static String crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
if (salt == null) {
return Sha2Crypt.sha512Crypt(keyBytes);
} else if (salt.startsWith(Sha2Crypt.SHA512_PREFIX)) {
@@ -79,14 +84,14 @@ public class Crypt {
* @param key
* plaintext password
* @return hash value
+ * @throws NoSuchAlgorithmException if no algorithm implementation is available
*/
- public static String crypt(String key) throws Exception {
+ public static String crypt(String key) throws NoSuchAlgorithmException {
return crypt(key, null);
}
/**
* Encrypts a password in a crypt(3) compatible way.
- *
* <p>
* The exact algorithm depends on the format of the salt string:
* <ul>
@@ -98,16 +103,13 @@ public class Crypt {
* </ul>
* The magic strings "$apr1$" and "$2a$" are not recognised by this method as its
* output should be identical with that of the libc implementation.
- *
* <p>
* The rest of the salt string is drawn from the set [a-zA-Z0-9./] and is cut at the
* maximum length of if a "$" sign is encountered. It is therefore valid to enter a
* complete hash value as salt to e.g. verify a password with:
- *
* <pre>
* storedPwd.equals(crypt(enteredPwd, storedPwd))
* </pre>
- *
* <p>
* The resulting string starts with the marker string ($6$), continues with the salt
* value and ends with a "$" sign followed by the actual hash value. For DES the string
@@ -118,15 +120,12 @@ public class Crypt {
* <li>MD5: 34 chars
* <li>DES: 13 chars
* </ul>
- *
* <p>
* Example:
- *
* <pre>
* crypt("secret", "$1$xxxx") => "$1$xxxx$aMkevjfEIpa35Bh3G4bAc."
* crypt("secret", "xx") => "xxWAum7tHdIUw"
* </pre>
- *
* <p>
* This method comes in a variation that accepts a byte[] array to support input strings that
* are not encoded in UTF-8 but e.g. in ISO-8859-1 where equal characters result in different byte values.
@@ -137,8 +136,10 @@ public class Crypt {
* @param salt
* salt value
* @return hash value, i.e. encrypted password including the salt string
+ * @throws IllegalArgumentException if the salt does not match the allowed pattern
+ * @throws NoSuchAlgorithmException if no algorithm implementation is available
*/
- public static String crypt(String key, String salt) throws Exception {
+ public static String crypt(String key, String salt) throws NoSuchAlgorithmException {
return crypt(key.getBytes(Charsets.UTF_8), salt);
}
}
Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Md5Crypt.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Md5Crypt.java?rev=1374430&r1=1374429&r2=1374430&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Md5Crypt.java (original)
+++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Md5Crypt.java Fri Aug 17 20:42:18 2012
@@ -17,6 +17,7 @@
package org.apache.commons.codec.digest;
import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -27,54 +28,54 @@ import org.apache.commons.codec.Charsets
* The libc crypt() "$1$" and Apache "$apr1$" MD5-based hash algorithm.
* <p>
* Based on the public domain ("beer-ware") C implementation from Poul-Henning Kamp which was found at:
- * </p>
+ * <a href="http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libcrypt/crypt-md5.c?rev=1.1;content-type=text%2Fplain">
+ * crypt-md5.c @ freebsd.org</a><br/>
* <p>
- * http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libcrypt/crypt-md5.c?rev=1.1;content-type=text%2Fplain<br/>
- * Source: $FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.1 1999/01/21 13:50:09 brandon Exp $
- * </p>
- * <p>Conversion to Kotlin and from there to Java in 2012.</p>
- *
- * <p>The C style comments are from the original C code, the ones with "//" from the port.</p>
- *
- * <p>This class is immutable and thread-safe.</p>
+ * Source:
+ * <pre>$FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.1 1999/01/21 13:50:09 brandon Exp $</pre>
+ * <p>
+ * Conversion to Kotlin and from there to Java in 2012.
+ * <p>
+ * The C style comments are from the original C code, the ones with "//" from the port.
+ * <p>
+ * This class is immutable and thread-safe.
*
* @version $Id$
* @since 1.7
*/
public class Md5Crypt {
- /**
- * The Identifier of the Apache variant.
- */
+ /** The Identifier of the Apache variant. */
static final String APR1_PREFIX = "$apr1$";
- /**
- * The number of bytes of the final hash.
- */
+ /** The number of bytes of the final hash. */
private static final int BLOCKSIZE = 16;
- /**
- * The MessageDigest MD5_ALGORITHM.
- */
+ /** The MessageDigest MD5_ALGORITHM. */
private static final String MD5_ALGORITHM = "MD5";
- /**
- * The Identifier of this crypt() variant.
- */
+ /** The Identifier of this crypt() variant. */
static final String MD5_PREFIX = "$1$";
- /**
- * The number of rounds of the big loop.
- */
+ /** The number of rounds of the big loop. */
private static final int ROUNDS = 1000;
- /** See {@link #apr1Crypt(String, String)} for details. */
- public static String apr1Crypt(byte[] keyBytes) throws Exception {
+ /**
+ * See {@link #apr1Crypt(String, String)} for details.
+ *
+ * @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
+ */
+ public static String apr1Crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
return apr1Crypt(keyBytes, APR1_PREFIX + B64.getRandomSalt(8));
}
- /** See {@link #apr1Crypt(String, String)} for details. */
- public static String apr1Crypt(byte[] keyBytes, String salt) throws Exception {
+ /**
+ * See {@link #apr1Crypt(String, String)} for details.
+ *
+ * @throws IllegalArgumentException if the salt does not match the allowed pattern
+ * @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
+ */
+ public static String apr1Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
// to make the md5Crypt regex happy
if (salt != null && !salt.startsWith(APR1_PREFIX)) {
salt = APR1_PREFIX + salt;
@@ -82,8 +83,12 @@ public class Md5Crypt {
return Md5Crypt.md5Crypt(keyBytes, salt, APR1_PREFIX);
}
- /** See {@link #apr1Crypt(String, String)} for details. */
- public static String apr1Crypt(String keyBytes) throws Exception {
+ /**
+ * See {@link #apr1Crypt(String, String)} for details.
+ *
+ * @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
+ */
+ public static String apr1Crypt(String keyBytes) throws NoSuchAlgorithmException {
return apr1Crypt(keyBytes.getBytes(Charsets.UTF_8));
}
@@ -99,8 +104,10 @@ public class Md5Crypt {
* salt string including the prefix and optionally garbage at the end.
* Will be generated randomly if null.
* @return computed hash value
+ * @throws IllegalArgumentException if the salt does not match the allowed pattern
+ * @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
*/
- public static String apr1Crypt(String keyBytes, String salt) throws Exception {
+ public static String apr1Crypt(String keyBytes, String salt) throws NoSuchAlgorithmException {
return apr1Crypt(keyBytes.getBytes(Charsets.UTF_8), salt);
}
@@ -108,8 +115,10 @@ public class Md5Crypt {
* Generates a libc6 crypt() compatible "$1$" hash value.
* <p>
* See {@link Crypt#crypt(String, String)} for details.
+ *
+ * @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
*/
- public static String md5Crypt(final byte[] keyBytes) throws Exception {
+ public static String md5Crypt(final byte[] keyBytes) throws NoSuchAlgorithmException {
return md5Crypt(keyBytes, MD5_PREFIX + B64.getRandomSalt(8));
}
@@ -124,8 +133,10 @@ public class Md5Crypt {
* salt string including the prefix and optionally garbage at the end.
* Will be generated randomly if null.
* @return computed hash value
+ * @throws IllegalArgumentException if the salt does not match the allowed pattern
+ * @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
*/
- public static String md5Crypt(byte[] keyBytes, String salt) throws Exception {
+ public static String md5Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
return md5Crypt(keyBytes, salt, MD5_PREFIX);
}
@@ -133,8 +144,12 @@ public class Md5Crypt {
* Generates a libc6 crypt() "$1$" or Apache htpasswd "$apr1$" hash value.
* <p>
* See {@link Crypt#crypt(String, String)} or {@link #apr1Crypt(String, String)} for details.
+ *
+ * @throws IllegalArgumentException if the salt does not match the allowed pattern
+ * @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
*/
- public static String md5Crypt(final byte[] keyBytes, final String salt, final String prefix) throws Exception {
+ public static String md5Crypt(final byte[] keyBytes, final String salt, final String prefix)
+ throws NoSuchAlgorithmException {
int keyLen = keyBytes.length;
// Extract the real salt from the given string which can be a complete hash string.
@@ -142,8 +157,8 @@ public class Md5Crypt {
if (salt == null) {
saltString = B64.getRandomSalt(8);
} else {
- Pattern p = Pattern.compile("^" + prefix.replace("$", "\\$") + "([\\.\\/a-zA-Z0-9]{1,8}).*");
- Matcher m = p.matcher(salt);
+ final Pattern p = Pattern.compile("^" + prefix.replace("$", "\\$") + "([\\.\\/a-zA-Z0-9]{1,8}).*");
+ final Matcher m = p.matcher(salt);
if (m == null || !m.find()) {
throw new IllegalArgumentException("Invalid salt value: " + salt);
}
Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Sha2Crypt.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Sha2Crypt.java?rev=1374430&r1=1374429&r2=1374430&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Sha2Crypt.java (original)
+++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Sha2Crypt.java Fri Aug 17 20:42:18 2012
@@ -17,77 +17,71 @@
package org.apache.commons.codec.digest;
import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.apache.commons.codec.Charsets;
+
/**
* SHA2-based Unix crypt implementation.
- *
* <p>
* Based on the C implementation released into the Public Domain by Ulrich Drepper <drepper@redhat.com>
* http://www.akkadia.org/drepper/SHA-crypt.txt
- * </p>
- *
* <p>
- * Conversion to Kotlin and from there to Java in 2012 by Christian Hammers <ch@lathspell.de> and likewise put
- * into the Public Domain.
- * </p>
- *
- * <p>This class is immutable and thread-safe.</p>
+ * Conversion to Kotlin and from there to Java in 2012 by Christian Hammers <ch@lathspell.de>
+ * and likewise put into the Public Domain.
+ * <p>
+ * This class is immutable and thread-safe.
*
* @version $Id$
* @since 1.7
*/
public class Sha2Crypt {
- /**
- * Default number of rounds if not explicitly specified.
- */
+ /** Default number of rounds if not explicitly specified. */
private static final int ROUNDS_DEFAULT = 5000;
- /**
- * Maximum number of rounds.
- */
+ /** Maximum number of rounds. */
private static final int ROUNDS_MAX = 999999999;
- /**
- * Minimum number of rounds.
- */
+ /** Minimum number of rounds. */
private static final int ROUNDS_MIN = 1000;
- /**
- * Prefix for optional rounds specification.
- */
+ /** Prefix for optional rounds specification. */
private static final String ROUNDS_PREFIX = "rounds=";
- /**
- * The MessageDigest algorithm.
- */
+ /** The SHA-256 MessageDigest algorithm. */
private static final String SHA256_ALGORITHM = "SHA-256";
- /**
- * The number of bytes the final hash value will have.
- */
+ /** The number of bytes the final hash value will have (SHA-256 variant). */
private static final int SHA256_BLOCKSIZE = 32;
- /**
- * The prefixes that can be used to identify this crypt() variant.
- */
+ /** The prefixes that can be used to identify this crypt() variant (SHA-256). */
static final String SHA256_PREFIX = "$5$";
+ /** The SHA-512 MessageDigest algorithm. */
private static final String SHA512_ALGORITHM = "SHA-512";
+ /** The number of bytes the final hash value will have (SHA-512 variant). */
private static final int SHA512_BLOCKSIZE = 64;
+ /** The prefixes that can be used to identify this crypt() variant (SHA-512). */
static final String SHA512_PREFIX = "$6$";
+ /** The pattern to match valid salt values. */
+ private static final Pattern SALT_PATTERN =
+ Pattern.compile("^\\$([56])\\$(rounds=(\\d+)\\$)?([\\.\\/a-zA-Z0-9]{1,16}).*");
+
/**
* Generates a libc crypt() compatible "$5$" hash value with random salt.
* <p>
* See {@link Crypt#crypt(String, String)} for details.
+ *
+ * @throws NoSuchAlgorithmException if no "SHA-256" algorithm implementation is available
*/
- public static String sha256Crypt(byte[] keyBytes) throws Exception {
+ public static String sha256Crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
return sha256Crypt(keyBytes, null);
}
@@ -95,8 +89,11 @@ public class Sha2Crypt {
* Generates a libc6 crypt() compatible "$5$" hash value.
* <p>
* See {@link Crypt#crypt(String, String)} for details.
+ *
+ * @throws IllegalArgumentException if the salt does not match the allowed pattern
+ * @throws NoSuchAlgorithmException if no "SHA-256" algorithm implementation is available
*/
- public static String sha256Crypt(byte[] keyBytes, String salt) throws Exception {
+ public static String sha256Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
if (salt == null) {
salt = SHA256_PREFIX + B64.getRandomSalt(8);
}
@@ -114,7 +111,7 @@ public class Sha2Crypt {
*
* @param keyBytes
* plaintext that should be hashed
- * @param salt_string
+ * @param salt
* real salt value without prefix or "rounds="
* @param saltPrefix
* either $5$ or $6$
@@ -123,19 +120,22 @@ public class Sha2Crypt {
* @param algorithm
* {@link MessageDigest} algorithm identifier string
* @return complete hash value including prefix and salt
+ * @throws IllegalArgumentException if the given salt is {@code null} or does not match the allowed pattern
+ * @throws NoSuchAlgorithmException if no implementation for the given algorithm is available
*/
private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix, int blocksize, String algorithm)
- throws Exception {
+ throws NoSuchAlgorithmException {
+
int keyLen = keyBytes.length;
// Extracts effective salt and the number of rounds from the given salt.
int rounds = ROUNDS_DEFAULT;
boolean roundsCustom = false;
if (salt == null) {
- throw new IllegalArgumentException("Invalid salt value: null");
+ throw new IllegalArgumentException("Salt must not be null");
}
- Pattern p = Pattern.compile("^\\$([56])\\$(rounds=(\\d+)\\$)?([\\.\\/a-zA-Z0-9]{1,16}).*");
- Matcher m = p.matcher(salt);
+
+ Matcher m = SALT_PATTERN.matcher(salt);
if (m == null || !m.find()) {
throw new IllegalArgumentException("Invalid salt value: " + salt);
}
@@ -145,7 +145,7 @@ public class Sha2Crypt {
roundsCustom = true;
}
String saltString = m.group(4);
- byte[] saltBytes = saltString.getBytes("UTF-8");
+ byte[] saltBytes = saltString.getBytes(Charsets.UTF_8);
int saltLen = saltBytes.length;
// 1. start digest A
@@ -342,7 +342,7 @@ public class Sha2Crypt {
// The loop uses a digest as input. In the first round it is the
// digest produced in step 12. In the latter steps it is the digest
// produced in step 21.h. The following text uses the notation
- // "digest A/C" to desribe this behavior.
+ // "digest A/C" to describe this behavior.
/*
* Repeatedly run the collected hash value through sha512 to burn CPU cycles.
*/
@@ -413,8 +413,14 @@ public class Sha2Crypt {
/*
* Now we can construct the result string. It consists of three parts.
*/
- StringBuilder buffer = new StringBuilder(saltPrefix +
- (roundsCustom ? ROUNDS_PREFIX + rounds + "$" : "") + saltString + "$");
+ StringBuilder buffer = new StringBuilder(saltPrefix);
+ if (roundsCustom) {
+ buffer.append(ROUNDS_PREFIX);
+ buffer.append(rounds);
+ buffer.append("$");
+ }
+ buffer.append(saltString);
+ buffer.append("$");
// e) the base-64 encoded final C digest. The encoding used is as
// follows:
@@ -497,8 +503,10 @@ public class Sha2Crypt {
* Generates a libc crypt() compatible "$6$" hash value with random salt.
* <p>
* See {@link Crypt#crypt(String, String)} for details.
+ *
+ * @throws NoSuchAlgorithmException if no "SHA-512" algorithm implementation is available
*/
- public static String sha512Crypt(byte[] keyBytes) throws Exception {
+ public static String sha512Crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
return sha512Crypt(keyBytes, null);
}
@@ -506,8 +514,11 @@ public class Sha2Crypt {
* Generates a libc6 crypt() compatible "$6$" hash value.
* <p>
* See {@link Crypt#crypt(String, String)} for details.
+ *
+ * @throws IllegalArgumentException if the salt does not match the allowed pattern
+ * @throws NoSuchAlgorithmException if no "SHA-512" algorithm implementation is available
*/
- public static String sha512Crypt(byte[] keyBytes, String salt) throws Exception {
+ public static String sha512Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
if (salt == null) {
salt = SHA512_PREFIX + B64.getRandomSalt(8);
}
Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/UnixCrypt.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/UnixCrypt.java?rev=1374430&r1=1374429&r2=1374430&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/UnixCrypt.java (original)
+++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/UnixCrypt.java Fri Aug 17 20:42:18 2012
@@ -22,18 +22,18 @@ import org.apache.commons.codec.Charsets
/**
* Unix crypt(3) algorithm implementation.
- *
- * <p>This class only implements the traditional 56 bit DES based algorithm. Please
+ * <p>
+ * This class only implements the traditional 56 bit DES based algorithm. Please
* use DigestUtils.crypt() for a method that distinguishes between all the
- * algorithms supported in the current glibc's crypt().</p>
- *
- * <p>The Java implementation was taken from the JetSpeed Portal project (see
- * org.apache.jetspeed.services.security.ldap.UnixCrypt).</p>
- *
- * <p>This class is slightly incompatible if the given salt contains characters
- * that are not part of the allowed range [a-zA-Z0-9./].</p>
- *
- * <p>This class is immutable and thread-safe.</p>
+ * algorithms supported in the current glibc's crypt().
+ * <p>
+ * The Java implementation was taken from the JetSpeed Portal project (see
+ * org.apache.jetspeed.services.security.ldap.UnixCrypt).
+ * <p>
+ * This class is slightly incompatible if the given salt contains characters
+ * that are not part of the allowed range [a-zA-Z0-9./].
+ * <p>
+ * This class is immutable and thread-safe.
*
* @version $Id$
* @since 1.7
@@ -209,6 +209,117 @@ public class UnixCrypt {
}
};
+ /**
+ * 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(byte[] original) {
+ return crypt(original, null);
+ }
+
+ /**
+ * Generates a crypt(3) compatible hash using the DES algorithm.
+ * <p>
+ * Using unspecified characters as salt results incompatible hash values.
+ *
+ * @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(byte[] original, String salt) {
+ if (salt == null) {
+ Random randomGenerator = new Random();
+ int numSaltChars = SALT_CHARS.length;
+ salt = "" + SALT_CHARS[Math.abs(randomGenerator.nextInt()) % numSaltChars] +
+ SALT_CHARS[Math.abs(randomGenerator.nextInt()) % numSaltChars];
+ } else if (!salt.matches("^[" + B64.B64T + "]{2,}$")) {
+ throw new IllegalArgumentException("Invalid salt value: " + salt);
+ }
+
+ for (; salt.length() < 2; salt = salt + "A") {
+ // NOOP
+ }
+
+ StringBuilder buffer = new StringBuilder(" ");
+ char charZero = salt.charAt(0);
+ char charOne = salt.charAt(1);
+ buffer.setCharAt(0, charZero);
+ buffer.setCharAt(1, charOne);
+ int eSwap0 = CON_SALT[charZero];
+ int eSwap1 = CON_SALT[charOne] << 4;
+ byte key[] = new byte[8];
+ for (int i = 0; i < key.length; i++) {
+ key[i] = 0;
+ }
+
+ for (int i = 0; i < key.length && i < original.length; i++) {
+ int iChar = original[i];
+ key[i] = (byte) (iChar << 1);
+ }
+
+ int schedule[] = desSetKey(key);
+ int out[] = body(schedule, eSwap0, eSwap1);
+ byte b[] = new byte[9];
+ intToFourBytes(out[0], b, 0);
+ intToFourBytes(out[1], b, 4);
+ b[8] = 0;
+ int i = 2;
+ int y = 0;
+ int u = 128;
+ for (; i < 13; i++) {
+ int j = 0;
+ int c = 0;
+ for (; j < 6; j++) {
+ c <<= 1;
+ if ((b[y] & u) != 0) {
+ c |= 0x1;
+ }
+ u >>>= 1;
+ if (u == 0) {
+ y++;
+ u = 128;
+ }
+ buffer.setCharAt(i, (char) COV2CHAR[c]);
+ }
+ }
+ return buffer.toString();
+ }
+
+ /**
+ * 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(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(String original, String salt) {
+ return crypt(original.getBytes(Charsets.UTF_8), salt);
+ }
+
private static int[] body(int schedule[], int eSwap0, int eSwap1) {
int left = 0;
int right = 0;
@@ -340,111 +451,4 @@ public class UnixCrypt {
results[1] = b;
}
- /**
- * 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(byte[] original) {
- return crypt(original, null);
- }
-
- /**
- * Generates a crypt(3) compatible hash using the DES algorithm.
- * <p>
- * Using unspecified characters as salt results incompatible hash values.
- *
- * @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
- */
- public static String crypt(byte[] original, String salt) {
- if (salt == null) {
- Random randomGenerator = new Random();
- int numSaltChars = SALT_CHARS.length;
- salt = "" + SALT_CHARS[Math.abs(randomGenerator.nextInt()) % numSaltChars] + SALT_CHARS[Math.abs(randomGenerator.nextInt()) % numSaltChars];
- } else if (!salt.matches("^[" + B64.B64T + "]{2,}$")) {
- throw new IllegalArgumentException("Invalid salt value: " + salt);
- }
-
- for (; salt.length() < 2; salt = salt + "A") {
- // NOOP
- }
- StringBuilder buffer = new StringBuilder(" ");
- char charZero = salt.charAt(0);
- char charOne = salt.charAt(1);
- buffer.setCharAt(0, charZero);
- buffer.setCharAt(1, charOne);
- int eSwap0 = CON_SALT[charZero];
- int eSwap1 = CON_SALT[charOne] << 4;
- byte key[] = new byte[8];
- for (int i = 0; i < key.length; i++) {
- key[i] = 0;
- }
-
- for (int i = 0; i < key.length && i < original.length; i++) {
- int iChar = original[i];
- key[i] = (byte) (iChar << 1);
- }
-
- int schedule[] = desSetKey(key);
- int out[] = body(schedule, eSwap0, eSwap1);
- byte b[] = new byte[9];
- intToFourBytes(out[0], b, 0);
- intToFourBytes(out[1], b, 4);
- b[8] = 0;
- int i = 2;
- int y = 0;
- int u = 128;
- for (; i < 13; i++) {
- int j = 0;
- int c = 0;
- for (; j < 6; j++) {
- c <<= 1;
- if ((b[y] & u) != 0) {
- c |= 0x1;
- }
- u >>>= 1;
- if (u == 0) {
- y++;
- u = 128;
- }
- buffer.setCharAt(i, (char) COV2CHAR[c]);
- }
- }
- return buffer.toString();
- }
-
- /**
- * 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(String original) throws Exception {
- 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
- */
- public static String crypt(String original, String salt) throws Exception {
- return crypt(original.getBytes(Charsets.UTF_8), salt);
- }
-
}
Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Apr1CryptTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Apr1CryptTest.java?rev=1374430&r1=1374429&r2=1374430&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Apr1CryptTest.java (original)
+++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Apr1CryptTest.java Fri Aug 17 20:42:18 2012
@@ -18,12 +18,16 @@ package org.apache.commons.codec.digest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+
+import java.security.NoSuchAlgorithmException;
+
+import org.apache.commons.codec.Charsets;
import org.junit.Test;
public class Apr1CryptTest {
@Test
- public void testApr1CryptStrings() throws Exception {
+ public void testApr1CryptStrings() throws NoSuchAlgorithmException {
// A random example using htpasswd
assertEquals("$apr1$TqI9WECO$LHZB2DqRlk9nObiB6vJG9.", Md5Crypt.apr1Crypt("secret", "$apr1$TqI9WECO"));
// empty data
@@ -38,17 +42,17 @@ public class Apr1CryptTest {
}
@Test
- public void testApr1CryptBytes() throws Exception {
+ public void testApr1CryptBytes() throws NoSuchAlgorithmException {
// An empty Bytearray equals an empty String
assertEquals("$apr1$foo$P27KyD1htb4EllIPEYhqi0", Md5Crypt.apr1Crypt(new byte[0], "$apr1$foo"));
// UTF-8 stores \u00e4 "a with diaeresis" as two bytes 0xc3 0xa4.
assertEquals("$apr1$./$EeFrYzWWbmTyGdf4xULYc.", Md5Crypt.apr1Crypt("t\u00e4st", "$apr1$./$"));
// ISO-8859-1 stores "a with diaeresis" as single byte 0xe4.
- assertEquals("$apr1$./$kCwT1pY9qXAJElYG9q1QE1", Md5Crypt.apr1Crypt("t\u00e4st".getBytes("ISO-8859-1"), "$apr1$./$"));
+ assertEquals("$apr1$./$kCwT1pY9qXAJElYG9q1QE1", Md5Crypt.apr1Crypt("t\u00e4st".getBytes(Charsets.ISO_8859_1), "$apr1$./$"));
}
@Test
- public void testApr1CryptExplicitCall() throws Exception {
+ public void testApr1CryptExplicitCall() throws NoSuchAlgorithmException {
// When explicitly called the prefix is optional
assertEquals("$apr1$1234$mAlH7FRST6FiRZ.kcYL.j1", Md5Crypt.apr1Crypt("secret", "1234"));
// When explicitly called without salt, a random one will be used.
@@ -57,12 +61,12 @@ public class Apr1CryptTest {
}
@Test(expected = NullPointerException.class)
- public void testApr1CryptNullData() throws Exception {
+ public void testApr1CryptNullData() throws NoSuchAlgorithmException {
Md5Crypt.apr1Crypt((byte[]) null);
}
@Test(expected = IllegalArgumentException.class)
- public void testApr1CryptWithEmptySalt() throws Exception {
+ public void testApr1CryptWithEmptySalt() throws NoSuchAlgorithmException {
Md5Crypt.apr1Crypt("secret".getBytes(), "");
}
}
Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/CryptTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/CryptTest.java?rev=1374430&r1=1374429&r2=1374430&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/CryptTest.java (original)
+++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/CryptTest.java Fri Aug 17 20:42:18 2012
@@ -17,12 +17,15 @@
package org.apache.commons.codec.digest;
import static org.junit.Assert.assertTrue;
+
+import java.security.NoSuchAlgorithmException;
+
import org.junit.Test;
public class CryptTest {
@Test
- public void testDefaultCryptVariant() throws Exception {
+ public void testDefaultCryptVariant() throws NoSuchAlgorithmException {
// If salt is null or completely omitted, a random "$6$" is used.
assertTrue(Crypt.crypt("secret").startsWith("$6$"));
assertTrue(Crypt.crypt("secret", null).startsWith("$6$"));
@@ -36,7 +39,7 @@ public class CryptTest {
* hash would not be verifyable with other implementations of crypt().
*/
@Test(expected = IllegalArgumentException.class)
- public void testCryptWithEmptySalt() throws Exception {
+ public void testCryptWithEmptySalt() throws NoSuchAlgorithmException {
Crypt.crypt("secret", "");
}
Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Md5CryptTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Md5CryptTest.java?rev=1374430&r1=1374429&r2=1374430&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Md5CryptTest.java (original)
+++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Md5CryptTest.java Fri Aug 17 20:42:18 2012
@@ -18,12 +18,16 @@ package org.apache.commons.codec.digest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+
+import java.security.NoSuchAlgorithmException;
+
+import org.apache.commons.codec.Charsets;
import org.junit.Test;
public class Md5CryptTest {
@Test
- public void testMd5CryptStrings() throws Exception {
+ public void testMd5CryptStrings() throws NoSuchAlgorithmException {
// empty data
assertEquals("$1$foo$9mS5ExwgIECGE5YKlD5o91", Crypt.crypt("", "$1$foo"));
// salt gets cut at dollar sign
@@ -36,28 +40,28 @@ public class Md5CryptTest {
}
@Test
- public void testMd5CryptBytes() throws Exception {
+ public void testMd5CryptBytes() throws NoSuchAlgorithmException {
// An empty Bytearray equals an empty String
assertEquals("$1$foo$9mS5ExwgIECGE5YKlD5o91", Crypt.crypt(new byte[0], "$1$foo"));
// UTF-8 stores \u00e4 "a with diaeresis" as two bytes 0xc3 0xa4.
assertEquals("$1$./$52agTEQZs877L9jyJnCNZ1", Crypt.crypt("t\u00e4st", "$1$./$"));
// ISO-8859-1 stores "a with diaeresis" as single byte 0xe4.
- assertEquals("$1$./$J2UbKzGe0Cpe63WZAt6p//", Crypt.crypt("t\u00e4st".getBytes("ISO-8859-1"), "$1$./$"));
+ assertEquals("$1$./$J2UbKzGe0Cpe63WZAt6p//", Crypt.crypt("t\u00e4st".getBytes(Charsets.ISO_8859_1), "$1$./$"));
}
@Test
- public void testMd5CryptExplicitCall() throws Exception {
+ public void testMd5CryptExplicitCall() throws NoSuchAlgorithmException {
assertTrue(Md5Crypt.md5Crypt("secret".getBytes()).matches("^\\$1\\$[a-zA-Z0-9./]{0,8}\\$.{1,}$"));
assertTrue(Md5Crypt.md5Crypt("secret".getBytes(), null).matches("^\\$1\\$[a-zA-Z0-9./]{0,8}\\$.{1,}$"));
}
@Test(expected = NullPointerException.class)
- public void testMd5CryptNullData() throws Exception {
+ public void testMd5CryptNullData() throws NoSuchAlgorithmException {
Md5Crypt.md5Crypt((byte[]) null);
}
@Test(expected = IllegalArgumentException.class)
- public void testMd5CryptWithEmptySalt() throws Exception {
+ public void testMd5CryptWithEmptySalt() throws NoSuchAlgorithmException {
Md5Crypt.md5Crypt("secret".getBytes(), "");
}
}
Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Sha256CryptTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Sha256CryptTest.java?rev=1374430&r1=1374429&r2=1374430&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Sha256CryptTest.java (original)
+++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Sha256CryptTest.java Fri Aug 17 20:42:18 2012
@@ -18,12 +18,16 @@ package org.apache.commons.codec.digest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+
+import java.security.NoSuchAlgorithmException;
+
+import org.apache.commons.codec.Charsets;
import org.junit.Test;
public class Sha256CryptTest {
@Test
- public void testSha256CryptStrings() throws Exception {
+ public void testSha256CryptStrings() throws NoSuchAlgorithmException {
// empty data
assertEquals("$5$foo$Fq9CX624QIfnCAmlGiPKLlAasdacKCRxZztPoeo7o0B", Crypt.crypt("", "$5$foo"));
// salt gets cut at dollar sign
@@ -36,28 +40,28 @@ public class Sha256CryptTest {
}
@Test
- public void testSha256CryptBytes() throws Exception {
+ public void testSha256CryptBytes() throws NoSuchAlgorithmException {
// An empty Bytearray equals an empty String
assertEquals("$5$foo$Fq9CX624QIfnCAmlGiPKLlAasdacKCRxZztPoeo7o0B", Crypt.crypt(new byte[0], "$5$foo"));
// UTF-8 stores \u00e4 "a with diaeresis" as two bytes 0xc3 0xa4.
assertEquals("$5$./$iH66LwY5sTDTdHeOxq5nvNDVAxuoCcyH/y6Ptte82P8", Crypt.crypt("t\u00e4st", "$5$./$"));
// ISO-8859-1 stores "a with diaeresis" as single byte 0xe4.
- assertEquals("$5$./$qx5gFfCzjuWUOvsDDy.5Nor3UULPIqLVBZhgGNS0c14", Crypt.crypt("t\u00e4st".getBytes("ISO-8859-1"), "$5$./$"));
+ assertEquals("$5$./$qx5gFfCzjuWUOvsDDy.5Nor3UULPIqLVBZhgGNS0c14", Crypt.crypt("t\u00e4st".getBytes(Charsets.ISO_8859_1), "$5$./$"));
}
@Test
- public void testSha256CryptExplicitCall() throws Exception {
+ public void testSha256CryptExplicitCall() throws NoSuchAlgorithmException {
assertTrue(Sha2Crypt.sha256Crypt("secret".getBytes()).matches("^\\$5\\$[a-zA-Z0-9./]{0,16}\\$.{1,}$"));
assertTrue(Sha2Crypt.sha256Crypt("secret".getBytes(), null).matches("^\\$5\\$[a-zA-Z0-9./]{0,16}\\$.{1,}$"));
}
@Test(expected = NullPointerException.class)
- public void testSha256CryptNullData() throws Exception {
+ public void testSha256CryptNullData() throws NoSuchAlgorithmException {
Sha2Crypt.sha256Crypt((byte[]) null);
}
@Test(expected = IllegalArgumentException.class)
- public void testSha256CryptWithEmptySalt() throws Exception {
+ public void testSha256CryptWithEmptySalt() throws NoSuchAlgorithmException {
Sha2Crypt.sha256Crypt("secret".getBytes(), "");
}
}
Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Sha512CryptTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Sha512CryptTest.java?rev=1374430&r1=1374429&r2=1374430&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Sha512CryptTest.java (original)
+++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/Sha512CryptTest.java Fri Aug 17 20:42:18 2012
@@ -18,12 +18,16 @@ package org.apache.commons.codec.digest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+
+import java.security.NoSuchAlgorithmException;
+
+import org.apache.commons.codec.Charsets;
import org.junit.Test;
public class Sha512CryptTest {
@Test
- public void testSha512CryptStrings() throws Exception {
+ public void testSha512CryptStrings() throws NoSuchAlgorithmException {
// empty data
assertEquals("$6$foo$Nywkte7LPWjaJhWjNeGJN.dFdY3pN1wYlGifyRLYOVlGS9EMSiZaDDe/BGSOYQ327q9.32I4UqQ5odsqvsBLX/", Crypt.crypt("", "$6$foo"));
// salt gets cut at dollar sign
@@ -36,28 +40,28 @@ public class Sha512CryptTest {
}
@Test
- public void testSha512CryptBytes() throws Exception {
+ public void testSha512CryptBytes() throws NoSuchAlgorithmException {
// An empty Bytearray equals an empty String
assertEquals("$6$foo$Nywkte7LPWjaJhWjNeGJN.dFdY3pN1wYlGifyRLYOVlGS9EMSiZaDDe/BGSOYQ327q9.32I4UqQ5odsqvsBLX/", Crypt.crypt(new byte[0], "$6$foo"));
// UTF-8 stores \u00e4 "a with diaeresis" as two bytes 0xc3 0xa4.
assertEquals("$6$./$fKtWqslQkwI8ZxjdWoeS.jHHrte97bZxiwB5gwCRHX6LG62fUhT6Bb5MRrjWvieh0C/gxh8ItFuTsVy80VrED1", Crypt.crypt("t\u00e4st", "$6$./$"));
// ISO-8859-1 stores "a with diaeresis" as single byte 0xe4.
- assertEquals("$6$./$L49DSK.d2df/LxGLJQMyS5A/Um.TdHqgc46j5FpScEPlqQHP5dEazltaDNDZ6UEs2mmNI6kPwtH/rsP9g5zBI.", Crypt.crypt("t\u00e4st".getBytes("ISO-8859-1"), "$6$./$"));
+ assertEquals("$6$./$L49DSK.d2df/LxGLJQMyS5A/Um.TdHqgc46j5FpScEPlqQHP5dEazltaDNDZ6UEs2mmNI6kPwtH/rsP9g5zBI.", Crypt.crypt("t\u00e4st".getBytes(Charsets.ISO_8859_1), "$6$./$"));
}
@Test
- public void testSha512CryptExplicitCall() throws Exception {
+ public void testSha512CryptExplicitCall() throws NoSuchAlgorithmException {
assertTrue(Sha2Crypt.sha512Crypt("secret".getBytes()).matches("^\\$6\\$[a-zA-Z0-9./]{0,16}\\$.{1,}$"));
assertTrue(Sha2Crypt.sha512Crypt("secret".getBytes(), null).matches("^\\$6\\$[a-zA-Z0-9./]{0,16}\\$.{1,}$"));
}
@Test(expected = NullPointerException.class)
- public void testSha512CryptNullData() throws Exception {
+ public void testSha512CryptNullData() throws NoSuchAlgorithmException {
Sha2Crypt.sha512Crypt((byte[]) null);
}
@Test(expected = IllegalArgumentException.class)
- public void testSha512CryptWithEmptySalt() throws Exception {
+ public void testSha512CryptWithEmptySalt() throws NoSuchAlgorithmException {
Sha2Crypt.sha512Crypt("secret".getBytes(), "");
}
}
Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/UnixCryptTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/UnixCryptTest.java?rev=1374430&r1=1374429&r2=1374430&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/UnixCryptTest.java (original)
+++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/UnixCryptTest.java Fri Aug 17 20:42:18 2012
@@ -18,12 +18,16 @@ package org.apache.commons.codec.digest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+
+import java.security.NoSuchAlgorithmException;
+
+import org.apache.commons.codec.Charsets;
import org.junit.Test;
public class UnixCryptTest {
@Test
- public void testUnixCryptStrings() throws Exception {
+ public void testUnixCryptStrings() throws NoSuchAlgorithmException {
// trivial test
assertEquals("xxWAum7tHdIUw", Crypt.crypt("secret", "xx"));
// empty data
@@ -34,13 +38,13 @@ public class UnixCryptTest {
}
@Test
- public void testUnixCryptBytes() throws Exception {
+ public void testUnixCryptBytes() throws NoSuchAlgorithmException {
// An empty Bytearray equals an empty String
assertEquals("12UFlHxel6uMM", Crypt.crypt(new byte[0], "12"));
// UTF-8 stores \u00e4 "a with diaeresis" as two bytes 0xc3 0xa4.
assertEquals("./287bds2PjVw", Crypt.crypt("t\u00e4st", "./"));
// ISO-8859-1 stores "a with diaeresis" as single byte 0xe4.
- assertEquals("./bLIFNqo9XKQ", Crypt.crypt("t\u00e4st".getBytes("ISO-8859-1"), "./"));
+ assertEquals("./bLIFNqo9XKQ", Crypt.crypt("t\u00e4st".getBytes(Charsets.ISO_8859_1), "./"));
assertEquals("./bLIFNqo9XKQ", Crypt.crypt(new byte[]{(byte) 0x74, (byte) 0xe4, (byte) 0x73, (byte) 0x74}, "./"));
}
@@ -59,7 +63,7 @@ public class UnixCryptTest {
* Unimplemented "$foo$" salt prefixes would be threated as UnixCrypt salt.
*/
@Test(expected = IllegalArgumentException.class)
- public void testUnicCryptInvalidSalt() throws Exception {
+ public void testUnicCryptInvalidSalt() {
UnixCrypt.crypt("secret", "$a");
}
@@ -69,7 +73,7 @@ public class UnixCryptTest {
}
@Test(expected = IllegalArgumentException.class)
- public void testUnixCryptWithEmptySalt() throws Exception {
+ public void testUnixCryptWithEmptySalt() {
UnixCrypt.crypt("secret", "");
}
}