@@ -34,6 +34,26 @@ public class DigestUtils {
3434
3535 private static final int STREAM_BUFFER_LENGTH = 1024 ;
3636
37+ /**
38+ * Read through an InputStream and returns the digest for the data
39+ *
40+ * @param digest The MessageDigest to use (e.g. MD5)
41+ * @param data Data to digest
42+ * @return MD5 digest
43+ * @throws IOException On error reading from the stream
44+ */
45+ private static byte [] digest (MessageDigest digest , InputStream data ) throws IOException {
46+ byte [] buffer = new byte [STREAM_BUFFER_LENGTH ];
47+ int read = data .read (buffer , 0 , STREAM_BUFFER_LENGTH );
48+
49+ while (read > -1 ) {
50+ digest .update (buffer , 0 , read );
51+ read = data .read (buffer , 0 , STREAM_BUFFER_LENGTH );
52+ }
53+
54+ return digest .digest ();
55+ }
56+
3757 /**
3858 * Returns a <code>MessageDigest</code> for the given <code>algorithm</code>.
3959 *
@@ -118,26 +138,6 @@ private static MessageDigest getSha512Digest() {
118138 private static MessageDigest getShaDigest () {
119139 return getDigest ("SHA" );
120140 }
121-
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- }
141141
142142 /**
143143 * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
@@ -157,6 +157,7 @@ public static byte[] md5(byte[] data) {
157157 * Data to digest
158158 * @return MD5 digest
159159 * @throws IOException On error reading from the stream
160+ * @since 1.4
160161 */
161162 public static byte [] md5 (InputStream data ) throws IOException {
162163 return digest (getMd5Digest (), data );
@@ -190,8 +191,10 @@ public static String md5Hex(byte[] data) {
190191 * @param data
191192 * Data to digest
192193 * @return MD5 digest as a hex string
194+ * @throws IOException On error reading from the stream
195+ * @since 1.4
193196 */
194- public static String md5Hex (String data ) {
197+ public static String md5Hex (InputStream data ) throws IOException {
195198 return new String (Hex .encodeHex (md5 (data )));
196199 }
197200
@@ -201,9 +204,8 @@ public static String md5Hex(String data) {
201204 * @param data
202205 * Data to digest
203206 * @return MD5 digest as a hex string
204- * @throws IOException On error reading from the stream
205207 */
206- public static String md5Hex (InputStream data ) throws IOException {
208+ public static String md5Hex (String data ) {
207209 return new String (Hex .encodeHex (md5 (data )));
208210 }
209211
@@ -224,9 +226,10 @@ public static byte[] sha(byte[] data) {
224226 * @param data
225227 * Data to digest
226228 * @return SHA-1 digest
229+ * @throws IOException On error reading from the stream
227230 */
228- public static byte [] sha (String data ) {
229- return sha ( data . getBytes () );
231+ public static byte [] sha (InputStream data ) throws IOException {
232+ return digest ( getShaDigest (), data );
230233 }
231234
232235 /**
@@ -235,10 +238,9 @@ public static byte[] sha(String data) {
235238 * @param data
236239 * Data to digest
237240 * @return SHA-1 digest
238- * @throws IOException On error reading from the stream
239241 */
240- public static byte [] sha (InputStream data ) throws IOException {
241- return digest ( getShaDigest (), data );
242+ public static byte [] sha (String data ) {
243+ return sha ( data . getBytes () );
242244 }
243245
244246 /**
@@ -250,6 +252,7 @@ public static byte[] sha(InputStream data) throws IOException {
250252 * @param data
251253 * Data to digest
252254 * @return SHA-256 digest
255+ * @since 1.4
253256 */
254257 public static byte [] sha256 (byte [] data ) {
255258 return getSha256Digest ().digest (data );
@@ -264,9 +267,11 @@ public static byte[] sha256(byte[] data) {
264267 * @param data
265268 * Data to digest
266269 * @return SHA-256 digest
270+ * @throws IOException On error reading from the stream
271+ * @since 1.4
267272 */
268- public static byte [] sha256 (String data ) {
269- return sha256 ( data . getBytes () );
273+ public static byte [] sha256 (InputStream data ) throws IOException {
274+ return digest ( getSha256Digest (), data );
270275 }
271276
272277 /**
@@ -278,10 +283,10 @@ public static byte[] sha256(String data) {
278283 * @param data
279284 * Data to digest
280285 * @return SHA-256 digest
281- * @throws IOException On error reading from the stream
286+ * @since 1.4
282287 */
283- public static byte [] sha256 (InputStream data ) throws IOException {
284- return digest ( getSha256Digest (), data );
288+ public static byte [] sha256 (String data ) {
289+ return sha256 ( data . getBytes () );
285290 }
286291
287292 /**
@@ -293,6 +298,7 @@ public static byte[] sha256(InputStream data) throws IOException {
293298 * @param data
294299 * Data to digest
295300 * @return SHA-256 digest as a hex string
301+ * @since 1.4
296302 */
297303 public static String sha256Hex (byte [] data ) {
298304 return new String (Hex .encodeHex (sha256 (data )));
@@ -307,8 +313,10 @@ public static String sha256Hex(byte[] data) {
307313 * @param data
308314 * Data to digest
309315 * @return SHA-256 digest as a hex string
316+ * @throws IOException On error reading from the stream
317+ * @since 1.4
310318 */
311- public static String sha256Hex (String data ) {
319+ public static String sha256Hex (InputStream data ) throws IOException {
312320 return new String (Hex .encodeHex (sha256 (data )));
313321 }
314322
@@ -321,9 +329,9 @@ public static String sha256Hex(String data) {
321329 * @param data
322330 * Data to digest
323331 * @return SHA-256 digest as a hex string
324- * @throws IOException On error reading from the stream
332+ * @since 1.4
325333 */
326- public static String sha256Hex (InputStream data ) throws IOException {
334+ public static String sha256Hex (String data ) {
327335 return new String (Hex .encodeHex (sha256 (data )));
328336 }
329337
@@ -336,9 +344,9 @@ public static String sha256Hex(InputStream data) throws IOException {
336344 * @param data
337345 * Data to digest
338346 * @return SHA-384 digest
347+ * @since 1.4
339348 */
340349 public static byte [] sha384 (byte [] data ) {
341- // FIXME: check Sun docs for how to get a sha 384 digest
342350 return getSha384Digest ().digest (data );
343351 }
344352
@@ -351,9 +359,11 @@ public static byte[] sha384(byte[] data) {
351359 * @param data
352360 * Data to digest
353361 * @return SHA-384 digest
362+ * @throws IOException On error reading from the stream
363+ * @since 1.4
354364 */
355- public static byte [] sha384 (String data ) {
356- return sha384 ( data . getBytes () );
365+ public static byte [] sha384 (InputStream data ) throws IOException {
366+ return digest ( getSha384Digest (), data );
357367 }
358368
359369 /**
@@ -365,10 +375,10 @@ public static byte[] sha384(String data) {
365375 * @param data
366376 * Data to digest
367377 * @return SHA-384 digest
368- * @throws IOException On error reading from the stream
378+ * @since 1.4
369379 */
370- public static byte [] sha384 (InputStream data ) throws IOException {
371- return digest ( getSha384Digest (), data );
380+ public static byte [] sha384 (String data ) {
381+ return sha384 ( data . getBytes () );
372382 }
373383
374384 /**
@@ -380,6 +390,7 @@ public static byte[] sha384(InputStream data) throws IOException {
380390 * @param data
381391 * Data to digest
382392 * @return SHA-384 digest as a hex string
393+ * @since 1.4
383394 */
384395 public static String sha384Hex (byte [] data ) {
385396 return new String (Hex .encodeHex (sha384 (data )));
@@ -394,8 +405,10 @@ public static String sha384Hex(byte[] data) {
394405 * @param data
395406 * Data to digest
396407 * @return SHA-384 digest as a hex string
408+ * @throws IOException On error reading from the stream
409+ * @since 1.4
397410 */
398- public static String sha384Hex (String data ) {
411+ public static String sha384Hex (InputStream data ) throws IOException {
399412 return new String (Hex .encodeHex (sha384 (data )));
400413 }
401414
@@ -408,9 +421,9 @@ public static String sha384Hex(String data) {
408421 * @param data
409422 * Data to digest
410423 * @return SHA-384 digest as a hex string
411- * @throws IOException On error reading from the stream
424+ * @since 1.4
412425 */
413- public static String sha384Hex (InputStream data ) throws IOException {
426+ public static String sha384Hex (String data ) {
414427 return new String (Hex .encodeHex (sha384 (data )));
415428 }
416429
@@ -423,6 +436,7 @@ public static String sha384Hex(InputStream data) throws IOException {
423436 * @param data
424437 * Data to digest
425438 * @return SHA-512 digest
439+ * @since 1.4
426440 */
427441 public static byte [] sha512 (byte [] data ) {
428442 return getSha512Digest ().digest (data );
@@ -437,9 +451,11 @@ public static byte[] sha512(byte[] data) {
437451 * @param data
438452 * Data to digest
439453 * @return SHA-512 digest
454+ * @throws IOException On error reading from the stream
455+ * @since 1.4
440456 */
441- public static byte [] sha512 (String data ) {
442- return sha512 ( data . getBytes () );
457+ public static byte [] sha512 (InputStream data ) throws IOException {
458+ return digest ( getSha512Digest (), data );
443459 }
444460
445461 /**
@@ -451,10 +467,10 @@ public static byte[] sha512(String data) {
451467 * @param data
452468 * Data to digest
453469 * @return SHA-512 digest
454- * @throws IOException On error reading from the stream
470+ * @since 1.4
455471 */
456- public static byte [] sha512 (InputStream data ) throws IOException {
457- return digest ( getSha512Digest (), data );
472+ public static byte [] sha512 (String data ) {
473+ return sha512 ( data . getBytes () );
458474 }
459475
460476 /**
@@ -466,6 +482,7 @@ public static byte[] sha512(InputStream data) throws IOException {
466482 * @param data
467483 * Data to digest
468484 * @return SHA-512 digest as a hex string
485+ * @since 1.4
469486 */
470487 public static String sha512Hex (byte [] data ) {
471488 return new String (Hex .encodeHex (sha512 (data )));
@@ -480,8 +497,10 @@ public static String sha512Hex(byte[] data) {
480497 * @param data
481498 * Data to digest
482499 * @return SHA-512 digest as a hex string
500+ * @throws IOException On error reading from the stream
501+ * @since 1.4
483502 */
484- public static String sha512Hex (String data ) {
503+ public static String sha512Hex (InputStream data ) throws IOException {
485504 return new String (Hex .encodeHex (sha512 (data )));
486505 }
487506
@@ -494,9 +513,9 @@ public static String sha512Hex(String data) {
494513 * @param data
495514 * Data to digest
496515 * @return SHA-512 digest as a hex string
497- * @throws IOException On error reading from the stream
516+ * @since 1.4
498517 */
499- public static String sha512Hex (InputStream data ) throws IOException {
518+ public static String sha512Hex (String data ) {
500519 return new String (Hex .encodeHex (sha512 (data )));
501520 }
502521
@@ -517,8 +536,9 @@ public static String shaHex(byte[] data) {
517536 * @param data
518537 * Data to digest
519538 * @return SHA-1 digest as a hex string
539+ * @throws IOException On error reading from the stream
520540 */
521- public static String shaHex (String data ) {
541+ public static String shaHex (InputStream data ) throws IOException {
522542 return new String (Hex .encodeHex (sha (data )));
523543 }
524544
@@ -528,9 +548,8 @@ public static String shaHex(String data) {
528548 * @param data
529549 * Data to digest
530550 * @return SHA-1 digest as a hex string
531- * @throws IOException On error reading from the stream
532551 */
533- public static String shaHex (InputStream data ) throws IOException {
552+ public static String shaHex (String data ) {
534553 return new String (Hex .encodeHex (sha (data )));
535554 }
536555}
0 commit comments