001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.codec.digest; 019 020import java.io.BufferedInputStream; 021import java.io.File; 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.RandomAccessFile; 025import java.nio.ByteBuffer; 026import java.nio.channels.FileChannel; 027import java.nio.file.Files; 028import java.nio.file.OpenOption; 029import java.nio.file.Path; 030import java.security.MessageDigest; 031import java.security.NoSuchAlgorithmException; 032 033import org.apache.commons.codec.binary.Hex; 034import org.apache.commons.codec.binary.StringUtils; 035 036/** 037 * Operations to simplify common {@link java.security.MessageDigest} tasks. This class is immutable and thread-safe. However the MessageDigest instances it 038 * creates generally won't be. 039 * <p> 040 * The {@link MessageDigestAlgorithms} class provides constants for standard digest algorithms that can be used with the {@link #getDigest(String)} method and 041 * other methods that require the Digest algorithm name. 042 * </p> 043 * <p> 044 * 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 045 * quickly becomes unwieldy. The following code works with all algorithms: 046 * </p> 047 * 048 * <pre> 049 * import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224; 050 * ... 051 * byte [] digest = new DigestUtils(SHA_224).digest(dataToDigest); 052 * String hdigest = new DigestUtils(SHA_224).digestAsHex(new File("pom.xml")); 053 * </pre> 054 * 055 * @see MessageDigestAlgorithms 056 */ 057public class DigestUtils { 058 059 /** 060 * Package-private for tests. 061 */ 062 static final int BUFFER_SIZE = 1024; 063 064 /** 065 * Reads through a byte array and returns the digest for the data. Provided for symmetry with other methods. 066 * 067 * @param messageDigest The MessageDigest to use (for example MD5) 068 * @param data Data to digest 069 * @return the digest 070 * @since 1.11 071 */ 072 public static byte[] digest(final MessageDigest messageDigest, final byte[] data) { 073 return messageDigest.digest(data); 074 } 075 076 /** 077 * Reads through a ByteBuffer and returns the digest for the data 078 * 079 * @param messageDigest The MessageDigest to use (for example MD5) 080 * @param data Data to digest 081 * @return the digest 082 * @since 1.11 083 */ 084 public static byte[] digest(final MessageDigest messageDigest, final ByteBuffer data) { 085 messageDigest.update(data); 086 return messageDigest.digest(); 087 } 088 089 /** 090 * Reads through a File and returns the digest for the data 091 * 092 * @param messageDigest The MessageDigest to use (for example MD5) 093 * @param data Data to digest 094 * @return the digest 095 * @throws IOException On error reading from the stream 096 * @since 1.11 097 */ 098 public static byte[] digest(final MessageDigest messageDigest, final File data) throws IOException { 099 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 * Test whether the algorithm is supported. 346 * 347 * @param messageDigestAlgorithm the algorithm name 348 * @return {@code true} if the algorithm can be found 349 * @since 1.11 350 */ 351 public static boolean isAvailable(final String messageDigestAlgorithm) { 352 return getDigest(messageDigestAlgorithm, null) != null; 353 } 354 355 /** 356 * Calculates the MD2 digest and returns the value as a 16 element {@code byte[]}. 357 * 358 * @param data Data to digest 359 * @return MD2 digest 360 * @since 1.7 361 */ 362 public static byte[] md2(final byte[] data) { 363 return getMd2Digest().digest(data); 364 } 365 366 /** 367 * Calculates the MD2 digest and returns the value as a 16 element {@code byte[]}. 368 * 369 * @param data Data to digest 370 * @return MD2 digest 371 * @throws IOException On error reading from the stream 372 * @since 1.7 373 */ 374 public static byte[] md2(final InputStream data) throws IOException { 375 return digest(getMd2Digest(), data); 376 } 377 378 /** 379 * Calculates the MD2 digest and returns the value as a 16 element {@code byte[]}. 380 * 381 * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 382 * @return MD2 digest 383 * @since 1.7 384 */ 385 public static byte[] md2(final String data) { 386 return md2(StringUtils.getBytesUtf8(data)); 387 } 388 389 /** 390 * Calculates the MD2 digest and returns the value as a 32 character hexadecimal string. 391 * 392 * @param data Data to digest 393 * @return MD2 digest as a hexadecimal string 394 * @since 1.7 395 */ 396 public static String md2Hex(final byte[] data) { 397 return Hex.encodeHexString(md2(data)); 398 } 399 400 /** 401 * Calculates the MD2 digest and returns the value as a 32 character hexadecimal string. 402 * 403 * @param data Data to digest 404 * @return MD2 digest as a hexadecimal string 405 * @throws IOException On error reading from the stream 406 * @since 1.7 407 */ 408 public static String md2Hex(final InputStream data) throws IOException { 409 return Hex.encodeHexString(md2(data)); 410 } 411 412 /** 413 * Calculates the MD2 digest and returns the value as a 32 character hexadecimal string. 414 * 415 * @param data Data to digest 416 * @return MD2 digest as a hexadecimal string 417 * @since 1.7 418 */ 419 public static String md2Hex(final String data) { 420 return Hex.encodeHexString(md2(data)); 421 } 422 423 /** 424 * Calculates the MD5 digest and returns the value as a 16 element {@code byte[]}. 425 * 426 * @param data Data to digest 427 * @return MD5 digest 428 */ 429 public static byte[] md5(final byte[] data) { 430 return getMd5Digest().digest(data); 431 } 432 433 /** 434 * Calculates the MD5 digest and returns the value as a 16 element {@code byte[]}. 435 * 436 * @param data Data to digest 437 * @return MD5 digest 438 * @throws IOException On error reading from the stream 439 * @since 1.4 440 */ 441 public static byte[] md5(final InputStream data) throws IOException { 442 return digest(getMd5Digest(), data); 443 } 444 445 /** 446 * Calculates the MD5 digest and returns the value as a 16 element {@code byte[]}. 447 * 448 * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 449 * @return MD5 digest 450 */ 451 public static byte[] md5(final String data) { 452 return md5(StringUtils.getBytesUtf8(data)); 453 } 454 455 /** 456 * Calculates the MD5 digest and returns the value as a 32 character hexadecimal string. 457 * 458 * @param data Data to digest 459 * @return MD5 digest as a hexadecimal string 460 */ 461 public static String md5Hex(final byte[] data) { 462 return Hex.encodeHexString(md5(data)); 463 } 464 465 /** 466 * Calculates the MD5 digest and returns the value as a 32 character hexadecimal string. 467 * 468 * @param data Data to digest 469 * @return MD5 digest as a hexadecimal string 470 * @throws IOException On error reading from the stream 471 * @since 1.4 472 */ 473 public static String md5Hex(final InputStream data) throws IOException { 474 return Hex.encodeHexString(md5(data)); 475 } 476 477 /** 478 * Calculates the MD5 digest and returns the value as a 32 character hexadecimal string. 479 * 480 * @param data Data to digest 481 * @return MD5 digest as a hexadecimal string 482 */ 483 public static String md5Hex(final String data) { 484 return Hex.encodeHexString(md5(data)); 485 } 486 487 /** 488 * Calculates the SHA-1 digest and returns the value as a {@code byte[]}. 489 * 490 * @param data Data to digest 491 * @return SHA-1 digest 492 * @deprecated (1.11) Use {@link #sha1(byte[])} 493 */ 494 @Deprecated 495 public static byte[] sha(final byte[] data) { 496 return sha1(data); 497 } 498 499 /** 500 * Calculates the SHA-1 digest and returns the value as a {@code byte[]}. 501 * 502 * @param data Data to digest 503 * @return SHA-1 digest 504 * @throws IOException On error reading from the stream 505 * @since 1.4 506 * @deprecated (1.11) Use {@link #sha1(InputStream)} 507 */ 508 @Deprecated 509 public static byte[] sha(final InputStream data) throws IOException { 510 return sha1(data); 511 } 512 513 /** 514 * Calculates the SHA-1 digest and returns the value as a {@code byte[]}. 515 * 516 * @param data Data to digest 517 * @return SHA-1 digest 518 * @deprecated (1.11) Use {@link #sha1(String)} 519 */ 520 @Deprecated 521 public static byte[] sha(final String data) { 522 return sha1(data); 523 } 524 525 /** 526 * Calculates the SHA-1 digest and returns the value as a {@code byte[]}. 527 * 528 * @param data Data to digest 529 * @return SHA-1 digest 530 * @since 1.7 531 */ 532 public static byte[] sha1(final byte[] data) { 533 return getSha1Digest().digest(data); 534 } 535 536 /** 537 * Calculates the SHA-1 digest and returns the value as a {@code byte[]}. 538 * 539 * @param data Data to digest 540 * @return SHA-1 digest 541 * @throws IOException On error reading from the stream 542 * @since 1.7 543 */ 544 public static byte[] sha1(final InputStream data) throws IOException { 545 return digest(getSha1Digest(), data); 546 } 547 548 /** 549 * Calculates the SHA-1 digest and returns the value as a {@code byte[]}. 550 * 551 * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 552 * @return SHA-1 digest 553 */ 554 public static byte[] sha1(final String data) { 555 return sha1(StringUtils.getBytesUtf8(data)); 556 } 557 558 /** 559 * Calculates the SHA-1 digest and returns the value as a hexadecimal string. 560 * 561 * @param data Data to digest 562 * @return SHA-1 digest as a hexadecimal string 563 * @since 1.7 564 */ 565 public static String sha1Hex(final byte[] data) { 566 return Hex.encodeHexString(sha1(data)); 567 } 568 569 /** 570 * Calculates the SHA-1 digest and returns the value as a hexadecimal string. 571 * 572 * @param data Data to digest 573 * @return SHA-1 digest as a hexadecimal string 574 * @throws IOException On error reading from the stream 575 * @since 1.7 576 */ 577 public static String sha1Hex(final InputStream data) throws IOException { 578 return Hex.encodeHexString(sha1(data)); 579 } 580 581 /** 582 * Calculates the SHA-1 digest and returns the value as a hexadecimal string. 583 * 584 * @param data Data to digest 585 * @return SHA-1 digest as a hexadecimal string 586 * @since 1.7 587 */ 588 public static String sha1Hex(final String data) { 589 return Hex.encodeHexString(sha1(data)); 590 } 591 592 /** 593 * Calculates the SHA-256 digest and returns the value as a {@code byte[]}. 594 * 595 * @param data Data to digest 596 * @return SHA-256 digest 597 * @since 1.4 598 */ 599 public static byte[] sha256(final byte[] data) { 600 return getSha256Digest().digest(data); 601 } 602 603 /** 604 * Calculates the SHA-256 digest and returns the value as a {@code byte[]}. 605 * 606 * @param data Data to digest 607 * @return SHA-256 digest 608 * @throws IOException On error reading from the stream 609 * @since 1.4 610 */ 611 public static byte[] sha256(final InputStream data) throws IOException { 612 return digest(getSha256Digest(), data); 613 } 614 615 /** 616 * Calculates the SHA-256 digest and returns the value as a {@code byte[]}. 617 * 618 * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 619 * @return SHA-256 digest 620 * @since 1.4 621 */ 622 public static byte[] sha256(final String data) { 623 return sha256(StringUtils.getBytesUtf8(data)); 624 } 625 626 /** 627 * Calculates the SHA-256 digest and returns the value as a hexadecimal string. 628 * 629 * @param data Data to digest 630 * @return SHA-256 digest as a hexadecimal string 631 * @since 1.4 632 */ 633 public static String sha256Hex(final byte[] data) { 634 return Hex.encodeHexString(sha256(data)); 635 } 636 637 /** 638 * Calculates the SHA-256 digest and returns the value as a hexadecimal string. 639 * 640 * @param data Data to digest 641 * @return SHA-256 digest as a hexadecimal string 642 * @throws IOException On error reading from the stream 643 * @since 1.4 644 */ 645 public static String sha256Hex(final InputStream data) throws IOException { 646 return Hex.encodeHexString(sha256(data)); 647 } 648 649 /** 650 * Calculates the SHA-256 digest and returns the value as a hexadecimal string. 651 * 652 * @param data Data to digest 653 * @return SHA-256 digest as a hexadecimal string 654 * @since 1.4 655 */ 656 public static String sha256Hex(final String data) { 657 return Hex.encodeHexString(sha256(data)); 658 } 659 660 /** 661 * Calculates the SHA3-224 digest and returns the value as a {@code byte[]}. 662 * 663 * @param data Data to digest 664 * @return SHA3-224 digest 665 * @since 1.12 666 */ 667 public static byte[] sha3_224(final byte[] data) { 668 return getSha3_224Digest().digest(data); 669 } 670 671 /** 672 * Calculates the SHA3-224 digest and returns the value as a {@code byte[]}. 673 * 674 * @param data Data to digest 675 * @return SHA3-224 digest 676 * @throws IOException On error reading from the stream 677 * @since 1.12 678 */ 679 public static byte[] sha3_224(final InputStream data) throws IOException { 680 return digest(getSha3_224Digest(), data); 681 } 682 683 /** 684 * Calculates the SHA3-224 digest and returns the value as a {@code byte[]}. 685 * 686 * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 687 * @return SHA3-224 digest 688 * @since 1.12 689 */ 690 public static byte[] sha3_224(final String data) { 691 return sha3_224(StringUtils.getBytesUtf8(data)); 692 } 693 694 /** 695 * Calculates the SHA3-224 digest and returns the value as a hexadecimal string. 696 * 697 * @param data Data to digest 698 * @return SHA3-224 digest as a hexadecimal string 699 * @since 1.12 700 */ 701 public static String sha3_224Hex(final byte[] data) { 702 return Hex.encodeHexString(sha3_224(data)); 703 } 704 705 /** 706 * Calculates the SHA3-224 digest and returns the value as a hexadecimal string. 707 * 708 * @param data Data to digest 709 * @return SHA3-224 digest as a hexadecimal string 710 * @throws IOException On error reading from the stream 711 * @since 1.12 712 */ 713 public static String sha3_224Hex(final InputStream data) throws IOException { 714 return Hex.encodeHexString(sha3_224(data)); 715 } 716 717 /** 718 * Calculates the SHA3-224 digest and returns the value as a hexadecimal string. 719 * 720 * @param data Data to digest 721 * @return SHA3-224 digest as a hexadecimal string 722 * @since 1.12 723 */ 724 public static String sha3_224Hex(final String data) { 725 return Hex.encodeHexString(sha3_224(data)); 726 } 727 728 /** 729 * Calculates the SHA3-256 digest and returns the value as a {@code byte[]}. 730 * 731 * @param data Data to digest 732 * @return SHA3-256 digest 733 * @since 1.12 734 */ 735 public static byte[] sha3_256(final byte[] data) { 736 return getSha3_256Digest().digest(data); 737 } 738 739 /** 740 * Calculates the SHA3-256 digest and returns the value as a {@code byte[]}. 741 * 742 * @param data Data to digest 743 * @return SHA3-256 digest 744 * @throws IOException On error reading from the stream 745 * @since 1.12 746 */ 747 public static byte[] sha3_256(final InputStream data) throws IOException { 748 return digest(getSha3_256Digest(), data); 749 } 750 751 /** 752 * Calculates the SHA3-256 digest and returns the value as a {@code byte[]}. 753 * 754 * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 755 * @return SHA3-256 digest 756 * @since 1.12 757 */ 758 public static byte[] sha3_256(final String data) { 759 return sha3_256(StringUtils.getBytesUtf8(data)); 760 } 761 762 /** 763 * Calculates the SHA3-256 digest and returns the value as a hexadecimal string. 764 * 765 * @param data Data to digest 766 * @return SHA3-256 digest as a hexadecimal string 767 * @since 1.12 768 */ 769 public static String sha3_256Hex(final byte[] data) { 770 return Hex.encodeHexString(sha3_256(data)); 771 } 772 773 /** 774 * Calculates the SHA3-256 digest and returns the value as a hexadecimal string. 775 * 776 * @param data Data to digest 777 * @return SHA3-256 digest as a hexadecimal string 778 * @throws IOException On error reading from the stream 779 * @since 1.12 780 */ 781 public static String sha3_256Hex(final InputStream data) throws IOException { 782 return Hex.encodeHexString(sha3_256(data)); 783 } 784 785 /** 786 * Calculates the SHA3-256 digest and returns the value as a hexadecimal string. 787 * 788 * @param data Data to digest 789 * @return SHA3-256 digest as a hexadecimal string 790 * @since 1.12 791 */ 792 public static String sha3_256Hex(final String data) { 793 return Hex.encodeHexString(sha3_256(data)); 794 } 795 796 /** 797 * Calculates the SHA3-384 digest and returns the value as a {@code byte[]}. 798 * 799 * @param data Data to digest 800 * @return SHA3-384 digest 801 * @since 1.12 802 */ 803 public static byte[] sha3_384(final byte[] data) { 804 return getSha3_384Digest().digest(data); 805 } 806 807 /** 808 * Calculates the SHA3-384 digest and returns the value as a {@code byte[]}. 809 * 810 * @param data Data to digest 811 * @return SHA3-384 digest 812 * @throws IOException On error reading from the stream 813 * @since 1.12 814 */ 815 public static byte[] sha3_384(final InputStream data) throws IOException { 816 return digest(getSha3_384Digest(), data); 817 } 818 819 /** 820 * Calculates the SHA3-384 digest and returns the value as a {@code byte[]}. 821 * 822 * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 823 * @return SHA3-384 digest 824 * @since 1.12 825 */ 826 public static byte[] sha3_384(final String data) { 827 return sha3_384(StringUtils.getBytesUtf8(data)); 828 } 829 830 /** 831 * Calculates the SHA3-384 digest and returns the value as a hexadecimal string. 832 * 833 * @param data Data to digest 834 * @return SHA3-384 digest as a hexadecimal string 835 * @since 1.12 836 */ 837 public static String sha3_384Hex(final byte[] data) { 838 return Hex.encodeHexString(sha3_384(data)); 839 } 840 841 /** 842 * Calculates the SHA3-384 digest and returns the value as a hexadecimal string. 843 * 844 * @param data Data to digest 845 * @return SHA3-384 digest as a hexadecimal string 846 * @throws IOException On error reading from the stream 847 * @since 1.12 848 */ 849 public static String sha3_384Hex(final InputStream data) throws IOException { 850 return Hex.encodeHexString(sha3_384(data)); 851 } 852 853 /** 854 * Calculates the SHA3-384 digest and returns the value as a hexadecimal string. 855 * 856 * @param data Data to digest 857 * @return SHA3-384 digest as a hexadecimal string 858 * @since 1.12 859 */ 860 public static String sha3_384Hex(final String data) { 861 return Hex.encodeHexString(sha3_384(data)); 862 } 863 864 /** 865 * Calculates the SHA3-512 digest and returns the value as a {@code byte[]}. 866 * 867 * @param data Data to digest 868 * @return SHA3-512 digest 869 * @since 1.12 870 */ 871 public static byte[] sha3_512(final byte[] data) { 872 return getSha3_512Digest().digest(data); 873 } 874 875 /** 876 * Calculates the SHA3-512 digest and returns the value as a {@code byte[]}. 877 * 878 * @param data Data to digest 879 * @return SHA3-512 digest 880 * @throws IOException On error reading from the stream 881 * @since 1.12 882 */ 883 public static byte[] sha3_512(final InputStream data) throws IOException { 884 return digest(getSha3_512Digest(), data); 885 } 886 887 /** 888 * Calculates the SHA3-512 digest and returns the value as a {@code byte[]}. 889 * 890 * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 891 * @return SHA3-512 digest 892 * @since 1.12 893 */ 894 public static byte[] sha3_512(final String data) { 895 return sha3_512(StringUtils.getBytesUtf8(data)); 896 } 897 898 /** 899 * Calculates the SHA3-512 digest and returns the value as a hexadecimal string. 900 * 901 * @param data Data to digest 902 * @return SHA3-512 digest as a hexadecimal string 903 * @since 1.12 904 */ 905 public static String sha3_512Hex(final byte[] data) { 906 return Hex.encodeHexString(sha3_512(data)); 907 } 908 909 /** 910 * Calculates the SHA3-512 digest and returns the value as a hexadecimal string. 911 * 912 * @param data Data to digest 913 * @return SHA3-512 digest as a hexadecimal string 914 * @throws IOException On error reading from the stream 915 * @since 1.12 916 */ 917 public static String sha3_512Hex(final InputStream data) throws IOException { 918 return Hex.encodeHexString(sha3_512(data)); 919 } 920 921 /** 922 * Calculates the SHA3-512 digest and returns the value as a hexadecimal string. 923 * 924 * @param data Data to digest 925 * @return SHA3-512 digest as a hexadecimal string 926 * @since 1.12 927 */ 928 public static String sha3_512Hex(final String data) { 929 return Hex.encodeHexString(sha3_512(data)); 930 } 931 932 /** 933 * Calculates the SHA-384 digest and returns the value as a {@code byte[]}. 934 * 935 * @param data Data to digest 936 * @return SHA-384 digest 937 * @since 1.4 938 */ 939 public static byte[] sha384(final byte[] data) { 940 return getSha384Digest().digest(data); 941 } 942 943 /** 944 * Calculates the SHA-384 digest and returns the value as a {@code byte[]}. 945 * 946 * @param data Data to digest 947 * @return SHA-384 digest 948 * @throws IOException On error reading from the stream 949 * @since 1.4 950 */ 951 public static byte[] sha384(final InputStream data) throws IOException { 952 return digest(getSha384Digest(), data); 953 } 954 955 /** 956 * Calculates the SHA-384 digest and returns the value as a {@code byte[]}. 957 * 958 * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 959 * @return SHA-384 digest 960 * @since 1.4 961 */ 962 public static byte[] sha384(final String data) { 963 return sha384(StringUtils.getBytesUtf8(data)); 964 } 965 966 /** 967 * Calculates the SHA-384 digest and returns the value as a hexadecimal string. 968 * 969 * @param data Data to digest 970 * @return SHA-384 digest as a hexadecimal string 971 * @since 1.4 972 */ 973 public static String sha384Hex(final byte[] data) { 974 return Hex.encodeHexString(sha384(data)); 975 } 976 977 /** 978 * Calculates the SHA-384 digest and returns the value as a hexadecimal string. 979 * 980 * @param data Data to digest 981 * @return SHA-384 digest as a hexadecimal string 982 * @throws IOException On error reading from the stream 983 * @since 1.4 984 */ 985 public static String sha384Hex(final InputStream data) throws IOException { 986 return Hex.encodeHexString(sha384(data)); 987 } 988 989 /** 990 * Calculates the SHA-384 digest and returns the value as a hexadecimal string. 991 * 992 * @param data Data to digest 993 * @return SHA-384 digest as a hexadecimal string 994 * @since 1.4 995 */ 996 public static String sha384Hex(final String data) { 997 return Hex.encodeHexString(sha384(data)); 998 } 999 1000 /** 1001 * Calculates the SHA-512 digest and returns the value as a {@code byte[]}. 1002 * 1003 * @param data Data to digest 1004 * @return SHA-512 digest 1005 * @since 1.4 1006 */ 1007 public static byte[] sha512(final byte[] data) { 1008 return getSha512Digest().digest(data); 1009 } 1010 1011 /** 1012 * Calculates the SHA-512 digest and returns the value as a {@code byte[]}. 1013 * 1014 * @param data Data to digest 1015 * @return SHA-512 digest 1016 * @throws IOException On error reading from the stream 1017 * @since 1.4 1018 */ 1019 public static byte[] sha512(final InputStream data) throws IOException { 1020 return digest(getSha512Digest(), data); 1021 } 1022 1023 /** 1024 * Calculates the SHA-512 digest and returns the value as a {@code byte[]}. 1025 * 1026 * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 1027 * @return SHA-512 digest 1028 * @since 1.4 1029 */ 1030 public static byte[] sha512(final String data) { 1031 return sha512(StringUtils.getBytesUtf8(data)); 1032 } 1033 1034 /** 1035 * Calculates the SHA-512/224 digest and returns the value as a {@code byte[]}. 1036 * 1037 * @param data Data to digest 1038 * @return SHA-512/224 digest 1039 * @since 1.14 1040 */ 1041 public static byte[] sha512_224(final byte[] data) { 1042 return getSha512_224Digest().digest(data); 1043 } 1044 1045 /** 1046 * Calculates the SHA-512/224 digest and returns the value as a {@code byte[]}. 1047 * 1048 * @param data Data to digest 1049 * @return SHA-512/224 digest 1050 * @throws IOException On error reading from the stream 1051 * @since 1.14 1052 */ 1053 public static byte[] sha512_224(final InputStream data) throws IOException { 1054 return digest(getSha512_224Digest(), data); 1055 } 1056 1057 /** 1058 * Calculates the SHA-512/224 digest and returns the value as a {@code byte[]}. 1059 * 1060 * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 1061 * @return SHA-512/224 digest 1062 * @since 1.14 1063 */ 1064 public static byte[] sha512_224(final String data) { 1065 return sha512_224(StringUtils.getBytesUtf8(data)); 1066 } 1067 1068 /** 1069 * Calculates the SHA-512/224 digest and returns the value as a hexadecimal string. 1070 * 1071 * @param data Data to digest 1072 * @return SHA-512/224 digest as a hexadecimal string 1073 * @since 1.14 1074 */ 1075 public static String sha512_224Hex(final byte[] data) { 1076 return Hex.encodeHexString(sha512_224(data)); 1077 } 1078 1079 /** 1080 * Calculates the SHA-512/224 digest and returns the value as a hexadecimal string. 1081 * 1082 * @param data Data to digest 1083 * @return SHA-512/224 digest as a hexadecimal string 1084 * @throws IOException On error reading from the stream 1085 * @since 1.14 1086 */ 1087 public static String sha512_224Hex(final InputStream data) throws IOException { 1088 return Hex.encodeHexString(sha512_224(data)); 1089 } 1090 1091 /** 1092 * Calculates the SHA-512/224 digest and returns the value as a hexadecimal string. 1093 * 1094 * @param data Data to digest 1095 * @return SHA-512/224 digest as a hexadecimal string 1096 * @since 1.14 1097 */ 1098 public static String sha512_224Hex(final String data) { 1099 return Hex.encodeHexString(sha512_224(data)); 1100 } 1101 1102 /** 1103 * Calculates the SHA-512/256 digest and returns the value as a {@code byte[]}. 1104 * 1105 * @param data Data to digest 1106 * @return SHA-512/256 digest 1107 * @since 1.14 1108 */ 1109 public static byte[] sha512_256(final byte[] data) { 1110 return getSha512_256Digest().digest(data); 1111 } 1112 1113 /** 1114 * Calculates the SHA-512/256 digest and returns the value as a {@code byte[]}. 1115 * 1116 * @param data Data to digest 1117 * @return SHA-512/256 digest 1118 * @throws IOException On error reading from the stream 1119 * @since 1.14 1120 */ 1121 public static byte[] sha512_256(final InputStream data) throws IOException { 1122 return digest(getSha512_256Digest(), data); 1123 } 1124 1125 /** 1126 * Calculates the SHA-512/256 digest and returns the value as a {@code byte[]}. 1127 * 1128 * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 1129 * @return SHA-512/224 digest 1130 * @since 1.14 1131 */ 1132 public static byte[] sha512_256(final String data) { 1133 return sha512_256(StringUtils.getBytesUtf8(data)); 1134 } 1135 1136 /** 1137 * Calculates the SHA-512/256 digest and returns the value as a hexadecimal string. 1138 * 1139 * @param data Data to digest 1140 * @return SHA-512/256 digest as a hexadecimal string 1141 * @since 1.14 1142 */ 1143 public static String sha512_256Hex(final byte[] data) { 1144 return Hex.encodeHexString(sha512_256(data)); 1145 } 1146 1147 /** 1148 * Calculates the SHA-512/256 digest and returns the value as a hexadecimal string. 1149 * 1150 * @param data Data to digest 1151 * @return SHA-512/256 digest as a hexadecimal string 1152 * @throws IOException On error reading from the stream 1153 * @since 1.14 1154 */ 1155 public static String sha512_256Hex(final InputStream data) throws IOException { 1156 return Hex.encodeHexString(sha512_256(data)); 1157 } 1158 1159 /** 1160 * Calculates the SHA-512/256 digest and returns the value as a hexadecimal string. 1161 * 1162 * @param data Data to digest 1163 * @return SHA-512/256 digest as a hexadecimal string 1164 * @since 1.14 1165 */ 1166 public static String sha512_256Hex(final String data) { 1167 return Hex.encodeHexString(sha512_256(data)); 1168 } 1169 1170 /** 1171 * Calculates the SHA-512 digest and returns the value as a hexadecimal string. 1172 * 1173 * @param data Data to digest 1174 * @return SHA-512 digest as a hexadecimal string 1175 * @since 1.4 1176 */ 1177 public static String sha512Hex(final byte[] data) { 1178 return Hex.encodeHexString(sha512(data)); 1179 } 1180 1181 /** 1182 * Calculates the SHA-512 digest and returns the value as a hexadecimal string. 1183 * 1184 * @param data Data to digest 1185 * @return SHA-512 digest as a hexadecimal string 1186 * @throws IOException On error reading from the stream 1187 * @since 1.4 1188 */ 1189 public static String sha512Hex(final InputStream data) throws IOException { 1190 return Hex.encodeHexString(sha512(data)); 1191 } 1192 1193 /** 1194 * Calculates the SHA-512 digest and returns the value as a hexadecimal string. 1195 * 1196 * @param data Data to digest 1197 * @return SHA-512 digest as a hexadecimal string 1198 * @since 1.4 1199 */ 1200 public static String sha512Hex(final String data) { 1201 return Hex.encodeHexString(sha512(data)); 1202 } 1203 1204 /** 1205 * Calculates the SHA-1 digest and returns the value as a hexadecimal string. 1206 * 1207 * @param data Data to digest 1208 * @return SHA-1 digest as a hexadecimal string 1209 * @deprecated (1.11) Use {@link #sha1Hex(byte[])} 1210 */ 1211 @Deprecated 1212 public static String shaHex(final byte[] data) { 1213 return sha1Hex(data); 1214 } 1215 1216 /** 1217 * Calculates the SHA-1 digest and returns the value as a hexadecimal string. 1218 * 1219 * @param data Data to digest 1220 * @return SHA-1 digest as a hexadecimal string 1221 * @throws IOException On error reading from the stream 1222 * @since 1.4 1223 * @deprecated (1.11) Use {@link #sha1Hex(InputStream)} 1224 */ 1225 @Deprecated 1226 public static String shaHex(final InputStream data) throws IOException { 1227 return sha1Hex(data); 1228 } 1229 1230 /** 1231 * Calculates the SHA-1 digest and returns the value as a hexadecimal string. 1232 * 1233 * @param data Data to digest 1234 * @return SHA-1 digest as a hexadecimal string 1235 * @deprecated (1.11) Use {@link #sha1Hex(String)} 1236 */ 1237 @Deprecated 1238 public static String shaHex(final String data) { 1239 return sha1Hex(data); 1240 } 1241 1242 /** 1243 * Updates the given {@link MessageDigest}. 1244 * 1245 * @param messageDigest the {@link MessageDigest} to update 1246 * @param valueToDigest the value to update the {@link MessageDigest} with 1247 * @return the updated {@link MessageDigest} 1248 * @since 1.7 1249 */ 1250 public static MessageDigest updateDigest(final MessageDigest messageDigest, final byte[] valueToDigest) { 1251 messageDigest.update(valueToDigest); 1252 return messageDigest; 1253 } 1254 1255 /** 1256 * Updates the given {@link MessageDigest}. 1257 * 1258 * @param messageDigest the {@link MessageDigest} to update 1259 * @param valueToDigest the value to update the {@link MessageDigest} with 1260 * @return the updated {@link MessageDigest} 1261 * @since 1.11 1262 */ 1263 public static MessageDigest updateDigest(final MessageDigest messageDigest, final ByteBuffer valueToDigest) { 1264 messageDigest.update(valueToDigest); 1265 return messageDigest; 1266 } 1267 1268 /** 1269 * Reads through a File and updates the digest for the data 1270 * 1271 * @param digest The MessageDigest to use (for example MD5) 1272 * @param data Data to digest 1273 * @return the digest 1274 * @throws IOException On error reading from the stream 1275 * @since 1.11 1276 */ 1277 public static MessageDigest updateDigest(final MessageDigest digest, final File data) throws IOException { 1278 return updateDigest(digest, data.toPath()); 1279 } 1280 1281 /** 1282 * Reads through a RandomAccessFile and updates the digest for the data using non-blocking-io (NIO). 1283 * 1284 * TODO Decide if this should be public. 1285 * 1286 * @param digest The MessageDigest to use (for example MD5) 1287 * @param data Data to digest 1288 * @return the digest 1289 * @throws IOException On error reading from the stream 1290 * @since 1.14 1291 */ 1292 private static MessageDigest updateDigest(final MessageDigest digest, final FileChannel data) throws IOException { 1293 final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); 1294 while (data.read(buffer) > 0) { 1295 buffer.flip(); 1296 digest.update(buffer); 1297 buffer.clear(); 1298 } 1299 return digest; 1300 } 1301 1302 /** 1303 * Reads through an InputStream and updates the digest for the data 1304 * 1305 * @param digest The MessageDigest to use (for example MD5) 1306 * @param inputStream Data to digest 1307 * @return the digest 1308 * @throws IOException On error reading from the stream 1309 * @since 1.8 1310 */ 1311 public static MessageDigest updateDigest(final MessageDigest digest, final InputStream inputStream) throws IOException { 1312 final byte[] buffer = new byte[BUFFER_SIZE]; 1313 int read = inputStream.read(buffer, 0, BUFFER_SIZE); 1314 while (read > -1) { 1315 digest.update(buffer, 0, read); 1316 read = inputStream.read(buffer, 0, BUFFER_SIZE); 1317 } 1318 return digest; 1319 } 1320 1321 /** 1322 * Reads through a Path and updates the digest for the data 1323 * 1324 * @param digest The MessageDigest to use (for example MD5) 1325 * @param path Data to digest 1326 * @param options options How to open the file 1327 * @return the digest 1328 * @throws IOException On error reading from the stream 1329 * @since 1.14 1330 */ 1331 public static MessageDigest updateDigest(final MessageDigest digest, final Path path, final OpenOption... options) throws IOException { 1332 try (BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(path, options))) { 1333 return updateDigest(digest, inputStream); 1334 } 1335 } 1336 1337 /** 1338 * Reads through a RandomAccessFile and updates the digest for the data using non-blocking-io (NIO) 1339 * 1340 * @param digest The MessageDigest to use (for example MD5) 1341 * @param data Data to digest 1342 * @return the digest 1343 * @throws IOException On error reading from the stream 1344 * @since 1.14 1345 */ 1346 @SuppressWarnings("resource") // Closing RandomAccessFile closes the channel. 1347 public static MessageDigest updateDigest(final MessageDigest digest, final RandomAccessFile data) throws IOException { 1348 return updateDigest(digest, data.getChannel()); 1349 } 1350 1351 /** 1352 * Updates the given {@link MessageDigest} from a String (converted to bytes using UTF-8). 1353 * <p> 1354 * To update the digest using a different charset for the conversion, convert the String to a byte array using 1355 * {@link String#getBytes(java.nio.charset.Charset)} and pass that to the {@link DigestUtils#updateDigest(MessageDigest, byte[])} method 1356 * 1357 * @param messageDigest the {@link MessageDigest} to update 1358 * @param valueToDigest the value to update the {@link MessageDigest} with; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 1359 * @return the updated {@link MessageDigest} 1360 * @since 1.7 1361 */ 1362 public static MessageDigest updateDigest(final MessageDigest messageDigest, final String valueToDigest) { 1363 messageDigest.update(StringUtils.getBytesUtf8(valueToDigest)); 1364 return messageDigest; 1365 } 1366 1367 private final MessageDigest messageDigest; 1368 1369 /** 1370 * Preserves binary compatibility only. As for previous versions does not provide useful behavior 1371 * 1372 * @deprecated since 1.11; only useful to preserve binary compatibility 1373 */ 1374 @Deprecated 1375 public DigestUtils() { 1376 this.messageDigest = null; 1377 } 1378 1379 /** 1380 * Creates an instance using the provided {@link MessageDigest} parameter. 1381 * 1382 * This can then be used to create digests using methods such as {@link #digest(byte[])} and {@link #digestAsHex(File)}. 1383 * 1384 * @param digest the {@link MessageDigest} to use 1385 * @since 1.11 1386 */ 1387 public DigestUtils(final MessageDigest digest) { 1388 this.messageDigest = digest; 1389 } 1390 1391 /** 1392 * Creates an instance using the provided {@link MessageDigest} parameter. 1393 * 1394 * This can then be used to create digests using methods such as {@link #digest(byte[])} and {@link #digestAsHex(File)}. 1395 * 1396 * @param name the name of the {@link MessageDigest} to use 1397 * @see #getDigest(String) 1398 * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught. 1399 * @since 1.11 1400 */ 1401 public DigestUtils(final String name) { 1402 this(getDigest(name)); 1403 } 1404 1405 /** 1406 * Reads through a byte array and returns the digest for the data. 1407 * 1408 * @param data Data to digest 1409 * @return the digest 1410 * @since 1.11 1411 */ 1412 public byte[] digest(final byte[] data) { 1413 return updateDigest(messageDigest, data).digest(); 1414 } 1415 1416 /** 1417 * Reads through a ByteBuffer and returns the digest for the data 1418 * 1419 * @param data Data to digest 1420 * @return the digest 1421 * @since 1.11 1422 */ 1423 public byte[] digest(final ByteBuffer data) { 1424 return updateDigest(messageDigest, data).digest(); 1425 } 1426 1427 /** 1428 * Reads through a File and returns the digest for the data 1429 * 1430 * @param data Data to digest 1431 * @return the digest 1432 * @throws IOException On error reading from the stream 1433 * @since 1.11 1434 */ 1435 public byte[] digest(final File data) throws IOException { 1436 return updateDigest(messageDigest, data).digest(); 1437 } 1438 1439 /** 1440 * Reads through an InputStream and returns the digest for the data 1441 * 1442 * @param data Data to digest 1443 * @return the digest 1444 * @throws IOException On error reading from the stream 1445 * @since 1.11 1446 */ 1447 public byte[] digest(final InputStream data) throws IOException { 1448 return updateDigest(messageDigest, data).digest(); 1449 } 1450 1451 /** 1452 * Reads through a File and returns the digest for the data 1453 * 1454 * @param data Data to digest 1455 * @param options options How to open the file 1456 * @return the digest 1457 * @throws IOException On error reading from the stream 1458 * @since 1.14 1459 */ 1460 public byte[] digest(final Path data, final OpenOption... options) throws IOException { 1461 return updateDigest(messageDigest, data, options).digest(); 1462 } 1463 1464 /** 1465 * Reads through a byte array and returns the digest for the data. 1466 * 1467 * @param data Data to digest treated as UTF-8 string 1468 * @return the digest 1469 * @since 1.11 1470 */ 1471 public byte[] digest(final String data) { 1472 return updateDigest(messageDigest, data).digest(); 1473 } 1474 1475 /** 1476 * Reads through a byte array and returns the digest for the data. 1477 * 1478 * @param data Data to digest 1479 * @return the digest as a hexadecimal string 1480 * @since 1.11 1481 */ 1482 public String digestAsHex(final byte[] data) { 1483 return Hex.encodeHexString(digest(data)); 1484 } 1485 1486 /** 1487 * Reads through a ByteBuffer and returns the digest for the data 1488 * 1489 * @param data Data to digest 1490 * @return the digest as a hexadecimal string 1491 * @since 1.11 1492 */ 1493 public String digestAsHex(final ByteBuffer data) { 1494 return Hex.encodeHexString(digest(data)); 1495 } 1496 1497 /** 1498 * Reads through a File and returns the digest for the data 1499 * 1500 * @param data Data to digest 1501 * @return the digest as a hexadecimal string 1502 * @throws IOException On error reading from the stream 1503 * @since 1.11 1504 */ 1505 public String digestAsHex(final File data) throws IOException { 1506 return Hex.encodeHexString(digest(data)); 1507 } 1508 1509 /** 1510 * Reads through an InputStream and returns the digest for the data 1511 * 1512 * @param data Data to digest 1513 * @return the digest as a hexadecimal string 1514 * @throws IOException On error reading from the stream 1515 * @since 1.11 1516 */ 1517 public String digestAsHex(final InputStream data) throws IOException { 1518 return Hex.encodeHexString(digest(data)); 1519 } 1520 1521 /** 1522 * Reads through a File and returns the digest for the data 1523 * 1524 * @param data Data to digest 1525 * @param options options How to open the file 1526 * @return the digest as a hexadecimal string 1527 * @throws IOException On error reading from the stream 1528 * @since 1.11 1529 */ 1530 public String digestAsHex(final Path data, final OpenOption... options) throws IOException { 1531 return Hex.encodeHexString(digest(data, options)); 1532 } 1533 1534 /** 1535 * Reads through a byte array and returns the digest for the data. 1536 * 1537 * @param data Data to digest treated as UTF-8 string 1538 * @return the digest as a hexadecimal string 1539 * @since 1.11 1540 */ 1541 public String digestAsHex(final String data) { 1542 return Hex.encodeHexString(digest(data)); 1543 } 1544 1545 /** 1546 * Returns the message digest instance. 1547 * 1548 * @return the message digest instance 1549 * @since 1.11 1550 */ 1551 public MessageDigest getMessageDigest() { 1552 return messageDigest; 1553 } 1554 1555}