Skip to content

Commit d718e52

Browse files
author
Timothy O'Brien
committed
Added the ability to customize the Provider which is used
to get an instance of MD5 and SHA. By default, this Utils class will search the list of available Provider objects for implementations, but if you want to supply your own Provider object you may do so by calling the static method setProvider on DigestUtils. + Also, brought test coverage of DigestUtils up to 100% git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/codec/trunk@130210 13f79535-47bb-0310-9956-ffa450edef68
1 parent 10a1327 commit d718e52

2 files changed

Lines changed: 65 additions & 8 deletions

File tree

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959

6060
import java.security.MessageDigest;
6161
import java.security.NoSuchAlgorithmException;
62+
import java.security.Provider;
63+
import java.security.Security;
6264

6365
import org.apache.commons.codec.binary.Hex;
6466

@@ -70,6 +72,13 @@
7072
* @author David Graham
7173
*/
7274
public class DigestUtils {
75+
76+
/**
77+
* This is the provider which DigestUtils uses to get instances of the MD5 and SHA
78+
* algorithms. This variable is altered if a users wishes to customize the implementation
79+
* of an algorithm.
80+
*/
81+
private static Provider provider = null;
7382

7483
/**
7584
* Returns an MD5 MessageDigest.
@@ -78,10 +87,13 @@ public class DigestUtils {
7887
*/
7988
private static MessageDigest getMd5Digest() {
8089
try {
81-
return MessageDigest.getInstance("MD5");
82-
90+
if( provider != null ) {
91+
return MessageDigest.getInstance("MD5", provider);
92+
} else {
93+
return MessageDigest.getInstance("MD5");
94+
}
8395
} catch (NoSuchAlgorithmException e) {
84-
throw new InternalError(e.getMessage());
96+
throw new RuntimeException("Unable to get instance of MD5 message digest in DigestUtils" + e.getMessage());
8597
}
8698
}
8799

@@ -92,10 +104,13 @@ private static MessageDigest getMd5Digest() {
92104
*/
93105
private static MessageDigest getShaDigest() {
94106
try {
95-
return MessageDigest.getInstance("SHA");
96-
107+
if( provider != null) {
108+
return MessageDigest.getInstance("SHA", provider);
109+
} else {
110+
return MessageDigest.getInstance("SHA");
111+
}
97112
} catch (NoSuchAlgorithmException e) {
98-
throw new InternalError(e.getMessage());
113+
throw new RuntimeException("Unable to get instance of SHA message digest in DigestUtils" + e.getMessage());
99114
}
100115
}
101116

@@ -184,5 +199,14 @@ public static String shaHex(byte[] data) {
184199
public static String shaHex(String data) {
185200
return new String(Hex.encodeHex(sha(data)));
186201
}
187-
202+
203+
/**
204+
* Allows for the replacement of the default Provider from which the DigestUtils
205+
* retrieves the implementations of MD5 and SHA.
206+
*
207+
* @param provider an instance of a Provider
208+
*/
209+
public static void setProvider(Provider pProvider) {
210+
provider = pProvider;
211+
}
188212
}

src/test/org/apache/commons/codec/digest/DigestUtilsTest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757

5858
package org.apache.commons.codec.digest;
5959

60+
import java.security.MessageDigest;
61+
import java.security.MessageDigestSpi;
62+
import java.security.Security;
63+
64+
import org.apache.commons.codec.EncoderException;
65+
6066
import junit.framework.TestCase;
6167

6268
/**
@@ -129,11 +135,38 @@ public void testShaHex() {
129135
"a9993e364706816aba3e25717850c26c9cd0d89d",
130136
DigestUtils.shaHex("abc"));
131137

138+
assertEquals(
139+
"a9993e364706816aba3e25717850c26c9cd0d89d",
140+
DigestUtils.shaHex("abc".getBytes()));
141+
132142
assertEquals(
133143
"84983e441c3bd26ebaae4aa1f95129e5e54670f1",
134144
DigestUtils.shaHex(
135145
"abcdbcdecdefdefgefghfghighij"
136146
+ "hijkijkljklmklmnlmnomnopnopq"));
137147
}
138-
148+
149+
public void testMd5NoAvailable() {
150+
DigestUtils.setProvider( Security.getProviders()[3]);
151+
152+
try {
153+
DigestUtils.md5("test");
154+
fail( "The provider does not supply the MD5 algorithm, this operation should have failed");
155+
} catch( RuntimeException re ) {
156+
157+
}
158+
159+
}
160+
161+
public void testSHANoAvailable() {
162+
DigestUtils.setProvider( Security.getProviders()[3]);
163+
164+
try {
165+
DigestUtils.sha("test");
166+
fail( "The provider does not supply the SHA algorithm, this operation should have failed");
167+
} catch( RuntimeException re ) {
168+
169+
}
170+
171+
}
139172
}

0 commit comments

Comments
 (0)