1717
1818package org .apache .commons .codec .digest ;
1919
20+ import java .io .IOException ;
21+ import java .io .InputStream ;
2022import java .security .MessageDigest ;
2123import java .security .NoSuchAlgorithmException ;
2224
3032 */
3133public class DigestUtils {
3234
35+ private static final int STREAM_BUFFER_LENGTH = 1024 ;
36+
3337 /**
3438 * Returns a <code>MessageDigest</code> for the given <code>algorithm</code>.
3539 *
@@ -115,6 +119,26 @@ private static MessageDigest getShaDigest() {
115119 return getDigest ("SHA" );
116120 }
117121
122+ /**
123+ * Read through an InputStream and returns the digest for the data
124+ *
125+ * @param digest The MessageDigest to use (e.g. MD5)
126+ * @param data Data to digest
127+ * @return MD5 digest
128+ * @throws IOException On error reading from the stream
129+ */
130+ private static byte [] digest (MessageDigest digest , InputStream data ) throws IOException {
131+ byte [] buffer = new byte [STREAM_BUFFER_LENGTH ];
132+ int read = data .read (buffer , 0 , STREAM_BUFFER_LENGTH );
133+
134+ while (read > -1 ) {
135+ digest .update (buffer , 0 , read );
136+ read = data .read (buffer , 0 , STREAM_BUFFER_LENGTH );
137+ }
138+
139+ return digest .digest ();
140+ }
141+
118142 /**
119143 * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
120144 *
@@ -126,6 +150,18 @@ public static byte[] md5(byte[] data) {
126150 return getMd5Digest ().digest (data );
127151 }
128152
153+ /**
154+ * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
155+ *
156+ * @param data
157+ * Data to digest
158+ * @return MD5 digest
159+ * @throws IOException On error reading from the stream
160+ */
161+ public static byte [] md5 (InputStream data ) throws IOException {
162+ return digest (getMd5Digest (), data );
163+ }
164+
129165 /**
130166 * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
131167 *
@@ -159,6 +195,18 @@ public static String md5Hex(String data) {
159195 return new String (Hex .encodeHex (md5 (data )));
160196 }
161197
198+ /**
199+ * Calculates the MD5 digest and returns the value as a 32 character hex string.
200+ *
201+ * @param data
202+ * Data to digest
203+ * @return MD5 digest as a hex string
204+ * @throws IOException On error reading from the stream
205+ */
206+ public static String md5Hex (InputStream data ) throws IOException {
207+ return new String (Hex .encodeHex (md5 (data )));
208+ }
209+
162210 /**
163211 * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
164212 *
@@ -181,6 +229,18 @@ public static byte[] sha(String data) {
181229 return sha (data .getBytes ());
182230 }
183231
232+ /**
233+ * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
234+ *
235+ * @param data
236+ * Data to digest
237+ * @return SHA-1 digest
238+ * @throws IOException On error reading from the stream
239+ */
240+ public static byte [] sha (InputStream data ) throws IOException {
241+ return digest (getShaDigest (), data );
242+ }
243+
184244 /**
185245 * Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.
186246 * <p>
@@ -209,6 +269,21 @@ public static byte[] sha256(String data) {
209269 return sha256 (data .getBytes ());
210270 }
211271
272+ /**
273+ * Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.
274+ * <p>
275+ * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
276+ * </p>
277+ *
278+ * @param data
279+ * Data to digest
280+ * @return SHA-256 digest
281+ * @throws IOException On error reading from the stream
282+ */
283+ public static byte [] sha256 (InputStream data ) throws IOException {
284+ return digest (getSha256Digest (), data );
285+ }
286+
212287 /**
213288 * Calculates the SHA-256 digest and returns the value as a hex string.
214289 * <p>
@@ -237,6 +312,21 @@ public static String sha256Hex(String data) {
237312 return new String (Hex .encodeHex (sha256 (data )));
238313 }
239314
315+ /**
316+ * Calculates the SHA-256 digest and returns the value as a hex string.
317+ * <p>
318+ * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
319+ * </p>
320+ *
321+ * @param data
322+ * Data to digest
323+ * @return SHA-256 digest as a hex string
324+ * @throws IOException On error reading from the stream
325+ */
326+ public static String sha256Hex (InputStream data ) throws IOException {
327+ return new String (Hex .encodeHex (sha256 (data )));
328+ }
329+
240330 /**
241331 * Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.
242332 * <p>
@@ -265,6 +355,21 @@ public static byte[] sha384(byte[] data) {
265355 public static byte [] sha384 (String data ) {
266356 return sha384 (data .getBytes ());
267357 }
358+
359+ /**
360+ * Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.
361+ * <p>
362+ * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
363+ * </p>
364+ *
365+ * @param data
366+ * Data to digest
367+ * @return SHA-384 digest
368+ * @throws IOException On error reading from the stream
369+ */
370+ public static byte [] sha384 (InputStream data ) throws IOException {
371+ return digest (getSha384Digest (), data );
372+ }
268373
269374 /**
270375 * Calculates the SHA-384 digest and returns the value as a hex string.
@@ -294,6 +399,21 @@ public static String sha384Hex(String data) {
294399 return new String (Hex .encodeHex (sha384 (data )));
295400 }
296401
402+ /**
403+ * Calculates the SHA-384 digest and returns the value as a hex string.
404+ * <p>
405+ * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
406+ * </p>
407+ *
408+ * @param data
409+ * Data to digest
410+ * @return SHA-384 digest as a hex string
411+ * @throws IOException On error reading from the stream
412+ */
413+ public static String sha384Hex (InputStream data ) throws IOException {
414+ return new String (Hex .encodeHex (sha384 (data )));
415+ }
416+
297417 /**
298418 * Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>.
299419 * <p>
@@ -322,6 +442,21 @@ public static byte[] sha512(String data) {
322442 return sha512 (data .getBytes ());
323443 }
324444
445+ /**
446+ * Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>.
447+ * <p>
448+ * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
449+ * </p>
450+ *
451+ * @param data
452+ * Data to digest
453+ * @return SHA-512 digest
454+ * @throws IOException On error reading from the stream
455+ */
456+ public static byte [] sha512 (InputStream data ) throws IOException {
457+ return digest (getSha512Digest (), data );
458+ }
459+
325460 /**
326461 * Calculates the SHA-512 digest and returns the value as a hex string.
327462 * <p>
@@ -350,6 +485,21 @@ public static String sha512Hex(String data) {
350485 return new String (Hex .encodeHex (sha512 (data )));
351486 }
352487
488+ /**
489+ * Calculates the SHA-512 digest and returns the value as a hex string.
490+ * <p>
491+ * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
492+ * </p>
493+ *
494+ * @param data
495+ * Data to digest
496+ * @return SHA-512 digest as a hex string
497+ * @throws IOException On error reading from the stream
498+ */
499+ public static String sha512Hex (InputStream data ) throws IOException {
500+ return new String (Hex .encodeHex (sha512 (data )));
501+ }
502+
353503 /**
354504 * Calculates the SHA-1 digest and returns the value as a hex string.
355505 *
@@ -371,4 +521,16 @@ public static String shaHex(byte[] data) {
371521 public static String shaHex (String data ) {
372522 return new String (Hex .encodeHex (sha (data )));
373523 }
524+
525+ /**
526+ * Calculates the SHA-1 digest and returns the value as a hex string.
527+ *
528+ * @param data
529+ * Data to digest
530+ * @return SHA-1 digest as a hex string
531+ * @throws IOException On error reading from the stream
532+ */
533+ public static String shaHex (InputStream data ) throws IOException {
534+ return new String (Hex .encodeHex (sha (data )));
535+ }
374536}
0 commit comments