Skip to content

Commit 536ede3

Browse files
committed
Cleanup of throws clauses in new crypt classes. Additional javadoc fixes.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1374430 13f79535-47bb-0310-9956-ffa450edef68
1 parent d2c0d2a commit 536ede3

10 files changed

Lines changed: 296 additions & 242 deletions

File tree

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

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

19+
import java.security.NoSuchAlgorithmException;
20+
1921
import org.apache.commons.codec.Charsets;
2022

2123
/**
2224
* GNU libc crypt(3) compatible hash method.
2325
* <p>
2426
* See {@link #crypt(String, String)} for further details.
25-
*
26-
* <p>This class is immutable and thread-safe.</p>
27+
* <p>
28+
* This class is immutable and thread-safe.
2729
*
2830
* @version $Id$
2931
* @since 1.7
@@ -39,24 +41,27 @@ public class Crypt {
3941
* @param keyBytes
4042
* plaintext password
4143
* @return hash value
44+
* @throws NoSuchAlgorithmException if no algorithm implementation is available
4245
*/
43-
public static String crypt(byte[] keyBytes) throws Exception {
46+
public static String crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
4447
return crypt(keyBytes, null);
4548
}
4649

4750
/**
4851
* Encrypts a password in a crypt(3) compatible way.
4952
* <p>
50-
* A random salt and the default algorithm (currently SHA-512) are used. See
51-
* {@link #crypt(String, String)} for details.
53+
* If no salt is provided, a random salt and the default algorithm (currently SHA-512) will be used.
54+
* See {@link #crypt(String, String)} for details.
5255
*
5356
* @param keyBytes
5457
* plaintext password
5558
* @param salt
5659
* salt value
5760
* @return hash value
61+
* @throws IllegalArgumentException if the salt does not match the allowed pattern
62+
* @throws NoSuchAlgorithmException if no algorithm implementation is available
5863
*/
59-
public static String crypt(byte[] keyBytes, String salt) throws Exception {
64+
public static String crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
6065
if (salt == null) {
6166
return Sha2Crypt.sha512Crypt(keyBytes);
6267
} else if (salt.startsWith(Sha2Crypt.SHA512_PREFIX)) {
@@ -79,14 +84,14 @@ public static String crypt(byte[] keyBytes, String salt) throws Exception {
7984
* @param key
8085
* plaintext password
8186
* @return hash value
87+
* @throws NoSuchAlgorithmException if no algorithm implementation is available
8288
*/
83-
public static String crypt(String key) throws Exception {
89+
public static String crypt(String key) throws NoSuchAlgorithmException {
8490
return crypt(key, null);
8591
}
8692

8793
/**
8894
* Encrypts a password in a crypt(3) compatible way.
89-
*
9095
* <p>
9196
* The exact algorithm depends on the format of the salt string:
9297
* <ul>
@@ -98,16 +103,13 @@ public static String crypt(String key) throws Exception {
98103
* </ul>
99104
* The magic strings "$apr1$" and "$2a$" are not recognised by this method as its
100105
* output should be identical with that of the libc implementation.
101-
*
102106
* <p>
103107
* The rest of the salt string is drawn from the set [a-zA-Z0-9./] and is cut at the
104108
* maximum length of if a "$" sign is encountered. It is therefore valid to enter a
105109
* complete hash value as salt to e.g. verify a password with:
106-
*
107110
* <pre>
108111
* storedPwd.equals(crypt(enteredPwd, storedPwd))
109112
* </pre>
110-
*
111113
* <p>
112114
* The resulting string starts with the marker string ($6$), continues with the salt
113115
* value and ends with a "$" sign followed by the actual hash value. For DES the string
@@ -118,15 +120,12 @@ public static String crypt(String key) throws Exception {
118120
* <li>MD5: 34 chars
119121
* <li>DES: 13 chars
120122
* </ul>
121-
*
122123
* <p>
123124
* Example:
124-
*
125125
* <pre>
126126
* crypt("secret", "$1$xxxx") => "$1$xxxx$aMkevjfEIpa35Bh3G4bAc."
127127
* crypt("secret", "xx") => "xxWAum7tHdIUw"
128128
* </pre>
129-
*
130129
* <p>
131130
* This method comes in a variation that accepts a byte[] array to support input strings that
132131
* 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 static String crypt(String key) throws Exception {
137136
* @param salt
138137
* salt value
139138
* @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
140141
*/
141-
public static String crypt(String key, String salt) throws Exception {
142+
public static String crypt(String key, String salt) throws NoSuchAlgorithmException {
142143
return crypt(key.getBytes(Charsets.UTF_8), salt);
143144
}
144145
}

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

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

1919
import java.security.MessageDigest;
20+
import java.security.NoSuchAlgorithmException;
2021
import java.util.Arrays;
2122
import java.util.regex.Matcher;
2223
import java.util.regex.Pattern;
@@ -27,63 +28,67 @@
2728
* The libc crypt() "$1$" and Apache "$apr1$" MD5-based hash algorithm.
2829
* <p>
2930
* Based on the public domain ("beer-ware") C implementation from Poul-Henning Kamp which was found at:
30-
* </p>
31+
* <a href="http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libcrypt/crypt-md5.c?rev=1.1;content-type=text%2Fplain">
32+
* crypt-md5.c @ freebsd.org</a><br/>
3133
* <p>
32-
* http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libcrypt/crypt-md5.c?rev=1.1;content-type=text%2Fplain<br/>
33-
* Source: $FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.1 1999/01/21 13:50:09 brandon Exp $
34-
* </p>
35-
* <p>Conversion to Kotlin and from there to Java in 2012.</p>
36-
*
37-
* <p>The C style comments are from the original C code, the ones with "//" from the port.</p>
38-
*
39-
* <p>This class is immutable and thread-safe.</p>
34+
* Source:
35+
* <pre>$FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.1 1999/01/21 13:50:09 brandon Exp $</pre>
36+
* <p>
37+
* Conversion to Kotlin and from there to Java in 2012.
38+
* <p>
39+
* The C style comments are from the original C code, the ones with "//" from the port.
40+
* <p>
41+
* This class is immutable and thread-safe.
4042
*
4143
* @version $Id$
4244
* @since 1.7
4345
*/
4446
public class Md5Crypt {
4547

46-
/**
47-
* The Identifier of the Apache variant.
48-
*/
48+
/** The Identifier of the Apache variant. */
4949
static final String APR1_PREFIX = "$apr1$";
5050

51-
/**
52-
* The number of bytes of the final hash.
53-
*/
51+
/** The number of bytes of the final hash. */
5452
private static final int BLOCKSIZE = 16;
5553

56-
/**
57-
* The MessageDigest MD5_ALGORITHM.
58-
*/
54+
/** The MessageDigest MD5_ALGORITHM. */
5955
private static final String MD5_ALGORITHM = "MD5";
6056

61-
/**
62-
* The Identifier of this crypt() variant.
63-
*/
57+
/** The Identifier of this crypt() variant. */
6458
static final String MD5_PREFIX = "$1$";
6559

66-
/**
67-
* The number of rounds of the big loop.
68-
*/
60+
/** The number of rounds of the big loop. */
6961
private static final int ROUNDS = 1000;
7062

71-
/** See {@link #apr1Crypt(String, String)} for details. */
72-
public static String apr1Crypt(byte[] keyBytes) throws Exception {
63+
/**
64+
* See {@link #apr1Crypt(String, String)} for details.
65+
*
66+
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
67+
*/
68+
public static String apr1Crypt(byte[] keyBytes) throws NoSuchAlgorithmException {
7369
return apr1Crypt(keyBytes, APR1_PREFIX + B64.getRandomSalt(8));
7470
}
7571

76-
/** See {@link #apr1Crypt(String, String)} for details. */
77-
public static String apr1Crypt(byte[] keyBytes, String salt) throws Exception {
72+
/**
73+
* See {@link #apr1Crypt(String, String)} for details.
74+
*
75+
* @throws IllegalArgumentException if the salt does not match the allowed pattern
76+
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
77+
*/
78+
public static String apr1Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
7879
// to make the md5Crypt regex happy
7980
if (salt != null && !salt.startsWith(APR1_PREFIX)) {
8081
salt = APR1_PREFIX + salt;
8182
}
8283
return Md5Crypt.md5Crypt(keyBytes, salt, APR1_PREFIX);
8384
}
8485

85-
/** See {@link #apr1Crypt(String, String)} for details. */
86-
public static String apr1Crypt(String keyBytes) throws Exception {
86+
/**
87+
* See {@link #apr1Crypt(String, String)} for details.
88+
*
89+
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
90+
*/
91+
public static String apr1Crypt(String keyBytes) throws NoSuchAlgorithmException {
8792
return apr1Crypt(keyBytes.getBytes(Charsets.UTF_8));
8893
}
8994

@@ -99,17 +104,21 @@ public static String apr1Crypt(String keyBytes) throws Exception {
99104
* salt string including the prefix and optionally garbage at the end.
100105
* Will be generated randomly if null.
101106
* @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
102109
*/
103-
public static String apr1Crypt(String keyBytes, String salt) throws Exception {
110+
public static String apr1Crypt(String keyBytes, String salt) throws NoSuchAlgorithmException {
104111
return apr1Crypt(keyBytes.getBytes(Charsets.UTF_8), salt);
105112
}
106113

107114
/**
108115
* Generates a libc6 crypt() compatible "$1$" hash value.
109116
* <p>
110117
* See {@link Crypt#crypt(String, String)} for details.
118+
*
119+
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
111120
*/
112-
public static String md5Crypt(final byte[] keyBytes) throws Exception {
121+
public static String md5Crypt(final byte[] keyBytes) throws NoSuchAlgorithmException {
113122
return md5Crypt(keyBytes, MD5_PREFIX + B64.getRandomSalt(8));
114123
}
115124

@@ -124,26 +133,32 @@ public static String md5Crypt(final byte[] keyBytes) throws Exception {
124133
* salt string including the prefix and optionally garbage at the end.
125134
* Will be generated randomly if null.
126135
* @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
127138
*/
128-
public static String md5Crypt(byte[] keyBytes, String salt) throws Exception {
139+
public static String md5Crypt(byte[] keyBytes, String salt) throws NoSuchAlgorithmException {
129140
return md5Crypt(keyBytes, salt, MD5_PREFIX);
130141
}
131142

132143
/**
133144
* Generates a libc6 crypt() "$1$" or Apache htpasswd "$apr1$" hash value.
134145
* <p>
135146
* See {@link Crypt#crypt(String, String)} or {@link #apr1Crypt(String, String)} for details.
147+
*
148+
* @throws IllegalArgumentException if the salt does not match the allowed pattern
149+
* @throws NoSuchAlgorithmException if no "MD5" algorithm implementation is available
136150
*/
137-
public static String md5Crypt(final byte[] keyBytes, final String salt, final String prefix) throws Exception {
151+
public static String md5Crypt(final byte[] keyBytes, final String salt, final String prefix)
152+
throws NoSuchAlgorithmException {
138153
int keyLen = keyBytes.length;
139154

140155
// Extract the real salt from the given string which can be a complete hash string.
141156
String saltString;
142157
if (salt == null) {
143158
saltString = B64.getRandomSalt(8);
144159
} else {
145-
Pattern p = Pattern.compile("^" + prefix.replace("$", "\\$") + "([\\.\\/a-zA-Z0-9]{1,8}).*");
146-
Matcher m = p.matcher(salt);
160+
final Pattern p = Pattern.compile("^" + prefix.replace("$", "\\$") + "([\\.\\/a-zA-Z0-9]{1,8}).*");
161+
final Matcher m = p.matcher(salt);
147162
if (m == null || !m.find()) {
148163
throw new IllegalArgumentException("Invalid salt value: " + salt);
149164
}

0 commit comments

Comments
 (0)