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