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