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