Skip to content

Commit f2e7938

Browse files
committed
[CODEC-155] DigestUtils.getDigest(String) should throw IllegalArgumentException instead of RuntimeException.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1380010 13f79535-47bb-0310-9956-ffa450edef68
1 parent 03fd138 commit f2e7938

3 files changed

Lines changed: 28 additions & 15 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ The <action> type attribute can be add,update,fix,remove.
5151
</release>
5252
-->
5353
<release version="1.7" date="TBD" description="Feature and fix release.">
54+
<action issue="CODEC-155" dev="ggregory" type="add" due-to="ggregory">
55+
DigestUtils.getDigest(String) should throw IllegalArgumentException instead of RuntimeException.
56+
</action>
5457
<action issue="CODEC-153" dev="ggregory" type="add" due-to="ggregory">
5558
Create a class MessageDigestAlgorithms to define standard algorithm names.
5659
</action>

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,25 @@ private static byte[] getBytesUtf8(String data) {
7878
* names.
7979
* @return An MD5 digest instance.
8080
* @see MessageDigest#getInstance(String)
81-
* @throws RuntimeException
82-
* when a {@link java.security.NoSuchAlgorithmException} is caught.
81+
* @throws IllegalArgumentException
82+
* when a {@link NoSuchAlgorithmException} is caught.
8383
*/
8484
public static MessageDigest getDigest(String algorithm) {
8585
try {
8686
return MessageDigest.getInstance(algorithm);
8787
} catch (NoSuchAlgorithmException e) {
88-
throw new RuntimeException(e);
88+
throw new IllegalArgumentException(e);
8989
}
9090
}
9191

9292
/**
9393
* Returns an MD5 MessageDigest.
9494
*
9595
* @return An MD5 digest instance.
96-
* @throws RuntimeException
97-
* when a {@link java.security.NoSuchAlgorithmException} is caught.
96+
* @throws IllegalArgumentException
97+
* when a {@link NoSuchAlgorithmException} is caught, which should never happen because MD5 is a
98+
* built-in algorithm
99+
* @see MessageDigestAlgorithms#MD5
98100
*/
99101
public static MessageDigest getMd5Digest() {
100102
return getDigest(MessageDigestAlgorithms.MD5);
@@ -107,8 +109,10 @@ public static MessageDigest getMd5Digest() {
107109
* </p>
108110
*
109111
* @return An SHA-256 digest instance.
110-
* @throws RuntimeException
111-
* when a {@link java.security.NoSuchAlgorithmException} is caught.
112+
* @throws IllegalArgumentException
113+
* when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-256 is a
114+
* built-in algorithm
115+
* @see MessageDigestAlgorithms#SHA_256
112116
*/
113117
public static MessageDigest getSha256Digest() {
114118
return getDigest(MessageDigestAlgorithms.SHA_256);
@@ -121,8 +125,10 @@ public static MessageDigest getSha256Digest() {
121125
* </p>
122126
*
123127
* @return An SHA-384 digest instance.
124-
* @throws RuntimeException
125-
* when a {@link java.security.NoSuchAlgorithmException} is caught.
128+
* @throws IllegalArgumentException
129+
* when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-384 is a
130+
* built-in algorithm
131+
* @see MessageDigestAlgorithms#SHA_384
126132
*/
127133
public static MessageDigest getSha384Digest() {
128134
return getDigest(MessageDigestAlgorithms.SHA_384);
@@ -135,8 +141,10 @@ public static MessageDigest getSha384Digest() {
135141
* </p>
136142
*
137143
* @return An SHA-512 digest instance.
138-
* @throws RuntimeException
139-
* when a {@link java.security.NoSuchAlgorithmException} is caught.
144+
* @throws IllegalArgumentException
145+
* when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-512 is a
146+
* built-in algorithm
147+
* @see MessageDigestAlgorithms#SHA_512
140148
*/
141149
public static MessageDigest getSha512Digest() {
142150
return getDigest(MessageDigestAlgorithms.SHA_512);
@@ -146,8 +154,8 @@ public static MessageDigest getSha512Digest() {
146154
* Returns an SHA-1 digest.
147155
*
148156
* @return An SHA-1 digest instance.
149-
* @throws RuntimeException
150-
* when a {@link java.security.NoSuchAlgorithmException} is caught.
157+
* @throws IllegalArgumentException
158+
* when a {@link NoSuchAlgorithmException} is caught
151159
*/
152160
public static MessageDigest getShaDigest() {
153161
return getDigest("SHA");

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

Lines changed: 4 additions & 2 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;
@@ -117,8 +118,9 @@ public static String sha256Crypt(byte[] keyBytes, String salt) {
117118
* @return complete hash value including prefix and salt
118119
* @throws IllegalArgumentException
119120
* if the given salt is {@code null} or does not match the allowed pattern
120-
* @throws RuntimeException
121-
* when a {@link java.security.NoSuchAlgorithmException} is caught.
121+
* @throws IllegalArgumentException
122+
* when a {@link NoSuchAlgorithmException} is caught
123+
* @see MessageDigestAlgorithms
122124
*/
123125
private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix, int blocksize, String algorithm) {
124126

0 commit comments

Comments
 (0)