@@ -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