tobrien 2003/10/11 19:54:41
Modified: codec/src/test/org/apache/commons/codec/digest
DigestUtilsTest.java
codec/src/java/org/apache/commons/codec/digest
DigestUtils.java
Log:
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%
Revision Changes Path
1.4 +34 -1
jakarta-commons/codec/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java
Index: DigestUtilsTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/codec/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DigestUtilsTest.java 5 Oct 2003 21:45:49 -0000 1.3
+++ DigestUtilsTest.java 12 Oct 2003 02:54:40 -0000 1.4
@@ -57,6 +57,12 @@
package org.apache.commons.codec.digest;
+import java.security.MessageDigest;
+import java.security.MessageDigestSpi;
+import java.security.Security;
+
+import org.apache.commons.codec.EncoderException;
+
import junit.framework.TestCase;
/**
@@ -129,11 +135,38 @@
"a9993e364706816aba3e25717850c26c9cd0d89d",
DigestUtils.shaHex("abc"));
+ assertEquals(
+ "a9993e364706816aba3e25717850c26c9cd0d89d",
+ DigestUtils.shaHex("abc".getBytes()));
+
assertEquals(
"84983e441c3bd26ebaae4aa1f95129e5e54670f1",
DigestUtils.shaHex(
"abcdbcdecdefdefgefghfghighij"
+ "hijkijkljklmklmnlmnomnopnopq"));
}
-
+
+ public void testMd5NoAvailable() {
+ DigestUtils.setProvider( Security.getProviders()[3]);
+
+ try {
+ DigestUtils.md5("test");
+ fail( "The provider does not supply the MD5 algorithm, this operation
should have failed");
+ } catch( RuntimeException re ) {
+
+ }
+
+ }
+
+ public void testSHANoAvailable() {
+ DigestUtils.setProvider( Security.getProviders()[3]);
+
+ try {
+ DigestUtils.sha("test");
+ fail( "The provider does not supply the SHA algorithm, this operation
should have failed");
+ } catch( RuntimeException re ) {
+
+ }
+
+ }
}
1.4 +31 -7
jakarta-commons/codec/src/java/org/apache/commons/codec/digest/DigestUtils.java
Index: DigestUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/codec/src/java/org/apache/commons/codec/digest/DigestUtils.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DigestUtils.java 5 Oct 2003 21:45:49 -0000 1.3
+++ DigestUtils.java 12 Oct 2003 02:54:40 -0000 1.4
@@ -59,6 +59,8 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import java.security.Provider;
+import java.security.Security;
import org.apache.commons.codec.binary.Hex;
@@ -70,6 +72,13 @@
* @author David Graham
*/
public class DigestUtils {
+
+ /**
+ * This is the provider which DigestUtils uses to get instances of the MD5 and
SHA
+ * algorithms. This variable is altered if a users wishes to customize the
implementation
+ * of an algorithm.
+ */
+ private static Provider provider = null;
/**
* Returns an MD5 MessageDigest.
@@ -78,10 +87,13 @@
*/
private static MessageDigest getMd5Digest() {
try {
- return MessageDigest.getInstance("MD5");
-
+ if( provider != null ) {
+ return MessageDigest.getInstance("MD5", provider);
+ } else {
+ return MessageDigest.getInstance("MD5");
+ }
} catch (NoSuchAlgorithmException e) {
- throw new InternalError(e.getMessage());
+ throw new RuntimeException("Unable to get instance of MD5
message digest in DigestUtils" + e.getMessage());
}
}
@@ -92,10 +104,13 @@
*/
private static MessageDigest getShaDigest() {
try {
- return MessageDigest.getInstance("SHA");
-
+ if( provider != null) {
+ return MessageDigest.getInstance("SHA", provider);
+ } else {
+ return MessageDigest.getInstance("SHA");
+ }
} catch (NoSuchAlgorithmException e) {
- throw new InternalError(e.getMessage());
+ throw new RuntimeException("Unable to get instance of SHA message
digest in DigestUtils" + e.getMessage());
}
}
@@ -184,5 +199,14 @@
public static String shaHex(String data) {
return new String(Hex.encodeHex(sha(data)));
}
-
+
+ /**
+ * Allows for the replacement of the default Provider from which the DigestUtils
+ * retrieves the implementations of MD5 and SHA.
+ *
+ * @param provider an instance of a Provider
+ */
+ public static void setProvider(Provider pProvider) {
+ provider = pProvider;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]