Skip to content

Commit 4970841

Browse files
committed
[CODEC-208] Make some DigestUtils APIs public.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1743770 13f79535-47bb-0310-9956-ffa450edef68
1 parent 895315e commit 4970841

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ The <action> type attribute can be add,update,fix,remove.
4646
<action dev="sebb" type="fix" issue="CODEC-200" due-to="Luciano Vernaschi">Base32.HEX_DECODE_TABLE contains the wrong value 32</action>
4747
<action dev="ggregory" type="fix" issue="CODEC-207" due-to="Gary Gregory">Charsets Javadoc breaks build when using Java 8</action>
4848
<action dev="ggregory" type="fix" issue="CODEC-199" due-to="Yossi Tamari">Bug in HW rule in Soundex</action>
49+
<action dev="ggregory" type="add" issue="CODEC-208" due-to="Gary Gregory">Make some DigestUtils APIs public</action>
4950
<action dev="ggregory" type="add" issue="CODEC-206" due-to="Gary Gregory">Add java.io.File APIs to DigestUtils</action>
5051
<action dev="ggregory" type="add" issue="CODEC-183" due-to="Steven Wurster">BaseNCodecOutputStream only supports writing EOF on close()</action>
5152
<action dev="ggregory" type="add" issue="CODEC-195" due-to="Gary Gregory">Support SHA-224 in DigestUtils on Java 8</action>

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,47 +42,50 @@ public class DigestUtils {
4242
/**
4343
* Read through an ByteBuffer and returns the digest for the data
4444
*
45-
* @param digest
45+
* @param messageDigest
4646
* The MessageDigest to use (e.g. MD5)
4747
* @param data
4848
* Data to digest
4949
* @return the digest
5050
* @throws IOException
5151
* On error reading from the stream
52+
* @since 1.11
5253
*/
53-
private static byte[] digest(final MessageDigest messageDigest, final ByteBuffer data) {
54+
public static byte[] digest(final MessageDigest messageDigest, final ByteBuffer data) {
5455
messageDigest.update(data);
5556
return messageDigest.digest();
5657
}
5758

5859
/**
5960
* Read through a File and returns the digest for the data
6061
*
61-
* @param digest
62+
* @param messageDigest
6263
* The MessageDigest to use (e.g. MD5)
6364
* @param data
6465
* Data to digest
6566
* @return the digest
6667
* @throws IOException
6768
* On error reading from the stream
69+
* @since 1.11
6870
*/
69-
private static byte[] digest(final MessageDigest digest, final File data) throws IOException {
70-
return updateDigest(digest, data).digest();
71+
public static byte[] digest(final MessageDigest messageDigest, final File data) throws IOException {
72+
return updateDigest(messageDigest, data).digest();
7173
}
7274

7375
/**
7476
* Read through an InputStream and returns the digest for the data
7577
*
76-
* @param digest
78+
* @param messageDigest
7779
* The MessageDigest to use (e.g. MD5)
7880
* @param data
7981
* Data to digest
8082
* @return the digest
8183
* @throws IOException
8284
* On error reading from the stream
85+
* @since 1.11
8386
*/
84-
private static byte[] digest(final MessageDigest digest, final InputStream data) throws IOException {
85-
return updateDigest(digest, data).digest();
87+
public static byte[] digest(final MessageDigest messageDigest, final InputStream data) throws IOException {
88+
return updateDigest(messageDigest, data).digest();
8689
}
8790

8891
/**
@@ -106,6 +109,29 @@ public static MessageDigest getDigest(final String algorithm) {
106109
}
107110
}
108111

112+
/**
113+
* Returns a <code>MessageDigest</code> for the given <code>algorithm</code> or a default if there is a problem getting the algorithm.
114+
*
115+
* @param algorithm
116+
* the name of the algorithm requested. See <a
117+
* href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA"
118+
* >Appendix A in the Java Cryptography Architecture Reference Guide</a> for information about standard
119+
* algorithm names.
120+
* @param defaultMessageDigest The default MessageDigest.
121+
* @return A digest instance.
122+
* @see MessageDigest#getInstance(String)
123+
* @throws IllegalArgumentException
124+
* when a {@link NoSuchAlgorithmException} is caught.
125+
* @since 1.11
126+
*/
127+
public static MessageDigest getDigest(final String algorithm, MessageDigest defaultMessageDigest) {
128+
try {
129+
return MessageDigest.getInstance(algorithm);
130+
} catch (final Exception e) {
131+
return defaultMessageDigest;
132+
}
133+
}
134+
109135
/**
110136
* Returns an MD2 MessageDigest.
111137
*

0 commit comments

Comments
 (0)