Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Removed NoSuchAlgorithmException in favour of DigestUtils.getDigest.
  • Loading branch information
Christian Hammers committed Sep 1, 2012
commit a3808e7bab509cbb6cd1e6490ed1046848008e4e
28 changes: 16 additions & 12 deletions src/main/java/org/apache/commons/codec/digest/Crypt.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package org.apache.commons.codec.digest;

import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.Charsets;

/**
Expand All @@ -41,9 +39,10 @@ public class Crypt {
* @param keyBytes
* plaintext password
* @return hash value
* @throws NoSuchAlgorithmException if no algorithm implementation is available
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
public static String crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
public static String crypt(byte[] keyBytes) {
return crypt(keyBytes, null);
}

Expand All @@ -58,10 +57,12 @@ public static String crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
* @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
* @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(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
public static String crypt(byte[] keyBytes, String salt) {
if (salt == null) {
return Sha2Crypt.sha512Crypt(keyBytes);
} else if (salt.startsWith(Sha2Crypt.SHA512_PREFIX)) {
Expand All @@ -84,9 +85,10 @@ public static String crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmE
* @param key
* plaintext password
* @return hash value
* @throws NoSuchAlgorithmException if no algorithm implementation is available
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
public static String crypt(String key) throws NoSuchAlgorithmException {
public static String crypt(String key) {
return crypt(key, null);
}

Expand Down Expand Up @@ -136,10 +138,12 @@ public static String crypt(String key) throws NoSuchAlgorithmException {
* @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
* @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(String key, String salt) throws NoSuchAlgorithmException {
public static String crypt(String key, String salt) {
return crypt(key.getBytes(Charsets.UTF_8), salt);
}
}
54 changes: 30 additions & 24 deletions src/main/java/org/apache/commons/codec/digest/Md5Crypt.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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;
Expand Down Expand Up @@ -51,9 +50,6 @@ public class Md5Crypt {
/** The number of bytes of the final hash. */
private static final int BLOCKSIZE = 16;

/** The MessageDigest MD5_ALGORITHM. */
private static final String MD5_ALGORITHM = "MD5";

/** The Identifier of this crypt() variant. */
static final String MD5_PREFIX = "$1$";

