Skip to content

Commit fc393f7

Browse files
committed
Partial patch application of [CODEC-148] More tests and minor things.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1379782 13f79535-47bb-0310-9956-ffa450edef68
1 parent 28e7813 commit fc393f7

13 files changed

Lines changed: 249 additions & 114 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.apache.commons.codec.digest;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
import org.junit.Test;
5+
6+
public class Sha2CryptTest {
7+
8+
@Test
9+
public void testCtor() {
10+
assertNotNull(new Sha2Crypt());
11+
}
12+
13+
}

src/main/java/org/apache/commons/codec/digest/Crypt.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
package org.apache.commons.codec.digest;
1818

19-
import java.security.NoSuchAlgorithmException;
20-
2119
import org.apache.commons.codec.Charsets;
2220

2321
/**
@@ -41,9 +39,10 @@ public class Crypt {
4139
* @param keyBytes
4240
* plaintext password
4341
* @return hash value
44-
* @throws NoSuchAlgorithmException if no algorithm implementation is available
42+
* @throws RuntimeException
43+
* when a {@link java.security.NoSuchAlgorithmException} is caught.
4544
*/
46-
public static String crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
45+
public static String crypt(byte[] keyBytes) {
4746
return crypt(keyBytes, null);
4847
}
4948

@@ -58,10 +57,12 @@ public static String crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
5857
* @param salt
5958
* salt value
6059
* @return hash value
61-
* @throws IllegalArgumentException if the salt does not match the allowed pattern
62-
* @throws NoSuchAlgorithmException if no algorithm implementation is available
60+
* @throws IllegalArgumentException
61+
* if the salt does not match the allowed pattern
62+
* @throws RuntimeException
63+
* when a {@link java.security.NoSuchAlgorithmException} is caught.
6364
*/
64-
public static String crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
65+
public static String crypt(byte[] keyBytes, String salt) {
6566
if (salt == null) {
6667
return Sha2Crypt.sha512Crypt(keyBytes);
6768
} else if (salt.startsWith(Sha2Crypt.SHA512_PREFIX)) {
@@ -84,9 +85,10 @@ public static String crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmE
8485
* @param key
8586
* plaintext password
8687
* @return hash value
87-
* @throws NoSuchAlgorithmException if no algorithm implementation is available
88+
* @throws RuntimeException
89+
* when a {@link java.security.NoSuchAlgorithmException} is caught.
8890
*/
89-
public static String crypt(String key) throws NoSuchAlgorithmException {
91+
public static String crypt(String key) {
9092
return crypt(key, null);
9193
}
9294

@@ -136,10 +138,12 @@ public static String crypt(String key) throws NoSuchAlgorithmException {
136138
* @param salt
137139
* salt value
138140
* @return hash value, i.e. encrypted password including the salt string
139-
* @throws IllegalArgumentException if the salt does not match the allowed pattern
140-
* @throws NoSuchAlgorithmException if no algorithm implementation is available
141+
* @throws IllegalArgumentException
142+
* if the salt does not match the allowed pattern
143+
* @throws RuntimeException
144+
* when a {@link java.security.NoSuchAlgorithmException} is caught. *
141145
*/
142-
public static String crypt(String key, String salt) throws NoSuchAlgorithmException {
146+
public static String crypt(String key, String salt) {
143147
return crypt(key.getBytes(Charsets.UTF_8), salt);
144148
}
145149
}

src/main/java/org/apache/commons/codec/digest/Md5Crypt.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.apache.commons.codec.digest;
1818

1919
import java.security.MessageDigest;
20-
import java.security.NoSuchAlgorithmException;
2120
import java.util.Arrays;
2221
import java.util.regex.Matcher;
2322
import java.util.regex.Pattern;
@@ -51,9 +50,6 @@ public class Md5Crypt {
5150
/** The number of bytes of the final hash. */
5251
private static final int BLOCKSIZE = 16;
5352

54-
/** The MessageDigest MD5_ALGORITHM. */
55-
private static final String MD5_ALGORITHM = "MD5";
56-
5753
/** The Identifier of this crypt() variant. */
5854
static final String MD5_PREFIX = "$1$";
5955

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

7269
/**
7370
* See {@link #apr1Crypt(String, String)} for details.
7471
*
7572
* @throws IllegalArgumentException if the salt does not match the allowed pattern
76-
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
73+
* @throws RuntimeException
74+
* when a {@link java.security.NoSuchAlgorithmException} is caught.
7775
*/
78-
public static String apr1Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
76+
public static String apr1Crypt(byte[] keyBytes, String salt) {
7977
// to make the md5Crypt regex happy
8078
if (salt != null && !salt.startsWith(APR1_PREFIX)) {
8179
salt = APR1_PREFIX + salt;
@@ -86,9 +84,10 @@ public static String apr1Crypt(byte[] keyBytes, String salt) throws NoSuchAlgori
8684
/**
8785
* See {@link #apr1Crypt(String, String)} for details.
8886
*
89-
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
87+
* @throws RuntimeException
88+
* when a {@link java.security.NoSuchAlgorithmException} is caught.
9089
*/
91-
public static String apr1Crypt(String keyBytes) throws NoSuchAlgorithmException {
90+
public static String apr1Crypt(String keyBytes) {
9291
return apr1Crypt(keyBytes.getBytes(Charsets.UTF_8));
9392
}
9493

@@ -104,10 +103,12 @@ public static String apr1Crypt(String keyBytes) throws NoSuchAlgorithmException
104103
* salt string including the prefix and optionally garbage at the end.
105104
* Will be generated randomly if null.
106105
* @return computed hash value
107-
* @throws IllegalArgumentException if the salt does not match the allowed pattern
108-
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
106+
* @throws IllegalArgumentException
107+
* if the salt does not match the allowed pattern
108+
* @throws RuntimeException
109+
* when a {@link java.security.NoSuchAlgorithmException} is caught.
109110
*/
110-
public static String apr1Crypt(String keyBytes, String salt) throws NoSuchAlgorithmException {
111+
public static String apr1Crypt(String keyBytes, String salt) {
111112
return apr1Crypt(keyBytes.getBytes(Charsets.UTF_8), salt);
112113
}
113114

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

@@ -133,10 +135,12 @@ public static String md5Crypt(final byte[] keyBytes) throws NoSuchAlgorithmExcep
133135
* salt string including the prefix and optionally garbage at the end.
134136
* Will be generated randomly if null.
135137
* @return computed hash value
136-
* @throws IllegalArgumentException if the salt does not match the allowed pattern
137-
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
138+
* @throws IllegalArgumentException
139+
* if the salt does not match the allowed pattern
140+
* @throws RuntimeException
141+
* when a {@link java.security.NoSuchAlgorithmException} is caught.
138142
*/
139-
public static String md5Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
143+
public static String md5Crypt(byte[] keyBytes, String salt) {
140144
return md5Crypt(keyBytes, salt, MD5_PREFIX);
141145
}
142146

@@ -145,11 +149,13 @@ public static String md5Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorit
145149
* <p>
146150
* See {@link Crypt#crypt(String, String)} or {@link #apr1Crypt(String, String)} for details.
147151
*
148-
* @throws IllegalArgumentException if the salt does not match the allowed pattern
149-
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
152+
* @throws IllegalArgumentException
153+
* if the salt does not match the allowed pattern
154+
* @throws RuntimeException
155+
* when a {@link java.security.NoSuchAlgorithmException} is caught.
150156
*/
151157
public static String md5Crypt(final byte[] keyBytes, final String salt, final String prefix)
152-
throws NoSuchAlgorithmException {
158+
{
153159
int keyLen = keyBytes.length;
154160

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

169-
MessageDigest ctx = MessageDigest.getInstance(MD5_ALGORITHM);
175+
MessageDigest ctx = DigestUtils.getMd5Digest();
170176

171177
/*
172178
* The password first, since that is what is most unknown
@@ -186,7 +192,7 @@ public static String md5Crypt(final byte[] keyBytes, final String salt, final St
186192
/*
187193
* Then just as many characters of the MD5(pw,salt,pw)
188194
*/
189-
MessageDigest ctx1 = MessageDigest.getInstance(MD5_ALGORITHM);
195+
MessageDigest ctx1 = DigestUtils.getMd5Digest();
190196
ctx1.update(keyBytes);
191197
ctx1.update(saltBytes);
192198
ctx1.update(keyBytes);
@@ -227,7 +233,7 @@ public static String md5Crypt(final byte[] keyBytes, final String salt, final St
227233
* need 30 seconds to build a 1000 entry dictionary...
228234
*/
229235
for (int i = 0; i < ROUNDS; i++) {
230-
ctx1 = MessageDigest.getInstance(MD5_ALGORITHM);
236+
ctx1 = DigestUtils.getMd5Digest();
231237
if ((i & 1) != 0) {
232238
ctx1.update(keyBytes);
233239
} else {

src/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@
2020
import java.security.MessageDigest;
2121

2222
/**
23-
* Standard {@link MessageDigest} algorithm names from the <cite>Java Cryptography Architecture Standard Algorithm
24-
* Name Documentation</cite>.
25-
*
23+
* Standard {@link MessageDigest} algorithm names from the <cite>Java Cryptography Architecture Standard Algorithm Name
24+
* Documentation</cite>.
25+
*
2626
* @see <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html">Java Cryptography
2727
* Architecture Standard Algorithm Name Documentation</a>
2828
* @since 1.7
2929
* @version $Id$
3030
*/
3131
public class MessageDigestAlgorithms {
3232

33+
private MessageDigestAlgorithms() {
34+
// cannot be instantiated.
35+
}
36+
3337
/**
3438
* The MD2 message digest algorithm defined in RFC 1319.
3539
*/

src/main/java/org/apache/commons/codec/digest/Sha2Crypt.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.apache.commons.codec.digest;
1818

1919
import java.security.MessageDigest;
20-
import java.security.NoSuchAlgorithmException;
2120
import java.util.Arrays;
2221
import java.util.regex.Matcher;
2322
import java.util.regex.Pattern;
@@ -52,18 +51,12 @@ public class Sha2Crypt {
5251
/** Prefix for optional rounds specification. */
5352
private static final String ROUNDS_PREFIX = "rounds=";
5453

55-
/** The SHA-256 MessageDigest algorithm. */
56-
private static final String SHA256_ALGORITHM = "SHA-256";
57-
5854
/** The number of bytes the final hash value will have (SHA-256 variant). */
5955
private static final int SHA256_BLOCKSIZE = 32;
6056

6157
/** The prefixes that can be used to identify this crypt() variant (SHA-256). */
6258
static final String SHA256_PREFIX = "$5$";
6359

64-
/** The SHA-512 MessageDigest algorithm. */
65-
private static final String SHA512_ALGORITHM = "SHA-512";
66-
6760
/** The number of bytes the final hash value will have (SHA-512 variant). */
6861
private static final int SHA512_BLOCKSIZE = 64;
6962

@@ -79,9 +72,10 @@ public class Sha2Crypt {
7972
* <p>
8073
* See {@link Crypt#crypt(String, String)} for details.
8174
*
82-
* @throws NoSuchAlgorithmException if no "SHA-256" algorithm implementation is available
75+
* @throws RuntimeException
76+
* when a {@link java.security.NoSuchAlgorithmException} is caught.
8377
*/
84-
public static String sha256Crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
78+
public static String sha256Crypt(byte[] keyBytes) {
8579
return sha256Crypt(keyBytes, null);
8680
}
8781

@@ -91,13 +85,14 @@ public static String sha256Crypt(byte[] keyBytes) throws NoSuchAlgorithmExceptio
9185
* See {@link Crypt#crypt(String, String)} for details.
9286
*
9387
* @throws IllegalArgumentException if the salt does not match the allowed pattern
94-
* @throws NoSuchAlgorithmException if no "SHA-256" algorithm implementation is available
88+
* @throws RuntimeException
89+
* when a {@link java.security.NoSuchAlgorithmException} is caught.
9590
*/
96-
public static String sha256Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
91+
public static String sha256Crypt(byte[] keyBytes, String salt) {
9792
if (salt == null) {
9893
salt = SHA256_PREFIX + B64.getRandomSalt(8);
9994
}
100-
return sha2Crypt(keyBytes, salt, SHA256_PREFIX, SHA256_BLOCKSIZE, SHA256_ALGORITHM);
95+
return sha2Crypt(keyBytes, salt, SHA256_PREFIX, SHA256_BLOCKSIZE, MessageDigestAlgorithms.SHA_256);
10196
}
10297

10398
/**
@@ -121,10 +116,11 @@ public static String sha256Crypt(byte[] keyBytes, String salt) throws NoSuchAlgo
121116
* {@link MessageDigest} algorithm identifier string
122117
* @return complete hash value including prefix and salt
123118
* @throws IllegalArgumentException if the given salt is {@code null} or does not match the allowed pattern
124-
* @throws NoSuchAlgorithmException if no implementation for the given algorithm is available
119+
* @throws RuntimeException
120+
* when a {@link java.security.NoSuchAlgorithmException} is caught.
125121
*/
126122
private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix, int blocksize, String algorithm)
127-
throws NoSuchAlgorithmException {
123+
{
128124

129125
int keyLen = keyBytes.length;
130126

@@ -150,7 +146,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
150146

151147
// 1. start digest A
152148
// Prepare for the real work.
153-
MessageDigest ctx = MessageDigest.getInstance(algorithm);
149+
MessageDigest ctx = DigestUtils.getDigest(algorithm);
154150

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

183179
// 5. add the password to digest B
184180
/*
@@ -256,7 +252,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
256252
/*
257253
* Start computation of P byte sequence.
258254
*/
259-
altCtx = MessageDigest.getInstance(algorithm);
255+
altCtx = DigestUtils.getDigest(algorithm);
260256

261257
// 14. for every byte in the password (excluding the terminating NUL byte
262258
// in the C representation of the string)
@@ -297,7 +293,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
297293
/*
298294
* Start computation of S byte sequence.
299295
*/
300-
altCtx = MessageDigest.getInstance(algorithm);
296+
altCtx = DigestUtils.getDigest(algorithm);
301297

302298
// 18. repeast the following 16+A[0] times, where A[0] represents the first
303299
// byte in digest A interpreted as an 8-bit unsigned value
@@ -351,7 +347,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
351347
/*
352348
* New context.
353349
*/
354-
ctx = MessageDigest.getInstance(algorithm);
350+
ctx = DigestUtils.getDigest(algorithm);
355351

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

@@ -516,12 +513,13 @@ public static String sha512Crypt(byte[] keyBytes) throws NoSuchAlgorithmExceptio
516513
* See {@link Crypt#crypt(String, String)} for details.
517514
*
518515
* @throws IllegalArgumentException if the salt does not match the allowed pattern
519-
* @throws NoSuchAlgorithmException if no "SHA-512" algorithm implementation is available
516+
* @throws RuntimeException
517+
* when a {@link java.security.NoSuchAlgorithmException} is caught.
520518
*/
521-
public static String sha512Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
519+
public static String sha512Crypt(byte[] keyBytes, String salt) {
522520
if (salt == null) {
523521
salt = SHA512_PREFIX + B64.getRandomSalt(8);
524522
}
525-
return sha2Crypt(keyBytes, salt, SHA512_PREFIX, SHA512_BLOCKSIZE, SHA512_ALGORITHM);
523+
return sha2Crypt(keyBytes, salt, SHA512_PREFIX, SHA512_BLOCKSIZE, MessageDigestAlgorithms.SHA_512);
526524
}
527525
}

0 commit comments

Comments
 (0)