View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.codec.digest;
19  
20  import java.io.BufferedInputStream;
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.RandomAccessFile;
26  import java.nio.ByteBuffer;
27  import java.nio.channels.FileChannel;
28  import java.nio.file.Files;
29  import java.nio.file.OpenOption;
30  import java.nio.file.Path;
31  import java.security.MessageDigest;
32  import java.security.NoSuchAlgorithmException;
33  
34  import org.apache.commons.codec.binary.Hex;
35  import org.apache.commons.codec.binary.StringUtils;
36  
37  /**
38   * Operations to simplify common {@link java.security.MessageDigest} tasks. This class is immutable and thread-safe. However the MessageDigest instances it
39   * creates generally won't be.
40   * <p>
41   * The {@link MessageDigestAlgorithms} class provides constants for standard digest algorithms that can be used with the {@link #getDigest(String)} method and
42   * other methods that require the Digest algorithm name.
43   * </p>
44   * <p>
45   * Note: The class has shorthand methods for all the algorithms present as standard in Java 6. This approach requires lots of methods for each algorithm, and
46   * quickly becomes unwieldy. The following code works with all algorithms:
47   * </p>
48   *
49   * <pre>
50   * import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224;
51   * ...
52   * byte [] digest = new DigestUtils(SHA_224).digest(dataToDigest);
53   * String hdigest = new DigestUtils(SHA_224).digestAsHex(new File("pom.xml"));
54   * </pre>
55   *
56   * @see MessageDigestAlgorithms
57   */
58  public class DigestUtils {
59  
60      /**
61       * Package-private for tests.
62       */
63      static final int BUFFER_SIZE = 1024;
64  
65      /**
66       * Reads through a byte array and returns the digest for the data. Provided for symmetry with other methods.
67       *
68       * @param messageDigest The MessageDigest to use (e.g. MD5)
69       * @param data          Data to digest
70       * @return the digest
71       * @since 1.11
72       */
73      public static byte[] digest(final MessageDigest messageDigest, final byte[] data) {
74          return messageDigest.digest(data);
75      }
76  
77      /**
78       * Reads through a ByteBuffer and returns the digest for the data
79       *
80       * @param messageDigest The MessageDigest to use (e.g. MD5)
81       * @param data          Data to digest
82       * @return the digest
83       * @since 1.11
84       */
85      public static byte[] digest(final MessageDigest messageDigest, final ByteBuffer data) {
86          messageDigest.update(data);
87          return messageDigest.digest();
88      }
89  
90      /**
91       * Reads through a File and returns the digest for the data
92       *
93       * @param messageDigest The MessageDigest to use (e.g. MD5)
94       * @param data          Data to digest
95       * @return the digest
96       * @throws IOException On error reading from the stream
97       * @since 1.11
98       */
99      public static byte[] digest(final MessageDigest messageDigest, final File data) throws IOException {
100         return updateDigest(messageDigest, data).digest();
101     }
102 
103     /**
104      * Reads through an InputStream and returns the digest for the data
105      *
106      * @param messageDigest The MessageDigest to use (e.g. MD5)
107      * @param data          Data to digest
108      * @return the digest
109      * @throws IOException On error reading from the stream
110      * @since 1.11 (was private)
111      */
112     public static byte[] digest(final MessageDigest messageDigest, final InputStream data) throws IOException {
113         return updateDigest(messageDigest, data).digest();
114     }
115 
116     /**
117      * Reads through a File and returns the digest for the data
118      *
119      * @param messageDigest The MessageDigest to use (e.g. MD5)
120      * @param data          Data to digest
121      * @param options       options How to open the file
122      * @return the digest
123      * @throws IOException On error reading from the stream
124      * @since 1.14
125      */
126     public static byte[] digest(final MessageDigest messageDigest, final Path data, final OpenOption... options) throws IOException {
127         return updateDigest(messageDigest, data, options).digest();
128     }
129 
130     /**
131      * Reads through a RandomAccessFile using non-blocking-io (NIO) and returns the digest for the data
132      *
133      * @param messageDigest The MessageDigest to use (e.g. MD5)
134      * @param data          Data to digest
135      * @return the digest
136      * @throws IOException On error reading from the stream
137      * @since 1.14
138      */
139     public static byte[] digest(final MessageDigest messageDigest, final RandomAccessFile data) throws IOException {
140         return updateDigest(messageDigest, data).digest();
141     }
142 
143     /**
144      * Gets a {@code MessageDigest} for the given {@code algorithm}.
145      *
146      * @param algorithm the name of the algorithm requested. See
147      *                  <a href="https://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA">Appendix A in the Java
148      *                  Cryptography Architecture Reference Guide</a> for information about standard algorithm names.
149      * @return A digest instance.
150      * @see MessageDigest#getInstance(String)
151      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
152      */
153     public static MessageDigest getDigest(final String algorithm) {
154         try {
155             return getMessageDigest(algorithm);
156         } catch (final NoSuchAlgorithmException e) {
157             throw new IllegalArgumentException(e);
158         }
159     }
160 
161     /**
162      * Gets a {@code MessageDigest} for the given {@code algorithm} or a default if there is a problem getting the algorithm.
163      *
164      * @param algorithm            the name of the algorithm requested. See
165      *                             <a href="https://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA"> Appendix A in the Java
166      *                             Cryptography Architecture Reference Guide</a> for information about standard algorithm names.
167      * @param defaultMessageDigest The default MessageDigest.
168      * @return A digest instance.
169      * @see MessageDigest#getInstance(String)
170      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
171      * @since 1.11
172      */
173     public static MessageDigest getDigest(final String algorithm, final MessageDigest defaultMessageDigest) {
174         try {
175             return getMessageDigest(algorithm);
176         } catch (final Exception e) {
177             return defaultMessageDigest;
178         }
179     }
180 
181     /**
182      * Gets an MD2 MessageDigest.
183      *
184      * @return An MD2 digest instance.
185      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should never happen because MD2 is a built-in algorithm
186      * @see MessageDigestAlgorithms#MD2
187      * @since 1.7
188      */
189     public static MessageDigest getMd2Digest() {
190         return getDigest(MessageDigestAlgorithms.MD2);
191     }
192 
193     /**
194      * Gets an MD5 MessageDigest.
195      *
196      * @return An MD5 digest instance.
197      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should never happen because MD5 is a built-in algorithm
198      * @see MessageDigestAlgorithms#MD5
199      */
200     public static MessageDigest getMd5Digest() {
201         return getDigest(MessageDigestAlgorithms.MD5);
202     }
203 
204     /**
205      * Gets a {@code MessageDigest} for the given {@code algorithm}.
206      *
207      * @param algorithm the name of the algorithm requested. See
208      *                  <a href="https://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA"> Appendix A in the Java
209      *                  Cryptography Architecture Reference Guide</a> for information about standard algorithm names.
210      * @return A digest instance.
211      * @see MessageDigest#getInstance(String)
212      * @throws NoSuchAlgorithmException if no Provider supports a MessageDigestSpi implementation for the specified algorithm.
213      */
214     private static MessageDigest getMessageDigest(final String algorithm) throws NoSuchAlgorithmException {
215         return MessageDigest.getInstance(algorithm);
216     }
217 
218     /**
219      * Gets an SHA-1 digest.
220      *
221      * @return An SHA-1 digest instance.
222      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-1 is a built-in algorithm
223      * @see MessageDigestAlgorithms#SHA_1
224      * @since 1.7
225      */
226     public static MessageDigest getSha1Digest() {
227         return getDigest(MessageDigestAlgorithms.SHA_1);
228     }
229 
230     /**
231      * Gets an SHA-256 digest.
232      *
233      * @return An SHA-256 digest instance.
234      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-256 is a built-in algorithm
235      * @see MessageDigestAlgorithms#SHA_256
236      */
237     public static MessageDigest getSha256Digest() {
238         return getDigest(MessageDigestAlgorithms.SHA_256);
239     }
240 
241     /**
242      * Gets an SHA3-224 digest.
243      *
244      * @return An SHA3-224 digest instance.
245      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should not happen on Oracle Java 9 and greater.
246      * @see MessageDigestAlgorithms#SHA3_224
247      * @since 1.12
248      */
249     public static MessageDigest getSha3_224Digest() {
250         return getDigest(MessageDigestAlgorithms.SHA3_224);
251     }
252 
253     /**
254      * Returns an SHA3-256 digest.
255      *
256      * @return An SHA3-256 digest instance.
257      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should not happen on Oracle Java 9 and greater.
258      * @see MessageDigestAlgorithms#SHA3_256
259      * @since 1.12
260      */
261     public static MessageDigest getSha3_256Digest() {
262         return getDigest(MessageDigestAlgorithms.SHA3_256);
263     }
264 
265     /**
266      * Gets an SHA3-384 digest.
267      *
268      * @return An SHA3-384 digest instance.
269      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should not happen on Oracle Java 9 and greater.
270      * @see MessageDigestAlgorithms#SHA3_384
271      * @since 1.12
272      */
273     public static MessageDigest getSha3_384Digest() {
274         return getDigest(MessageDigestAlgorithms.SHA3_384);
275     }
276 
277     /**
278      * Gets an SHA3-512 digest.
279      *
280      * @return An SHA3-512 digest instance.
281      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should not happen on Oracle Java 9 and greater.
282      * @see MessageDigestAlgorithms#SHA3_512
283      * @since 1.12
284      */
285     public static MessageDigest getSha3_512Digest() {
286         return getDigest(MessageDigestAlgorithms.SHA3_512);
287     }
288 
289     /**
290      * Gets an SHA-384 digest.
291      *
292      * @return An SHA-384 digest instance.
293      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-384 is a built-in algorithm
294      * @see MessageDigestAlgorithms#SHA_384
295      */
296     public static MessageDigest getSha384Digest() {
297         return getDigest(MessageDigestAlgorithms.SHA_384);
298     }
299 
300     /**
301      * Gets an SHA-512/224 digest.
302      *
303      * @return An SHA-512/224 digest instance.
304      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
305      * @see MessageDigestAlgorithms#SHA_512_224
306      */
307     public static MessageDigest getSha512_224Digest() {
308         return getDigest(MessageDigestAlgorithms.SHA_512_224);
309     }
310 
311     /**
312      * Gets an SHA-512/256 digest.
313      *
314      * @return An SHA-512/256 digest instance.
315      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
316      * @see MessageDigestAlgorithms#SHA_512_224
317      */
318     public static MessageDigest getSha512_256Digest() {
319         return getDigest(MessageDigestAlgorithms.SHA_512_256);
320     }
321 
322     /**
323      * Gets an SHA-512 digest.
324      *
325      * @return An SHA-512 digest instance.
326      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-512 is a built-in algorithm
327      * @see MessageDigestAlgorithms#SHA_512
328      */
329     public static MessageDigest getSha512Digest() {
330         return getDigest(MessageDigestAlgorithms.SHA_512);
331     }
332 
333     /**
334      * Gets an SHA-1 digest.
335      *
336      * @return An SHA-1 digest instance.
337      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught
338      * @deprecated (1.11) Use {@link #getSha1Digest()}
339      */
340     @Deprecated
341     public static MessageDigest getShaDigest() {
342         return getSha1Digest();
343     }
344 
345     /**
346      * Test whether the algorithm is supported.
347      *
348      * @param messageDigestAlgorithm the algorithm name
349      * @return {@code true} if the algorithm can be found
350      * @since 1.11
351      */
352     public static boolean isAvailable(final String messageDigestAlgorithm) {
353         return getDigest(messageDigestAlgorithm, null) != null;
354     }
355 
356     /**
357      * Calculates the MD2 digest and returns the value as a 16 element {@code byte[]}.
358      *
359      * @param data Data to digest
360      * @return MD2 digest
361      * @since 1.7
362      */
363     public static byte[] md2(final byte[] data) {
364         return getMd2Digest().digest(data);
365     }
366 
367     /**
368      * Calculates the MD2 digest and returns the value as a 16 element {@code byte[]}.
369      *
370      * @param data Data to digest
371      * @return MD2 digest
372      * @throws IOException On error reading from the stream
373      * @since 1.7
374      */
375     public static byte[] md2(final InputStream data) throws IOException {
376         return digest(getMd2Digest(), data);
377     }
378 
379     /**
380      * Calculates the MD2 digest and returns the value as a 16 element {@code byte[]}.
381      *
382      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
383      * @return MD2 digest
384      * @since 1.7
385      */
386     public static byte[] md2(final String data) {
387         return md2(StringUtils.getBytesUtf8(data));
388     }
389 
390     /**
391      * Calculates the MD2 digest and returns the value as a 32 character hexadecimal string.
392      *
393      * @param data Data to digest
394      * @return MD2 digest as a hexadecimal string
395      * @since 1.7
396      */
397     public static String md2Hex(final byte[] data) {
398         return Hex.encodeHexString(md2(data));
399     }
400 
401     /**
402      * Calculates the MD2 digest and returns the value as a 32 character hexadecimal string.
403      *
404      * @param data Data to digest
405      * @return MD2 digest as a hexadecimal string
406      * @throws IOException On error reading from the stream
407      * @since 1.7
408      */
409     public static String md2Hex(final InputStream data) throws IOException {
410         return Hex.encodeHexString(md2(data));
411     }
412 
413     /**
414      * Calculates the MD2 digest and returns the value as a 32 character hexadecimal string.
415      *
416      * @param data Data to digest
417      * @return MD2 digest as a hexadecimal string
418      * @since 1.7
419      */
420     public static String md2Hex(final String data) {
421         return Hex.encodeHexString(md2(data));
422     }
423 
424     /**
425      * Calculates the MD5 digest and returns the value as a 16 element {@code byte[]}.
426      *
427      * @param data Data to digest
428      * @return MD5 digest
429      */
430     public static byte[] md5(final byte[] data) {
431         return getMd5Digest().digest(data);
432     }
433 
434     /**
435      * Calculates the MD5 digest and returns the value as a 16 element {@code byte[]}.
436      *
437      * @param data Data to digest
438      * @return MD5 digest
439      * @throws IOException On error reading from the stream
440      * @since 1.4
441      */
442     public static byte[] md5(final InputStream data) throws IOException {
443         return digest(getMd5Digest(), data);
444     }
445 
446     /**
447      * Calculates the MD5 digest and returns the value as a 16 element {@code byte[]}.
448      *
449      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
450      * @return MD5 digest
451      */
452     public static byte[] md5(final String data) {
453         return md5(StringUtils.getBytesUtf8(data));
454     }
455 
456     /**
457      * Calculates the MD5 digest and returns the value as a 32 character hexadecimal string.
458      *
459      * @param data Data to digest
460      * @return MD5 digest as a hexadecimal string
461      */
462     public static String md5Hex(final byte[] data) {
463         return Hex.encodeHexString(md5(data));
464     }
465 
466     /**
467      * Calculates the MD5 digest and returns the value as a 32 character hexadecimal string.
468      *
469      * @param data Data to digest
470      * @return MD5 digest as a hexadecimal string
471      * @throws IOException On error reading from the stream
472      * @since 1.4
473      */
474     public static String md5Hex(final InputStream data) throws IOException {
475         return Hex.encodeHexString(md5(data));
476     }
477 
478     /**
479      * Calculates the MD5 digest and returns the value as a 32 character hexadecimal string.
480      *
481      * @param data Data to digest
482      * @return MD5 digest as a hexadecimal string
483      */
484     public static String md5Hex(final String data) {
485         return Hex.encodeHexString(md5(data));
486     }
487 
488     /**
489      * Calculates the SHA-1 digest and returns the value as a {@code byte[]}.
490      *
491      * @param data Data to digest
492      * @return SHA-1 digest
493      * @deprecated (1.11) Use {@link #sha1(byte[])}
494      */
495     @Deprecated
496     public static byte[] sha(final byte[] data) {
497         return sha1(data);
498     }
499 
500     /**
501      * Calculates the SHA-1 digest and returns the value as a {@code byte[]}.
502      *
503      * @param data Data to digest
504      * @return SHA-1 digest
505      * @throws IOException On error reading from the stream
506      * @since 1.4
507      * @deprecated (1.11) Use {@link #sha1(InputStream)}
508      */
509     @Deprecated
510     public static byte[] sha(final InputStream data) throws IOException {
511         return sha1(data);
512     }
513 
514     /**
515      * Calculates the SHA-1 digest and returns the value as a {@code byte[]}.
516      *
517      * @param data Data to digest
518      * @return SHA-1 digest
519      * @deprecated (1.11) Use {@link #sha1(String)}
520      */
521     @Deprecated
522     public static byte[] sha(final String data) {
523         return sha1(data);
524     }
525 
526     /**
527      * Calculates the SHA-1 digest and returns the value as a {@code byte[]}.
528      *
529      * @param data Data to digest
530      * @return SHA-1 digest
531      * @since 1.7
532      */
533     public static byte[] sha1(final byte[] data) {
534         return getSha1Digest().digest(data);
535     }
536 
537     /**
538      * Calculates the SHA-1 digest and returns the value as a {@code byte[]}.
539      *
540      * @param data Data to digest
541      * @return SHA-1 digest
542      * @throws IOException On error reading from the stream
543      * @since 1.7
544      */
545     public static byte[] sha1(final InputStream data) throws IOException {
546         return digest(getSha1Digest(), data);
547     }
548 
549     /**
550      * Calculates the SHA-1 digest and returns the value as a {@code byte[]}.
551      *
552      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
553      * @return SHA-1 digest
554      */
555     public static byte[] sha1(final String data) {
556         return sha1(StringUtils.getBytesUtf8(data));
557     }
558 
559     /**
560      * Calculates the SHA-1 digest and returns the value as a hexadecimal string.
561      *
562      * @param data Data to digest
563      * @return SHA-1 digest as a hexadecimal string
564      * @since 1.7
565      */
566     public static String sha1Hex(final byte[] data) {
567         return Hex.encodeHexString(sha1(data));
568     }
569 
570     /**
571      * Calculates the SHA-1 digest and returns the value as a hexadecimal string.
572      *
573      * @param data Data to digest
574      * @return SHA-1 digest as a hexadecimal string
575      * @throws IOException On error reading from the stream
576      * @since 1.7
577      */
578     public static String sha1Hex(final InputStream data) throws IOException {
579         return Hex.encodeHexString(sha1(data));
580     }
581 
582     /**
583      * Calculates the SHA-1 digest and returns the value as a hexadecimal string.
584      *
585      * @param data Data to digest
586      * @return SHA-1 digest as a hexadecimal string
587      * @since 1.7
588      */
589     public static String sha1Hex(final String data) {
590         return Hex.encodeHexString(sha1(data));
591     }
592 
593     /**
594      * Calculates the SHA-256 digest and returns the value as a {@code byte[]}.
595      *
596      * @param data Data to digest
597      * @return SHA-256 digest
598      * @since 1.4
599      */
600     public static byte[] sha256(final byte[] data) {
601         return getSha256Digest().digest(data);
602     }
603 
604     /**
605      * Calculates the SHA-256 digest and returns the value as a {@code byte[]}.
606      *
607      * @param data Data to digest
608      * @return SHA-256 digest
609      * @throws IOException On error reading from the stream
610      * @since 1.4
611      */
612     public static byte[] sha256(final InputStream data) throws IOException {
613         return digest(getSha256Digest(), data);
614     }
615 
616     /**
617      * Calculates the SHA-256 digest and returns the value as a {@code byte[]}.
618      *
619      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
620      * @return SHA-256 digest
621      * @since 1.4
622      */
623     public static byte[] sha256(final String data) {
624         return sha256(StringUtils.getBytesUtf8(data));
625     }
626 
627     /**
628      * Calculates the SHA-256 digest and returns the value as a hexadecimal string.
629      *
630      * @param data Data to digest
631      * @return SHA-256 digest as a hexadecimal string
632      * @since 1.4
633      */
634     public static String sha256Hex(final byte[] data) {
635         return Hex.encodeHexString(sha256(data));
636     }
637 
638     /**
639      * Calculates the SHA-256 digest and returns the value as a hexadecimal string.
640      *
641      * @param data Data to digest
642      * @return SHA-256 digest as a hexadecimal string
643      * @throws IOException On error reading from the stream
644      * @since 1.4
645      */
646     public static String sha256Hex(final InputStream data) throws IOException {
647         return Hex.encodeHexString(sha256(data));
648     }
649 
650     /**
651      * Calculates the SHA-256 digest and returns the value as a hexadecimal string.
652      *
653      * @param data Data to digest
654      * @return SHA-256 digest as a hexadecimal string
655      * @since 1.4
656      */
657     public static String sha256Hex(final String data) {
658         return Hex.encodeHexString(sha256(data));
659     }
660 
661     /**
662      * Calculates the SHA3-224 digest and returns the value as a {@code byte[]}.
663      *
664      * @param data Data to digest
665      * @return SHA3-224 digest
666      * @since 1.12
667      */
668     public static byte[] sha3_224(final byte[] data) {
669         return getSha3_224Digest().digest(data);
670     }
671 
672     /**
673      * Calculates the SHA3-224 digest and returns the value as a {@code byte[]}.
674      *
675      * @param data Data to digest
676      * @return SHA3-224 digest
677      * @throws IOException On error reading from the stream
678      * @since 1.12
679      */
680     public static byte[] sha3_224(final InputStream data) throws IOException {
681         return digest(getSha3_224Digest(), data);
682     }
683 
684     /**
685      * Calculates the SHA3-224 digest and returns the value as a {@code byte[]}.
686      *
687      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
688      * @return SHA3-224 digest
689      * @since 1.12
690      */
691     public static byte[] sha3_224(final String data) {
692         return sha3_224(StringUtils.getBytesUtf8(data));
693     }
694 
695     /**
696      * Calculates the SHA3-224 digest and returns the value as a hexadecimal string.
697      *
698      * @param data Data to digest
699      * @return SHA3-224 digest as a hexadecimal string
700      * @since 1.12
701      */
702     public static String sha3_224Hex(final byte[] data) {
703         return Hex.encodeHexString(sha3_224(data));
704     }
705 
706     /**
707      * Calculates the SHA3-224 digest and returns the value as a hexadecimal string.
708      *
709      * @param data Data to digest
710      * @return SHA3-224 digest as a hexadecimal string
711      * @throws IOException On error reading from the stream
712      * @since 1.12
713      */
714     public static String sha3_224Hex(final InputStream data) throws IOException {
715         return Hex.encodeHexString(sha3_224(data));
716     }
717 
718     /**
719      * Calculates the SHA3-224 digest and returns the value as a hexadecimal string.
720      *
721      * @param data Data to digest
722      * @return SHA3-224 digest as a hexadecimal string
723      * @since 1.12
724      */
725     public static String sha3_224Hex(final String data) {
726         return Hex.encodeHexString(sha3_224(data));
727     }
728 
729     /**
730      * Calculates the SHA3-256 digest and returns the value as a {@code byte[]}.
731      *
732      * @param data Data to digest
733      * @return SHA3-256 digest
734      * @since 1.12
735      */
736     public static byte[] sha3_256(final byte[] data) {
737         return getSha3_256Digest().digest(data);
738     }
739 
740     /**
741      * Calculates the SHA3-256 digest and returns the value as a {@code byte[]}.
742      *
743      * @param data Data to digest
744      * @return SHA3-256 digest
745      * @throws IOException On error reading from the stream
746      * @since 1.12
747      */
748     public static byte[] sha3_256(final InputStream data) throws IOException {
749         return digest(getSha3_256Digest(), data);
750     }
751 
752     /**
753      * Calculates the SHA3-256 digest and returns the value as a {@code byte[]}.
754      *
755      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
756      * @return SHA3-256 digest
757      * @since 1.12
758      */
759     public static byte[] sha3_256(final String data) {
760         return sha3_256(StringUtils.getBytesUtf8(data));
761     }
762 
763     /**
764      * Calculates the SHA3-256 digest and returns the value as a hexadecimal string.
765      *
766      * @param data Data to digest
767      * @return SHA3-256 digest as a hexadecimal string
768      * @since 1.12
769      */
770     public static String sha3_256Hex(final byte[] data) {
771         return Hex.encodeHexString(sha3_256(data));
772     }
773 
774     /**
775      * Calculates the SHA3-256 digest and returns the value as a hexadecimal string.
776      *
777      * @param data Data to digest
778      * @return SHA3-256 digest as a hexadecimal string
779      * @throws IOException On error reading from the stream
780      * @since 1.12
781      */
782     public static String sha3_256Hex(final InputStream data) throws IOException {
783         return Hex.encodeHexString(sha3_256(data));
784     }
785 
786     /**
787      * Calculates the SHA3-256 digest and returns the value as a hexadecimal string.
788      *
789      * @param data Data to digest
790      * @return SHA3-256 digest as a hexadecimal string
791      * @since 1.12
792      */
793     public static String sha3_256Hex(final String data) {
794         return Hex.encodeHexString(sha3_256(data));
795     }
796 
797     /**
798      * Calculates the SHA3-384 digest and returns the value as a {@code byte[]}.
799      *
800      * @param data Data to digest
801      * @return SHA3-384 digest
802      * @since 1.12
803      */
804     public static byte[] sha3_384(final byte[] data) {
805         return getSha3_384Digest().digest(data);
806     }
807 
808     /**
809      * Calculates the SHA3-384 digest and returns the value as a {@code byte[]}.
810      *
811      * @param data Data to digest
812      * @return SHA3-384 digest
813      * @throws IOException On error reading from the stream
814      * @since 1.12
815      */
816     public static byte[] sha3_384(final InputStream data) throws IOException {
817         return digest(getSha3_384Digest(), data);
818     }
819 
820     /**
821      * Calculates the SHA3-384 digest and returns the value as a {@code byte[]}.
822      *
823      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
824      * @return SHA3-384 digest
825      * @since 1.12
826      */
827     public static byte[] sha3_384(final String data) {
828         return sha3_384(StringUtils.getBytesUtf8(data));
829     }
830 
831     /**
832      * Calculates the SHA3-384 digest and returns the value as a hexadecimal string.
833      *
834      * @param data Data to digest
835      * @return SHA3-384 digest as a hexadecimal string
836      * @since 1.12
837      */
838     public static String sha3_384Hex(final byte[] data) {
839         return Hex.encodeHexString(sha3_384(data));
840     }
841 
842     /**
843      * Calculates the SHA3-384 digest and returns the value as a hexadecimal string.
844      *
845      * @param data Data to digest
846      * @return SHA3-384 digest as a hexadecimal string
847      * @throws IOException On error reading from the stream
848      * @since 1.12
849      */
850     public static String sha3_384Hex(final InputStream data) throws IOException {
851         return Hex.encodeHexString(sha3_384(data));
852     }
853 
854     /**
855      * Calculates the SHA3-384 digest and returns the value as a hexadecimal string.
856      *
857      * @param data Data to digest
858      * @return SHA3-384 digest as a hexadecimal string
859      * @since 1.12
860      */
861     public static String sha3_384Hex(final String data) {
862         return Hex.encodeHexString(sha3_384(data));
863     }
864 
865     /**
866      * Calculates the SHA3-512 digest and returns the value as a {@code byte[]}.
867      *
868      * @param data Data to digest
869      * @return SHA3-512 digest
870      * @since 1.12
871      */
872     public static byte[] sha3_512(final byte[] data) {
873         return getSha3_512Digest().digest(data);
874     }
875 
876     /**
877      * Calculates the SHA3-512 digest and returns the value as a {@code byte[]}.
878      *
879      * @param data Data to digest
880      * @return SHA3-512 digest
881      * @throws IOException On error reading from the stream
882      * @since 1.12
883      */
884     public static byte[] sha3_512(final InputStream data) throws IOException {
885         return digest(getSha3_512Digest(), data);
886     }
887 
888     /**
889      * Calculates the SHA3-512 digest and returns the value as a {@code byte[]}.
890      *
891      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
892      * @return SHA3-512 digest
893      * @since 1.12
894      */
895     public static byte[] sha3_512(final String data) {
896         return sha3_512(StringUtils.getBytesUtf8(data));
897     }
898 
899     /**
900      * Calculates the SHA3-512 digest and returns the value as a hexadecimal string.
901      *
902      * @param data Data to digest
903      * @return SHA3-512 digest as a hexadecimal string
904      * @since 1.12
905      */
906     public static String sha3_512Hex(final byte[] data) {
907         return Hex.encodeHexString(sha3_512(data));
908     }
909 
910     /**
911      * Calculates the SHA3-512 digest and returns the value as a hexadecimal string.
912      *
913      * @param data Data to digest
914      * @return SHA3-512 digest as a hexadecimal string
915      * @throws IOException On error reading from the stream
916      * @since 1.12
917      */
918     public static String sha3_512Hex(final InputStream data) throws IOException {
919         return Hex.encodeHexString(sha3_512(data));
920     }
921 
922     /**
923      * Calculates the SHA3-512 digest and returns the value as a hexadecimal string.
924      *
925      * @param data Data to digest
926      * @return SHA3-512 digest as a hexadecimal string
927      * @since 1.12
928      */
929     public static String sha3_512Hex(final String data) {
930         return Hex.encodeHexString(sha3_512(data));
931     }
932 
933     /**
934      * Calculates the SHA-384 digest and returns the value as a {@code byte[]}.
935      *
936      * @param data Data to digest
937      * @return SHA-384 digest
938      * @since 1.4
939      */
940     public static byte[] sha384(final byte[] data) {
941         return getSha384Digest().digest(data);
942     }
943 
944     /**
945      * Calculates the SHA-384 digest and returns the value as a {@code byte[]}.
946      *
947      * @param data Data to digest
948      * @return SHA-384 digest
949      * @throws IOException On error reading from the stream
950      * @since 1.4
951      */
952     public static byte[] sha384(final InputStream data) throws IOException {
953         return digest(getSha384Digest(), data);
954     }
955 
956     /**
957      * Calculates the SHA-384 digest and returns the value as a {@code byte[]}.
958      *
959      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
960      * @return SHA-384 digest
961      * @since 1.4
962      */
963     public static byte[] sha384(final String data) {
964         return sha384(StringUtils.getBytesUtf8(data));
965     }
966 
967     /**
968      * Calculates the SHA-384 digest and returns the value as a hexadecimal string.
969      *
970      * @param data Data to digest
971      * @return SHA-384 digest as a hexadecimal string
972      * @since 1.4
973      */
974     public static String sha384Hex(final byte[] data) {
975         return Hex.encodeHexString(sha384(data));
976     }
977 
978     /**
979      * Calculates the SHA-384 digest and returns the value as a hexadecimal string.
980      *
981      * @param data Data to digest
982      * @return SHA-384 digest as a hexadecimal string
983      * @throws IOException On error reading from the stream
984      * @since 1.4
985      */
986     public static String sha384Hex(final InputStream data) throws IOException {
987         return Hex.encodeHexString(sha384(data));
988     }
989 
990     /**
991      * Calculates the SHA-384 digest and returns the value as a hexadecimal string.
992      *
993      * @param data Data to digest
994      * @return SHA-384 digest as a hexadecimal string
995      * @since 1.4
996      */
997     public static String sha384Hex(final String data) {
998         return Hex.encodeHexString(sha384(data));
999     }
1000 
1001     /**
1002      * Calculates the SHA-512 digest and returns the value as a {@code byte[]}.
1003      *
1004      * @param data Data to digest
1005      * @return SHA-512 digest
1006      * @since 1.4
1007      */
1008     public static byte[] sha512(final byte[] data) {
1009         return getSha512Digest().digest(data);
1010     }
1011 
1012     /**
1013      * Calculates the SHA-512 digest and returns the value as a {@code byte[]}.
1014      *
1015      * @param data Data to digest
1016      * @return SHA-512 digest
1017      * @throws IOException On error reading from the stream
1018      * @since 1.4
1019      */
1020     public static byte[] sha512(final InputStream data) throws IOException {
1021         return digest(getSha512Digest(), data);
1022     }
1023 
1024     /**
1025      * Calculates the SHA-512 digest and returns the value as a {@code byte[]}.
1026      *
1027      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
1028      * @return SHA-512 digest
1029      * @since 1.4
1030      */
1031     public static byte[] sha512(final String data) {
1032         return sha512(StringUtils.getBytesUtf8(data));
1033     }
1034 
1035     /**
1036      * Calculates the SHA-512/224 digest and returns the value as a {@code byte[]}.
1037      *
1038      * @param data Data to digest
1039      * @return SHA-512/224 digest
1040      * @since 1.14
1041      */
1042     public static byte[] sha512_224(final byte[] data) {
1043         return getSha512_224Digest().digest(data);
1044     }
1045 
1046     /**
1047      * Calculates the SHA-512/224 digest and returns the value as a {@code byte[]}.
1048      *
1049      * @param data Data to digest
1050      * @return SHA-512/224 digest
1051      * @throws IOException On error reading from the stream
1052      * @since 1.14
1053      */
1054     public static byte[] sha512_224(final InputStream data) throws IOException {
1055         return digest(getSha512_224Digest(), data);
1056     }
1057 
1058     /**
1059      * Calculates the SHA-512/224 digest and returns the value as a {@code byte[]}.
1060      *
1061      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
1062      * @return SHA-512/224 digest
1063      * @since 1.14
1064      */
1065     public static byte[] sha512_224(final String data) {
1066         return sha512_224(StringUtils.getBytesUtf8(data));
1067     }
1068 
1069     /**
1070      * Calculates the SHA-512/224 digest and returns the value as a hexadecimal string.
1071      *
1072      * @param data Data to digest
1073      * @return SHA-512/224 digest as a hexadecimal string
1074      * @since 1.14
1075      */
1076     public static String sha512_224Hex(final byte[] data) {
1077         return Hex.encodeHexString(sha512_224(data));
1078     }
1079 
1080     /**
1081      * Calculates the SHA-512/224 digest and returns the value as a hexadecimal string.
1082      *
1083      * @param data Data to digest
1084      * @return SHA-512/224 digest as a hexadecimal string
1085      * @throws IOException On error reading from the stream
1086      * @since 1.14
1087      */
1088     public static String sha512_224Hex(final InputStream data) throws IOException {
1089         return Hex.encodeHexString(sha512_224(data));
1090     }
1091 
1092     /**
1093      * Calculates the SHA-512/224 digest and returns the value as a hexadecimal string.
1094      *
1095      * @param data Data to digest
1096      * @return SHA-512/224 digest as a hexadecimal string
1097      * @since 1.14
1098      */
1099     public static String sha512_224Hex(final String data) {
1100         return Hex.encodeHexString(sha512_224(data));
1101     }
1102 
1103     /**
1104      * Calculates the SHA-512/256 digest and returns the value as a {@code byte[]}.
1105      *
1106      * @param data Data to digest
1107      * @return SHA-512/256 digest
1108      * @since 1.14
1109      */
1110     public static byte[] sha512_256(final byte[] data) {
1111         return getSha512_256Digest().digest(data);
1112     }
1113 
1114     /**
1115      * Calculates the SHA-512/256 digest and returns the value as a {@code byte[]}.
1116      *
1117      * @param data Data to digest
1118      * @return SHA-512/256 digest
1119      * @throws IOException On error reading from the stream
1120      * @since 1.14
1121      */
1122     public static byte[] sha512_256(final InputStream data) throws IOException {
1123         return digest(getSha512_256Digest(), data);
1124     }
1125 
1126     /**
1127      * Calculates the SHA-512/256 digest and returns the value as a {@code byte[]}.
1128      *
1129      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
1130      * @return SHA-512/224 digest
1131      * @since 1.14
1132      */
1133     public static byte[] sha512_256(final String data) {
1134         return sha512_256(StringUtils.getBytesUtf8(data));
1135     }
1136 
1137     /**
1138      * Calculates the SHA-512/256 digest and returns the value as a hexadecimal string.
1139      *
1140      * @param data Data to digest
1141      * @return SHA-512/256 digest as a hexadecimal string
1142      * @since 1.14
1143      */
1144     public static String sha512_256Hex(final byte[] data) {
1145         return Hex.encodeHexString(sha512_256(data));
1146     }
1147 
1148     /**
1149      * Calculates the SHA-512/256 digest and returns the value as a hexadecimal string.
1150      *
1151      * @param data Data to digest
1152      * @return SHA-512/256 digest as a hexadecimal string
1153      * @throws IOException On error reading from the stream
1154      * @since 1.14
1155      */
1156     public static String sha512_256Hex(final InputStream data) throws IOException {
1157         return Hex.encodeHexString(sha512_256(data));
1158     }
1159 
1160     /**
1161      * Calculates the SHA-512/256 digest and returns the value as a hexadecimal string.
1162      *
1163      * @param data Data to digest
1164      * @return SHA-512/256 digest as a hexadecimal string
1165      * @since 1.14
1166      */
1167     public static String sha512_256Hex(final String data) {
1168         return Hex.encodeHexString(sha512_256(data));
1169     }
1170 
1171     /**
1172      * Calculates the SHA-512 digest and returns the value as a hexadecimal string.
1173      *
1174      * @param data Data to digest
1175      * @return SHA-512 digest as a hexadecimal string
1176      * @since 1.4
1177      */
1178     public static String sha512Hex(final byte[] data) {
1179         return Hex.encodeHexString(sha512(data));
1180     }
1181 
1182     /**
1183      * Calculates the SHA-512 digest and returns the value as a hexadecimal string.
1184      *
1185      * @param data Data to digest
1186      * @return SHA-512 digest as a hexadecimal string
1187      * @throws IOException On error reading from the stream
1188      * @since 1.4
1189      */
1190     public static String sha512Hex(final InputStream data) throws IOException {
1191         return Hex.encodeHexString(sha512(data));
1192     }
1193 
1194     /**
1195      * Calculates the SHA-512 digest and returns the value as a hexadecimal string.
1196      *
1197      * @param data Data to digest
1198      * @return SHA-512 digest as a hexadecimal string
1199      * @since 1.4
1200      */
1201     public static String sha512Hex(final String data) {
1202         return Hex.encodeHexString(sha512(data));
1203     }
1204 
1205     /**
1206      * Calculates the SHA-1 digest and returns the value as a hexadecimal string.
1207      *
1208      * @param data Data to digest
1209      * @return SHA-1 digest as a hexadecimal string
1210      * @deprecated (1.11) Use {@link #sha1Hex(byte[])}
1211      */
1212     @Deprecated
1213     public static String shaHex(final byte[] data) {
1214         return sha1Hex(data);
1215     }
1216 
1217     /**
1218      * Calculates the SHA-1 digest and returns the value as a hexadecimal string.
1219      *
1220      * @param data Data to digest
1221      * @return SHA-1 digest as a hexadecimal string
1222      * @throws IOException On error reading from the stream
1223      * @since 1.4
1224      * @deprecated (1.11) Use {@link #sha1Hex(InputStream)}
1225      */
1226     @Deprecated
1227     public static String shaHex(final InputStream data) throws IOException {
1228         return sha1Hex(data);
1229     }
1230 
1231     /**
1232      * Calculates the SHA-1 digest and returns the value as a hexadecimal string.
1233      *
1234      * @param data Data to digest
1235      * @return SHA-1 digest as a hexadecimal string
1236      * @deprecated (1.11) Use {@link #sha1Hex(String)}
1237      */
1238     @Deprecated
1239     public static String shaHex(final String data) {
1240         return sha1Hex(data);
1241     }
1242 
1243     /**
1244      * Updates the given {@link MessageDigest}.
1245      *
1246      * @param messageDigest the {@link MessageDigest} to update
1247      * @param valueToDigest the value to update the {@link MessageDigest} with
1248      * @return the updated {@link MessageDigest}
1249      * @since 1.7
1250      */
1251     public static MessageDigest updateDigest(final MessageDigest messageDigest, final byte[] valueToDigest) {
1252         messageDigest.update(valueToDigest);
1253         return messageDigest;
1254     }
1255 
1256     /**
1257      * Updates the given {@link MessageDigest}.
1258      *
1259      * @param messageDigest the {@link MessageDigest} to update
1260      * @param valueToDigest the value to update the {@link MessageDigest} with
1261      * @return the updated {@link MessageDigest}
1262      * @since 1.11
1263      */
1264     public static MessageDigest updateDigest(final MessageDigest messageDigest, final ByteBuffer valueToDigest) {
1265         messageDigest.update(valueToDigest);
1266         return messageDigest;
1267     }
1268 
1269     /**
1270      * Reads through a File and updates the digest for the data
1271      *
1272      * @param digest The MessageDigest to use (e.g. MD5)
1273      * @param data   Data to digest
1274      * @return the digest
1275      * @throws IOException On error reading from the stream
1276      * @since 1.11
1277      */
1278     public static MessageDigest updateDigest(final MessageDigest digest, final File data) throws IOException {
1279         try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(data))) {
1280             return updateDigest(digest, inputStream);
1281         }
1282     }
1283 
1284     /**
1285      * Reads through a RandomAccessFile and updates the digest for the data using non-blocking-io (NIO).
1286      *
1287      * TODO Decide if this should be public.
1288      *
1289      * @param digest The MessageDigest to use (e.g. MD5)
1290      * @param data   Data to digest
1291      * @return the digest
1292      * @throws IOException On error reading from the stream
1293      * @since 1.14
1294      */
1295     private static MessageDigest updateDigest(final MessageDigest digest, final FileChannel data) throws IOException {
1296         final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
1297         while (data.read(buffer) > 0) {
1298             buffer.flip();
1299             digest.update(buffer);
1300             buffer.clear();
1301         }
1302         return digest;
1303     }
1304 
1305     /**
1306      * Reads through an InputStream and updates the digest for the data
1307      *
1308      * @param digest      The MessageDigest to use (e.g. MD5)
1309      * @param inputStream Data to digest
1310      * @return the digest
1311      * @throws IOException On error reading from the stream
1312      * @since 1.8
1313      */
1314     public static MessageDigest updateDigest(final MessageDigest digest, final InputStream inputStream) throws IOException {
1315         final byte[] buffer = new byte[BUFFER_SIZE];
1316         int read = inputStream.read(buffer, 0, BUFFER_SIZE);
1317 
1318         while (read > -1) {
1319             digest.update(buffer, 0, read);
1320             read = inputStream.read(buffer, 0, BUFFER_SIZE);
1321         }
1322 
1323         return digest;
1324     }
1325 
1326     /**
1327      * Reads through a Path and updates the digest for the data
1328      *
1329      * @param digest  The MessageDigest to use (e.g. MD5)
1330      * @param path    Data to digest
1331      * @param options options How to open the file
1332      * @return the digest
1333      * @throws IOException On error reading from the stream
1334      * @since 1.14
1335      */
1336     public static MessageDigest updateDigest(final MessageDigest digest, final Path path, final OpenOption... options) throws IOException {
1337         try (BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(path, options))) {
1338             return updateDigest(digest, inputStream);
1339         }
1340     }
1341 
1342     /**
1343      * Reads through a RandomAccessFile and updates the digest for the data using non-blocking-io (NIO)
1344      *
1345      * @param digest The MessageDigest to use (e.g. MD5)
1346      * @param data   Data to digest
1347      * @return the digest
1348      * @throws IOException On error reading from the stream
1349      * @since 1.14
1350      */
1351     @SuppressWarnings("resource") // Closing RandomAccessFile closes the channel.
1352     public static MessageDigest updateDigest(final MessageDigest digest, final RandomAccessFile data) throws IOException {
1353         return updateDigest(digest, data.getChannel());
1354     }
1355 
1356     /**
1357      * Updates the given {@link MessageDigest} from a String (converted to bytes using UTF-8).
1358      * <p>
1359      * To update the digest using a different charset for the conversion, convert the String to a byte array using
1360      * {@link String#getBytes(java.nio.charset.Charset)} and pass that to the {@link DigestUtils#updateDigest(MessageDigest, byte[])} method
1361      *
1362      * @param messageDigest the {@link MessageDigest} to update
1363      * @param valueToDigest the value to update the {@link MessageDigest} with; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
1364      * @return the updated {@link MessageDigest}
1365      * @since 1.7
1366      */
1367     public static MessageDigest updateDigest(final MessageDigest messageDigest, final String valueToDigest) {
1368         messageDigest.update(StringUtils.getBytesUtf8(valueToDigest));
1369         return messageDigest;
1370     }
1371 
1372     private final MessageDigest messageDigest;
1373 
1374     /**
1375      * Preserves binary compatibility only. As for previous versions does not provide useful behavior
1376      *
1377      * @deprecated since 1.11; only useful to preserve binary compatibility
1378      */
1379     @Deprecated
1380     public DigestUtils() {
1381         this.messageDigest = null;
1382     }
1383 
1384     /**
1385      * Creates an instance using the provided {@link MessageDigest} parameter.
1386      *
1387      * This can then be used to create digests using methods such as {@link #digest(byte[])} and {@link #digestAsHex(File)}.
1388      *
1389      * @param digest the {@link MessageDigest} to use
1390      * @since 1.11
1391      */
1392     public DigestUtils(final MessageDigest digest) {
1393         this.messageDigest = digest;
1394     }
1395 
1396     /**
1397      * Creates an instance using the provided {@link MessageDigest} parameter.
1398      *
1399      * This can then be used to create digests using methods such as {@link #digest(byte[])} and {@link #digestAsHex(File)}.
1400      *
1401      * @param name the name of the {@link MessageDigest} to use
1402      * @see #getDigest(String)
1403      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
1404      * @since 1.11
1405      */
1406     public DigestUtils(final String name) {
1407         this(getDigest(name));
1408     }
1409 
1410     /**
1411      * Reads through a byte array and returns the digest for the data.
1412      *
1413      * @param data Data to digest
1414      * @return the digest
1415      * @since 1.11
1416      */
1417     public byte[] digest(final byte[] data) {
1418         return updateDigest(messageDigest, data).digest();
1419     }
1420 
1421     /**
1422      * Reads through a ByteBuffer and returns the digest for the data
1423      *
1424      * @param data Data to digest
1425      * @return the digest
1426      * @since 1.11
1427      */
1428     public byte[] digest(final ByteBuffer data) {
1429         return updateDigest(messageDigest, data).digest();
1430     }
1431 
1432     /**
1433      * Reads through a File and returns the digest for the data
1434      *
1435      * @param data Data to digest
1436      * @return the digest
1437      * @throws IOException On error reading from the stream
1438      * @since 1.11
1439      */
1440     public byte[] digest(final File data) throws IOException {
1441         return updateDigest(messageDigest, data).digest();
1442     }
1443 
1444     /**
1445      * Reads through an InputStream and returns the digest for the data
1446      *
1447      * @param data Data to digest
1448      * @return the digest
1449      * @throws IOException On error reading from the stream
1450      * @since 1.11
1451      */
1452     public byte[] digest(final InputStream data) throws IOException {
1453         return updateDigest(messageDigest, data).digest();
1454     }
1455 
1456     /**
1457      * Reads through a File and returns the digest for the data
1458      *
1459      * @param data    Data to digest
1460      * @param options options How to open the file
1461      * @return the digest
1462      * @throws IOException On error reading from the stream
1463      * @since 1.14
1464      */
1465     public byte[] digest(final Path data, final OpenOption... options) throws IOException {
1466         return updateDigest(messageDigest, data, options).digest();
1467     }
1468 
1469     /**
1470      * Reads through a byte array and returns the digest for the data.
1471      *
1472      * @param data Data to digest treated as UTF-8 string
1473      * @return the digest
1474      * @since 1.11
1475      */
1476     public byte[] digest(final String data) {
1477         return updateDigest(messageDigest, data).digest();
1478     }
1479 
1480     /**
1481      * Reads through a byte array and returns the digest for the data.
1482      *
1483      * @param data Data to digest
1484      * @return the digest as a hexadecimal string
1485      * @since 1.11
1486      */
1487     public String digestAsHex(final byte[] data) {
1488         return Hex.encodeHexString(digest(data));
1489     }
1490 
1491     /**
1492      * Reads through a ByteBuffer and returns the digest for the data
1493      *
1494      * @param data Data to digest
1495      * @return the digest as a hexadecimal string
1496      * @since 1.11
1497      */
1498     public String digestAsHex(final ByteBuffer data) {
1499         return Hex.encodeHexString(digest(data));
1500     }
1501 
1502     /**
1503      * Reads through a File and returns the digest for the data
1504      *
1505      * @param data Data to digest
1506      * @return the digest as a hexadecimal string
1507      * @throws IOException On error reading from the stream
1508      * @since 1.11
1509      */
1510     public String digestAsHex(final File data) throws IOException {
1511         return Hex.encodeHexString(digest(data));
1512     }
1513 
1514     /**
1515      * Reads through an InputStream and returns the digest for the data
1516      *
1517      * @param data Data to digest
1518      * @return the digest as a hexadecimal string
1519      * @throws IOException On error reading from the stream
1520      * @since 1.11
1521      */
1522     public String digestAsHex(final InputStream data) throws IOException {
1523         return Hex.encodeHexString(digest(data));
1524     }
1525 
1526     /**
1527      * Reads through a File and returns the digest for the data
1528      *
1529      * @param data    Data to digest
1530      * @param options options How to open the file
1531      * @return the digest as a hexadecimal string
1532      * @throws IOException On error reading from the stream
1533      * @since 1.11
1534      */
1535     public String digestAsHex(final Path data, final OpenOption... options) throws IOException {
1536         return Hex.encodeHexString(digest(data, options));
1537     }
1538 
1539     /**
1540      * Reads through a byte array and returns the digest for the data.
1541      *
1542      * @param data Data to digest treated as UTF-8 string
1543      * @return the digest as a hexadecimal string
1544      * @since 1.11
1545      */
1546     public String digestAsHex(final String data) {
1547         return Hex.encodeHexString(digest(data));
1548     }
1549 
1550     /**
1551      * Returns the message digest instance.
1552      *
1553      * @return the message digest instance
1554      * @since 1.11
1555      */
1556     public MessageDigest getMessageDigest() {
1557         return messageDigest;
1558     }
1559 
1560 }