Skip to content

Commit fb41ecd

Browse files
committed
Refactor common code and complete test coverage (100%, see clover report).
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/codec/trunk@130219 13f79535-47bb-0310-9956-ffa450edef68
1 parent ca86702 commit fb41ecd

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,40 +63,47 @@
6363
import org.apache.commons.codec.binary.Hex;
6464

6565
/**
66-
* Operations to simplifiy common <code>MessageDigest</code> tasks. This
66+
* Operations to simplifiy common {@link java.security.MessageDigest} tasks. This
6767
* class is thread safe.
6868
*
6969
* @author Dave Dribin
7070
* @author David Graham
71+
* @author Gary Gregory
7172
*/
7273
public class DigestUtils {
7374

74-
/**
75-
* Returns an MD5 MessageDigest.
76-
*
77-
* @return An MD5 digest instance.
78-
*/
79-
private static MessageDigest getMd5Digest() {
80-
try {
81-
return MessageDigest.getInstance("MD5");
82-
83-
} catch (NoSuchAlgorithmException e) {
84-
throw new RuntimeException(e.getMessage());
85-
}
86-
}
75+
/**
76+
* Returns a MessageDigest for the given <code>algorithm</code>.
77+
*
78+
* @return An MD5 digest instance.
79+
* @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
80+
*/
81+
protected static MessageDigest getDigest(String algorithm) {
82+
try {
83+
return MessageDigest.getInstance(algorithm);
84+
} catch (NoSuchAlgorithmException e) {
85+
throw new RuntimeException(e.getMessage());
86+
}
87+
}
88+
89+
/**
90+
* Returns an MD5 MessageDigest.
91+
*
92+
* @return An MD5 digest instance.
93+
* @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
94+
*/
95+
private static MessageDigest getMd5Digest() {
96+
return getDigest("MD5");
97+
}
8798

8899
/**
89100
* Returns an SHA digest.
90101
*
91102
* @return An SHA digest instance.
103+
* @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
92104
*/
93105
private static MessageDigest getShaDigest() {
94-
try {
95-
return MessageDigest.getInstance("SHA");
96-
97-
} catch (NoSuchAlgorithmException e) {
98-
throw new RuntimeException(e.getMessage());
99-
}
106+
return getDigest("SHA");
100107
}
101108

102109
/**

0 commit comments

Comments
 (0)