Expand All @@ -63,19 +59,21 @@ public class Md5Crypt {
/**
* See {@link #apr1Crypt(String, String)} for details.
*
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught. *
*/
public static String apr1Crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
public static String apr1Crypt(byte[] keyBytes) {
return apr1Crypt(keyBytes, APR1_PREFIX + B64.getRandomSalt(8));
}

/**
* 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
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
public static String apr1Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
public static String apr1Crypt(byte[] keyBytes, String salt) {
// to make the md5Crypt regex happy
if (salt != null && !salt.startsWith(APR1_PREFIX)) {
salt = APR1_PREFIX + salt;
Expand All @@ -86,9 +84,10 @@ public static String apr1Crypt(byte[] keyBytes, String salt) throws NoSuchAlgori
/**
* See {@link #apr1Crypt(String, String)} for details.
*
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
public static String apr1Crypt(String keyBytes) throws NoSuchAlgorithmException {
public static String apr1Crypt(String keyBytes) {
return apr1Crypt(keyBytes.getBytes(Charsets.UTF_8));
}

Expand All @@ -104,10 +103,12 @@ public static String apr1Crypt(String keyBytes) throws NoSuchAlgorithmException
* 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
* @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(String keyBytes, String salt) throws NoSuchAlgorithmException {
public static String apr1Crypt(String keyBytes, String salt) {
return apr1Crypt(keyBytes.getBytes(Charsets.UTF_8), salt);
}

Expand All @@ -116,9 +117,10 @@ public static String apr1Crypt(String keyBytes, String salt) throws NoSuchAlgori
* <p>
* See {@link Crypt#crypt(String, String)} for details.
*
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
public static String md5Crypt(final byte[] keyBytes) throws NoSuchAlgorithmException {
public static String md5Crypt(final byte[] keyBytes) {
return md5Crypt(keyBytes, MD5_PREFIX + B64.getRandomSalt(8));
}

Expand All @@ -133,10 +135,12 @@ public static String md5Crypt(final byte[] keyBytes) throws NoSuchAlgorithmExcep
* 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
* @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(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
public static String md5Crypt(byte[] keyBytes, String salt) {
return md5Crypt(keyBytes, salt, MD5_PREFIX);
}

Expand All @@ -145,11 +149,13 @@ public static String md5Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorit
* <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
* @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, final String prefix)
throws NoSuchAlgorithmException {
{
int keyLen = keyBytes.length;

// Extract the real salt from the given string which can be a complete hash string.
Expand All @@ -166,7 +172,7 @@ public static String md5Crypt(final byte[] keyBytes, final String salt, final St
}
byte[] saltBytes = saltString.getBytes(Charsets.UTF_8);

MessageDigest ctx = MessageDigest.getInstance(MD5_ALGORITHM);
MessageDigest ctx = DigestUtils.getMd5Digest();

/*
* The password first, since that is what is most unknown
Expand All @@ -186,7 +192,7 @@ public static String md5Crypt(final byte[] keyBytes, final String salt, final St
/*
* Then just as many characters of the MD5(pw,salt,pw)
*/
MessageDigest ctx1 = MessageDigest.getInstance(MD5_ALGORITHM);
MessageDigest ctx1 = DigestUtils.getMd5Digest();
ctx1.update(keyBytes);
ctx1.update(saltBytes);
ctx1.update(keyBytes);
Expand Down Expand Up @@ -227,7 +233,7 @@ public static String md5Crypt(final byte[] keyBytes, final String salt, final St
* need 30 seconds to build a 1000 entry dictionary...
*/
for (int i = 0; i < ROUNDS; i++) {
ctx1 = MessageDigest.getInstance(MD5_ALGORITHM);
ctx1 = DigestUtils.getMd5Digest();
if ((i & 1) != 0) {
ctx1.update(keyBytes);
} else {
Expand Down
36 changes: 20 additions & 16 deletions src/main/java/org/apache/commons/codec/digest/Sha2Crypt.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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;
Expand Down Expand Up @@ -79,9 +78,10 @@ public class Sha2Crypt {
* <p>
* See {@link Crypt#crypt(String, String)} for details.
*
* @throws NoSuchAlgorithmException if no "SHA-256" algorithm implementation is available
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
public static String sha256Crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
public static String sha256Crypt(byte[] keyBytes) {
return sha256Crypt(keyBytes, null);
}

Expand All @@ -91,9 +91,10 @@ public static String sha256Crypt(byte[] keyBytes) throws NoSuchAlgorithmExceptio
* 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
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
public static String sha256Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
public static String sha256Crypt(byte[] keyBytes, String salt) {
if (salt == null) {
salt = SHA256_PREFIX + B64.getRandomSalt(8);
}
Expand Down Expand Up @@ -121,10 +122,11 @@ public static String sha256Crypt(byte[] keyBytes, String salt) throws NoSuchAlgo
* {@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
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix, int blocksize, String algorithm)
throws NoSuchAlgorithmException {
{

int keyLen = keyBytes.length;

Expand All @@ -150,7 +152,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,

// 1. start digest A
// Prepare for the real work.
MessageDigest ctx = MessageDigest.getInstance(algorithm);
MessageDigest ctx = DigestUtils.getDigest(algorithm);

// 2. the password string is added to digest A
/*
Expand Down Expand Up @@ -178,7 +180,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
* Compute alternate sha512 sum with input KEY, SALT, and KEY. The final result will be added to the first
* context.
*/
MessageDigest altCtx = MessageDigest.getInstance(algorithm);
MessageDigest altCtx = DigestUtils.getDigest(algorithm);

// 5. add the password to digest B
/*
Expand Down Expand Up @@ -256,7 +258,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
/*
* Start computation of P byte sequence.
*/
altCtx = MessageDigest.getInstance(algorithm);
altCtx = DigestUtils.getDigest(algorithm);

// 14. for every byte in the password (excluding the terminating NUL byte
// in the C representation of the string)
Expand Down Expand Up @@ -297,7 +299,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
/*
* Start computation of S byte sequence.
*/
altCtx = MessageDigest.getInstance(algorithm);
altCtx = DigestUtils.getDigest(algorithm);

// 18. repeast the following 16+A[0] times, where A[0] represents the first
// byte in digest A interpreted as an 8-bit unsigned value
Expand Down Expand Up @@ -351,7 +353,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
/*
* New context.
*/
ctx = MessageDigest.getInstance(algorithm);
ctx = DigestUtils.getDigest(algorithm);

// b) for odd round numbers add the byte sequense P to digest C
// c) for even round numbers add digest A/C
Expand Down Expand Up @@ -504,9 +506,10 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
* <p>
* See {@link Crypt#crypt(String, String)} for details.
*
* @throws NoSuchAlgorithmException if no "SHA-512" algorithm implementation is available
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
public static String sha512Crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
public static String sha512Crypt(byte[] keyBytes) {
return sha512Crypt(keyBytes, null);
}

Expand All @@ -516,9 +519,10 @@ public static String sha512Crypt(byte[] keyBytes) throws NoSuchAlgorithmExceptio
* 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
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
public static String sha512Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
public static String sha512Crypt(byte[] keyBytes, String salt) {
if (salt == null) {
salt = SHA512_PREFIX + B64.getRandomSalt(8);
}
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/apache/commons/codec/digest/UnixCrypt.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ public class UnixCrypt {
* @param original
* plaintext password
* @return a 13 character string starting with the salt string
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
public static String crypt(byte[] original) {
return crypt(original, null);
Expand All @@ -232,7 +234,10 @@ public static String crypt(byte[] original) {
* @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
* @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(byte[] original, String salt) {
if (salt == null) {
Expand Down Expand Up @@ -301,6 +306,8 @@ public static String crypt(byte[] original, String salt) {
* @param original
* plaintext password
* @return a 13 character string starting with the salt string
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
public static String crypt(String original) {
return crypt(original.getBytes(Charsets.UTF_8));
Expand All @@ -314,7 +321,10 @@ public static String crypt(String original) {
* @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
* @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(String original, String salt) {
return crypt(original.getBytes(Charsets.UTF_8), salt);
Expand Down
Loading