DigestUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.codec.digest;

  18. import java.io.BufferedInputStream;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.RandomAccessFile;
  24. import java.nio.ByteBuffer;
  25. import java.nio.channels.FileChannel;
  26. import java.nio.file.Files;
  27. import java.nio.file.OpenOption;
  28. import java.nio.file.Path;
  29. import java.security.MessageDigest;
  30. import java.security.NoSuchAlgorithmException;

  31. import org.apache.commons.codec.binary.Hex;
  32. import org.apache.commons.codec.binary.StringUtils;

  33. /**
  34.  * Operations to simplify common {@link java.security.MessageDigest} tasks. This class is immutable and thread-safe. However the MessageDigest instances it
  35.  * creates generally won't be.
  36.  * <p>
  37.  * The {@link MessageDigestAlgorithms} class provides constants for standard digest algorithms that can be used with the {@link #getDigest(String)} method and
  38.  * other methods that require the Digest algorithm name.
  39.  * </p>
  40.  * <p>
  41.  * 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
  42.  * quickly becomes unwieldy. The following code works with all algorithms:
  43.  * </p>
  44.  *
  45.  * <pre>
  46.  * import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224;
  47.  * ...
  48.  * byte [] digest = new DigestUtils(SHA_224).digest(dataToDigest);
  49.  * String hdigest = new DigestUtils(SHA_224).digestAsHex(new File("pom.xml"));
  50.  * </pre>
  51.  *
  52.  * @see MessageDigestAlgorithms
  53.  */
  54. public class DigestUtils {

  55.     /**
  56.      * Package-private for tests.
  57.      */
  58.     static final int BUFFER_SIZE = 1024;

  59.     /**
  60.      * Reads through a byte array and returns the digest for the data. Provided for symmetry with other methods.
  61.      *
  62.      * @param messageDigest The MessageDigest to use (for example MD5)
  63.      * @param data          Data to digest
  64.      * @return the digest
  65.      * @since 1.11
  66.      */
  67.     public static byte[] digest(final MessageDigest messageDigest, final byte[] data) {
  68.         return messageDigest.digest(data);
  69.     }

  70.     /**
  71.      * Reads through a ByteBuffer and returns the digest for the data
  72.      *
  73.      * @param messageDigest The MessageDigest to use (for example MD5)
  74.      * @param data          Data to digest
  75.      * @return the digest
  76.      * @since 1.11
  77.      */
  78.     public static byte[] digest(final MessageDigest messageDigest, final ByteBuffer data) {
  79.         messageDigest.update(data);
  80.         return messageDigest.digest();
  81.     }

  82.     /**
  83.      * Reads through a File and returns the digest for the data
  84.      *
  85.      * @param messageDigest The MessageDigest to use (for example MD5)
  86.      * @param data          Data to digest
  87.      * @return the digest
  88.      * @throws IOException On error reading from the stream
  89.      * @since 1.11
  90.      */
  91.     public static byte[] digest(final MessageDigest messageDigest, final File data) throws IOException {
  92.         return updateDigest(messageDigest, data).digest();
  93.     }

  94.     /**
  95.      * Reads through an InputStream and returns the digest for the data
  96.      *
  97.      * @param messageDigest The MessageDigest to use (for example MD5)
  98.      * @param data          Data to digest
  99.      * @return the digest
  100.      * @throws IOException On error reading from the stream
  101.      * @since 1.11 (was private)
  102.      */
  103.     public static byte[] digest(final MessageDigest messageDigest, final InputStream data) throws IOException {
  104.         return updateDigest(messageDigest, data).digest();
  105.     }

  106.     /**
  107.      * Reads through a File and returns the digest for the data
  108.      *
  109.      * @param messageDigest The MessageDigest to use (for example MD5)
  110.      * @param data          Data to digest
  111.      * @param options       options How to open the file
  112.      * @return the digest
  113.      * @throws IOException On error reading from the stream
  114.      * @since 1.14
  115.      */
  116.     public static byte[] digest(final MessageDigest messageDigest, final Path data, final OpenOption... options) throws IOException {
  117.         return updateDigest(messageDigest, data, options).digest();
  118.     }

  119.     /**
  120.      * Reads through a RandomAccessFile using non-blocking-io (NIO) and returns the digest for the data
  121.      *
  122.      * @param messageDigest The MessageDigest to use (for example MD5)
  123.      * @param data          Data to digest
  124.      * @return the digest
  125.      * @throws IOException On error reading from the stream
  126.      * @since 1.14
  127.      */
  128.     public static byte[] digest(final MessageDigest messageDigest, final RandomAccessFile data) throws IOException {
  129.         return updateDigest(messageDigest, data).digest();
  130.     }

  131.     /**
  132.      * Gets a {@code MessageDigest} for the given {@code algorithm}.
  133.      *
  134.      * @param algorithm the name of the algorithm requested. See
  135.      *                  <a href="https://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA">Appendix A in the Java
  136.      *                  Cryptography Architecture Reference Guide</a> for information about standard algorithm names.
  137.      * @return A digest instance.
  138.      * @see MessageDigest#getInstance(String)
  139.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
  140.      */
  141.     public static MessageDigest getDigest(final String algorithm) {
  142.         try {
  143.             return getMessageDigest(algorithm);
  144.         } catch (final NoSuchAlgorithmException e) {
  145.             throw new IllegalArgumentException(e);
  146.         }
  147.     }

  148.     /**
  149.      * Gets a {@code MessageDigest} for the given {@code algorithm} or a default if there is a problem getting the algorithm.
  150.      *
  151.      * @param algorithm            the name of the algorithm requested. See
  152.      *                             <a href="https://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA"> Appendix A in the Java
  153.      *                             Cryptography Architecture Reference Guide</a> for information about standard algorithm names.
  154.      * @param defaultMessageDigest The default MessageDigest.
  155.      * @return A digest instance.
  156.      * @see MessageDigest#getInstance(String)
  157.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
  158.      * @since 1.11
  159.      */
  160.     public static MessageDigest getDigest(final String algorithm, final MessageDigest defaultMessageDigest) {
  161.         try {
  162.             return getMessageDigest(algorithm);
  163.         } catch (final Exception e) {
  164.             return defaultMessageDigest;
  165.         }
  166.     }

  167.     /**
  168.      * Gets an MD2 MessageDigest.
  169.      *
  170.      * @return An MD2 digest instance.
  171.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should never happen because MD2 is a built-in algorithm
  172.      * @see MessageDigestAlgorithms#MD2
  173.      * @since 1.7
  174.      */
  175.     public static MessageDigest getMd2Digest() {
  176.         return getDigest(MessageDigestAlgorithms.MD2);
  177.     }

  178.     /**
  179.      * Gets an MD5 MessageDigest.
  180.      *
  181.      * @return An MD5 digest instance.
  182.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should never happen because MD5 is a built-in algorithm
  183.      * @see MessageDigestAlgorithms#MD5
  184.      */
  185.     public static MessageDigest getMd5Digest() {
  186.         return getDigest(MessageDigestAlgorithms.MD5);
  187.     }

  188.     /**
  189.      * Gets a {@code MessageDigest} for the given {@code algorithm}.
  190.      *
  191.      * @param algorithm the name of the algorithm requested. See
  192.      *                  <a href="https://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA"> Appendix A in the Java
  193.      *                  Cryptography Architecture Reference Guide</a> for information about standard algorithm names.
  194.      * @return A digest instance.
  195.      * @see MessageDigest#getInstance(String)
  196.      * @throws NoSuchAlgorithmException if no Provider supports a MessageDigestSpi implementation for the specified algorithm.
  197.      */
  198.     private static MessageDigest getMessageDigest(final String algorithm) throws NoSuchAlgorithmException {
  199.         return MessageDigest.getInstance(algorithm);
  200.     }

  201.     /**
  202.      * Gets an SHA-1 digest.
  203.      *
  204.      * @return An SHA-1 digest instance.
  205.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-1 is a built-in algorithm
  206.      * @see MessageDigestAlgorithms#SHA_1
  207.      * @since 1.7
  208.      */
  209.     public static MessageDigest getSha1Digest() {
  210.         return getDigest(MessageDigestAlgorithms.SHA_1);
  211.     }

  212.     /**
  213.      * Gets an SHA-256 digest.
  214.      *
  215.      * @return An SHA-256 digest instance.
  216.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-256 is a built-in algorithm
  217.      * @see MessageDigestAlgorithms#SHA_256
  218.      */
  219.     public static MessageDigest getSha256Digest() {
  220.         return getDigest(MessageDigestAlgorithms.SHA_256);
  221.     }

  222.     /**
  223.      * Gets an SHA3-224 digest.
  224.      *
  225.      * @return An SHA3-224 digest instance.
  226.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should not happen on Oracle Java 9 and greater.
  227.      * @see MessageDigestAlgorithms#SHA3_224
  228.      * @since 1.12
  229.      */
  230.     public static MessageDigest getSha3_224Digest() {
  231.         return getDigest(MessageDigestAlgorithms.SHA3_224);
  232.     }

  233.     /**
  234.      * Returns an SHA3-256 digest.
  235.      *
  236.      * @return An SHA3-256 digest instance.
  237.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should not happen on Oracle Java 9 and greater.
  238.      * @see MessageDigestAlgorithms#SHA3_256
  239.      * @since 1.12
  240.      */
  241.     public static MessageDigest getSha3_256Digest() {
  242.         return getDigest(MessageDigestAlgorithms.SHA3_256);
  243.     }

  244.     /**
  245.      * Gets an SHA3-384 digest.
  246.      *
  247.      * @return An SHA3-384 digest instance.
  248.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should not happen on Oracle Java 9 and greater.
  249.      * @see MessageDigestAlgorithms#SHA3_384
  250.      * @since 1.12
  251.      */
  252.     public static MessageDigest getSha3_384Digest() {
  253.         return getDigest(MessageDigestAlgorithms.SHA3_384);
  254.     }

  255.     /**
  256.      * Gets an SHA3-512 digest.
  257.      *
  258.      * @return An SHA3-512 digest instance.
  259.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should not happen on Oracle Java 9 and greater.
  260.      * @see MessageDigestAlgorithms#SHA3_512
  261.      * @since 1.12
  262.      */
  263.     public static MessageDigest getSha3_512Digest() {
  264.         return getDigest(MessageDigestAlgorithms.SHA3_512);
  265.     }

  266.     /**
  267.      * Gets an SHA-384 digest.
  268.      *
  269.      * @return An SHA-384 digest instance.
  270.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-384 is a built-in algorithm
  271.      * @see MessageDigestAlgorithms#SHA_384
  272.      */
  273.     public static MessageDigest getSha384Digest() {
  274.         return getDigest(MessageDigestAlgorithms.SHA_384);
  275.     }

  276.     /**
  277.      * Gets an SHA-512/224 digest.
  278.      *
  279.      * @return An SHA-512/224 digest instance.
  280.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
  281.      * @see MessageDigestAlgorithms#SHA_512_224
  282.      */
  283.     public static MessageDigest getSha512_224Digest() {
  284.         return getDigest(MessageDigestAlgorithms.SHA_512_224);
  285.     }

  286.     /**
  287.      * Gets an SHA-512/256 digest.
  288.      *
  289.      * @return An SHA-512/256 digest instance.
  290.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
  291.      * @see MessageDigestAlgorithms#SHA_512_224
  292.      */
  293.     public static MessageDigest getSha512_256Digest() {
  294.         return getDigest(MessageDigestAlgorithms.SHA_512_256);
  295.     }

  296.     /**
  297.      * Gets an SHA-512 digest.
  298.      *
  299.      * @return An SHA-512 digest instance.
  300.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-512 is a built-in algorithm
  301.      * @see MessageDigestAlgorithms#SHA_512
  302.      */
  303.     public static MessageDigest getSha512Digest() {
  304.         return getDigest(MessageDigestAlgorithms.SHA_512);
  305.     }

  306.     /**
  307.      * Gets an SHA-1 digest.
  308.      *
  309.      * @return An SHA-1 digest instance.
  310.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught
  311.      * @deprecated (1.11) Use {@link #getSha1Digest()}
  312.      */
  313.     @Deprecated
  314.     public static MessageDigest getShaDigest() {
  315.         return getSha1Digest();
  316.     }

  317.     /**
  318.      * Test whether the algorithm is supported.
  319.      *
  320.      * @param messageDigestAlgorithm the algorithm name
  321.      * @return {@code true} if the algorithm can be found
  322.      * @since 1.11
  323.      */
  324.     public static boolean isAvailable(final String messageDigestAlgorithm) {
  325.         return getDigest(messageDigestAlgorithm, null) != null;
  326.     }

  327.     /**
  328.      * Calculates the MD2 digest and returns the value as a 16 element {@code byte[]}.
  329.      *
  330.      * @param data Data to digest
  331.      * @return MD2 digest
  332.      * @since 1.7
  333.      */
  334.     public static byte[] md2(final byte[] data) {
  335.         return getMd2Digest().digest(data);
  336.     }

  337.     /**
  338.      * Calculates the MD2 digest and returns the value as a 16 element {@code byte[]}.
  339.      *
  340.      * @param data Data to digest
  341.      * @return MD2 digest
  342.      * @throws IOException On error reading from the stream
  343.      * @since 1.7
  344.      */
  345.     public static byte[] md2(final InputStream data) throws IOException {
  346.         return digest(getMd2Digest(), data);
  347.     }

  348.     /**
  349.      * Calculates the MD2 digest and returns the value as a 16 element {@code byte[]}.
  350.      *
  351.      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  352.      * @return MD2 digest
  353.      * @since 1.7
  354.      */
  355.     public static byte[] md2(final String data) {
  356.         return md2(StringUtils.getBytesUtf8(data));
  357.     }

  358.     /**
  359.      * Calculates the MD2 digest and returns the value as a 32 character hexadecimal string.
  360.      *
  361.      * @param data Data to digest
  362.      * @return MD2 digest as a hexadecimal string
  363.      * @since 1.7
  364.      */
  365.     public static String md2Hex(final byte[] data) {
  366.         return Hex.encodeHexString(md2(data));
  367.     }

  368.     /**
  369.      * Calculates the MD2 digest and returns the value as a 32 character hexadecimal string.
  370.      *
  371.      * @param data Data to digest
  372.      * @return MD2 digest as a hexadecimal string
  373.      * @throws IOException On error reading from the stream
  374.      * @since 1.7
  375.      */
  376.     public static String md2Hex(final InputStream data) throws IOException {
  377.         return Hex.encodeHexString(md2(data));
  378.     }

  379.     /**
  380.      * Calculates the MD2 digest and returns the value as a 32 character hexadecimal string.
  381.      *
  382.      * @param data Data to digest
  383.      * @return MD2 digest as a hexadecimal string
  384.      * @since 1.7
  385.      */
  386.     public static String md2Hex(final String data) {
  387.         return Hex.encodeHexString(md2(data));
  388.     }

  389.     /**
  390.      * Calculates the MD5 digest and returns the value as a 16 element {@code byte[]}.
  391.      *
  392.      * @param data Data to digest
  393.      * @return MD5 digest
  394.      */
  395.     public static byte[] md5(final byte[] data) {
  396.         return getMd5Digest().digest(data);
  397.     }

  398.     /**
  399.      * Calculates the MD5 digest and returns the value as a 16 element {@code byte[]}.
  400.      *
  401.      * @param data Data to digest
  402.      * @return MD5 digest
  403.      * @throws IOException On error reading from the stream
  404.      * @since 1.4
  405.      */
  406.     public static byte[] md5(final InputStream data) throws IOException {
  407.         return digest(getMd5Digest(), data);
  408.     }

  409.     /**
  410.      * Calculates the MD5 digest and returns the value as a 16 element {@code byte[]}.
  411.      *
  412.      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  413.      * @return MD5 digest
  414.      */
  415.     public static byte[] md5(final String data) {
  416.         return md5(StringUtils.getBytesUtf8(data));
  417.     }

  418.     /**
  419.      * Calculates the MD5 digest and returns the value as a 32 character hexadecimal string.
  420.      *
  421.      * @param data Data to digest
  422.      * @return MD5 digest as a hexadecimal string
  423.      */
  424.     public static String md5Hex(final byte[] data) {
  425.         return Hex.encodeHexString(md5(data));
  426.     }

  427.     /**
  428.      * Calculates the MD5 digest and returns the value as a 32 character hexadecimal string.
  429.      *
  430.      * @param data Data to digest
  431.      * @return MD5 digest as a hexadecimal string
  432.      * @throws IOException On error reading from the stream
  433.      * @since 1.4
  434.      */
  435.     public static String md5Hex(final InputStream data) throws IOException {
  436.         return Hex.encodeHexString(md5(data));
  437.     }

  438.     /**
  439.      * Calculates the MD5 digest and returns the value as a 32 character hexadecimal string.
  440.      *
  441.      * @param data Data to digest
  442.      * @return MD5 digest as a hexadecimal string
  443.      */
  444.     public static String md5Hex(final String data) {
  445.         return Hex.encodeHexString(md5(data));
  446.     }

  447.     /**
  448.      * Calculates the SHA-1 digest and returns the value as a {@code byte[]}.
  449.      *
  450.      * @param data Data to digest
  451.      * @return SHA-1 digest
  452.      * @deprecated (1.11) Use {@link #sha1(byte[])}
  453.      */
  454.     @Deprecated
  455.     public static byte[] sha(final byte[] data) {
  456.         return sha1(data);
  457.     }

  458.     /**
  459.      * Calculates the SHA-1 digest and returns the value as a {@code byte[]}.
  460.      *
  461.      * @param data Data to digest
  462.      * @return SHA-1 digest
  463.      * @throws IOException On error reading from the stream
  464.      * @since 1.4
  465.      * @deprecated (1.11) Use {@link #sha1(InputStream)}
  466.      */
  467.     @Deprecated
  468.     public static byte[] sha(final InputStream data) throws IOException {
  469.         return sha1(data);
  470.     }

  471.     /**
  472.      * Calculates the SHA-1 digest and returns the value as a {@code byte[]}.
  473.      *
  474.      * @param data Data to digest
  475.      * @return SHA-1 digest
  476.      * @deprecated (1.11) Use {@link #sha1(String)}
  477.      */
  478.     @Deprecated
  479.     public static byte[] sha(final String data) {
  480.         return sha1(data);
  481.     }

  482.     /**
  483.      * Calculates the SHA-1 digest and returns the value as a {@code byte[]}.
  484.      *
  485.      * @param data Data to digest
  486.      * @return SHA-1 digest
  487.      * @since 1.7
  488.      */
  489.     public static byte[] sha1(final byte[] data) {
  490.         return getSha1Digest().digest(data);
  491.     }

  492.     /**
  493.      * Calculates the SHA-1 digest and returns the value as a {@code byte[]}.
  494.      *
  495.      * @param data Data to digest
  496.      * @return SHA-1 digest
  497.      * @throws IOException On error reading from the stream
  498.      * @since 1.7
  499.      */
  500.     public static byte[] sha1(final InputStream data) throws IOException {
  501.         return digest(getSha1Digest(), data);
  502.     }

  503.     /**
  504.      * Calculates the SHA-1 digest and returns the value as a {@code byte[]}.
  505.      *
  506.      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  507.      * @return SHA-1 digest
  508.      */
  509.     public static byte[] sha1(final String data) {
  510.         return sha1(StringUtils.getBytesUtf8(data));
  511.     }

  512.     /**
  513.      * Calculates the SHA-1 digest and returns the value as a hexadecimal string.
  514.      *
  515.      * @param data Data to digest
  516.      * @return SHA-1 digest as a hexadecimal string
  517.      * @since 1.7
  518.      */
  519.     public static String sha1Hex(final byte[] data) {
  520.         return Hex.encodeHexString(sha1(data));
  521.     }

  522.     /**
  523.      * Calculates the SHA-1 digest and returns the value as a hexadecimal string.
  524.      *
  525.      * @param data Data to digest
  526.      * @return SHA-1 digest as a hexadecimal string
  527.      * @throws IOException On error reading from the stream
  528.      * @since 1.7
  529.      */
  530.     public static String sha1Hex(final InputStream data) throws IOException {
  531.         return Hex.encodeHexString(sha1(data));
  532.     }

  533.     /**
  534.      * Calculates the SHA-1 digest and returns the value as a hexadecimal string.
  535.      *
  536.      * @param data Data to digest
  537.      * @return SHA-1 digest as a hexadecimal string
  538.      * @since 1.7
  539.      */
  540.     public static String sha1Hex(final String data) {
  541.         return Hex.encodeHexString(sha1(data));
  542.     }

  543.     /**
  544.      * Calculates the SHA-256 digest and returns the value as a {@code byte[]}.
  545.      *
  546.      * @param data Data to digest
  547.      * @return SHA-256 digest
  548.      * @since 1.4
  549.      */
  550.     public static byte[] sha256(final byte[] data) {
  551.         return getSha256Digest().digest(data);
  552.     }

  553.     /**
  554.      * Calculates the SHA-256 digest and returns the value as a {@code byte[]}.
  555.      *
  556.      * @param data Data to digest
  557.      * @return SHA-256 digest
  558.      * @throws IOException On error reading from the stream
  559.      * @since 1.4
  560.      */
  561.     public static byte[] sha256(final InputStream data) throws IOException {
  562.         return digest(getSha256Digest(), data);
  563.     }

  564.     /**
  565.      * Calculates the SHA-256 digest and returns the value as a {@code byte[]}.
  566.      *
  567.      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  568.      * @return SHA-256 digest
  569.      * @since 1.4
  570.      */
  571.     public static byte[] sha256(final String data) {
  572.         return sha256(StringUtils.getBytesUtf8(data));
  573.     }

  574.     /**
  575.      * Calculates the SHA-256 digest and returns the value as a hexadecimal string.
  576.      *
  577.      * @param data Data to digest
  578.      * @return SHA-256 digest as a hexadecimal string
  579.      * @since 1.4
  580.      */
  581.     public static String sha256Hex(final byte[] data) {
  582.         return Hex.encodeHexString(sha256(data));
  583.     }

  584.     /**
  585.      * Calculates the SHA-256 digest and returns the value as a hexadecimal string.
  586.      *
  587.      * @param data Data to digest
  588.      * @return SHA-256 digest as a hexadecimal string
  589.      * @throws IOException On error reading from the stream
  590.      * @since 1.4
  591.      */
  592.     public static String sha256Hex(final InputStream data) throws IOException {
  593.         return Hex.encodeHexString(sha256(data));
  594.     }

  595.     /**
  596.      * Calculates the SHA-256 digest and returns the value as a hexadecimal string.
  597.      *
  598.      * @param data Data to digest
  599.      * @return SHA-256 digest as a hexadecimal string
  600.      * @since 1.4
  601.      */
  602.     public static String sha256Hex(final String data) {
  603.         return Hex.encodeHexString(sha256(data));
  604.     }

  605.     /**
  606.      * Calculates the SHA3-224 digest and returns the value as a {@code byte[]}.
  607.      *
  608.      * @param data Data to digest
  609.      * @return SHA3-224 digest
  610.      * @since 1.12
  611.      */
  612.     public static byte[] sha3_224(final byte[] data) {
  613.         return getSha3_224Digest().digest(data);
  614.     }

  615.     /**
  616.      * Calculates the SHA3-224 digest and returns the value as a {@code byte[]}.
  617.      *
  618.      * @param data Data to digest
  619.      * @return SHA3-224 digest
  620.      * @throws IOException On error reading from the stream
  621.      * @since 1.12
  622.      */
  623.     public static byte[] sha3_224(final InputStream data) throws IOException {
  624.         return digest(getSha3_224Digest(), data);
  625.     }

  626.     /**
  627.      * Calculates the SHA3-224 digest and returns the value as a {@code byte[]}.
  628.      *
  629.      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  630.      * @return SHA3-224 digest
  631.      * @since 1.12
  632.      */
  633.     public static byte[] sha3_224(final String data) {
  634.         return sha3_224(StringUtils.getBytesUtf8(data));
  635.     }

  636.     /**
  637.      * Calculates the SHA3-224 digest and returns the value as a hexadecimal string.
  638.      *
  639.      * @param data Data to digest
  640.      * @return SHA3-224 digest as a hexadecimal string
  641.      * @since 1.12
  642.      */
  643.     public static String sha3_224Hex(final byte[] data) {
  644.         return Hex.encodeHexString(sha3_224(data));
  645.     }

  646.     /**
  647.      * Calculates the SHA3-224 digest and returns the value as a hexadecimal string.
  648.      *
  649.      * @param data Data to digest
  650.      * @return SHA3-224 digest as a hexadecimal string
  651.      * @throws IOException On error reading from the stream
  652.      * @since 1.12
  653.      */
  654.     public static String sha3_224Hex(final InputStream data) throws IOException {
  655.         return Hex.encodeHexString(sha3_224(data));
  656.     }

  657.     /**
  658.      * Calculates the SHA3-224 digest and returns the value as a hexadecimal string.
  659.      *
  660.      * @param data Data to digest
  661.      * @return SHA3-224 digest as a hexadecimal string
  662.      * @since 1.12
  663.      */
  664.     public static String sha3_224Hex(final String data) {
  665.         return Hex.encodeHexString(sha3_224(data));
  666.     }

  667.     /**
  668.      * Calculates the SHA3-256 digest and returns the value as a {@code byte[]}.
  669.      *
  670.      * @param data Data to digest
  671.      * @return SHA3-256 digest
  672.      * @since 1.12
  673.      */
  674.     public static byte[] sha3_256(final byte[] data) {
  675.         return getSha3_256Digest().digest(data);
  676.     }

  677.     /**
  678.      * Calculates the SHA3-256 digest and returns the value as a {@code byte[]}.
  679.      *
  680.      * @param data Data to digest
  681.      * @return SHA3-256 digest
  682.      * @throws IOException On error reading from the stream
  683.      * @since 1.12
  684.      */
  685.     public static byte[] sha3_256(final InputStream data) throws IOException {
  686.         return digest(getSha3_256Digest(), data);
  687.     }

  688.     /**
  689.      * Calculates the SHA3-256 digest and returns the value as a {@code byte[]}.
  690.      *
  691.      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  692.      * @return SHA3-256 digest
  693.      * @since 1.12
  694.      */
  695.     public static byte[] sha3_256(final String data) {
  696.         return sha3_256(StringUtils.getBytesUtf8(data));
  697.     }

  698.     /**
  699.      * Calculates the SHA3-256 digest and returns the value as a hexadecimal string.
  700.      *
  701.      * @param data Data to digest
  702.      * @return SHA3-256 digest as a hexadecimal string
  703.      * @since 1.12
  704.      */
  705.     public static String sha3_256Hex(final byte[] data) {
  706.         return Hex.encodeHexString(sha3_256(data));
  707.     }

  708.     /**
  709.      * Calculates the SHA3-256 digest and returns the value as a hexadecimal string.
  710.      *
  711.      * @param data Data to digest
  712.      * @return SHA3-256 digest as a hexadecimal string
  713.      * @throws IOException On error reading from the stream
  714.      * @since 1.12
  715.      */
  716.     public static String sha3_256Hex(final InputStream data) throws IOException {
  717.         return Hex.encodeHexString(sha3_256(data));
  718.     }

  719.     /**
  720.      * Calculates the SHA3-256 digest and returns the value as a hexadecimal string.
  721.      *
  722.      * @param data Data to digest
  723.      * @return SHA3-256 digest as a hexadecimal string
  724.      * @since 1.12
  725.      */
  726.     public static String sha3_256Hex(final String data) {
  727.         return Hex.encodeHexString(sha3_256(data));
  728.     }

  729.     /**
  730.      * Calculates the SHA3-384 digest and returns the value as a {@code byte[]}.
  731.      *
  732.      * @param data Data to digest
  733.      * @return SHA3-384 digest
  734.      * @since 1.12
  735.      */
  736.     public static byte[] sha3_384(final byte[] data) {
  737.         return getSha3_384Digest().digest(data);
  738.     }

  739.     /**
  740.      * Calculates the SHA3-384 digest and returns the value as a {@code byte[]}.
  741.      *
  742.      * @param data Data to digest
  743.      * @return SHA3-384 digest
  744.      * @throws IOException On error reading from the stream
  745.      * @since 1.12
  746.      */
  747.     public static byte[] sha3_384(final InputStream data) throws IOException {
  748.         return digest(getSha3_384Digest(), data);
  749.     }

  750.     /**
  751.      * Calculates the SHA3-384 digest and returns the value as a {@code byte[]}.
  752.      *
  753.      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  754.      * @return SHA3-384 digest
  755.      * @since 1.12
  756.      */
  757.     public static byte[] sha3_384(final String data) {
  758.         return sha3_384(StringUtils.getBytesUtf8(data));
  759.     }

  760.     /**
  761.      * Calculates the SHA3-384 digest and returns the value as a hexadecimal string.
  762.      *
  763.      * @param data Data to digest
  764.      * @return SHA3-384 digest as a hexadecimal string
  765.      * @since 1.12
  766.      */
  767.     public static String sha3_384Hex(final byte[] data) {
  768.         return Hex.encodeHexString(sha3_384(data));
  769.     }

  770.     /**
  771.      * Calculates the SHA3-384 digest and returns the value as a hexadecimal string.
  772.      *
  773.      * @param data Data to digest
  774.      * @return SHA3-384 digest as a hexadecimal string
  775.      * @throws IOException On error reading from the stream
  776.      * @since 1.12
  777.      */
  778.     public static String sha3_384Hex(final InputStream data) throws IOException {
  779.         return Hex.encodeHexString(sha3_384(data));
  780.     }

  781.     /**
  782.      * Calculates the SHA3-384 digest and returns the value as a hexadecimal string.
  783.      *
  784.      * @param data Data to digest
  785.      * @return SHA3-384 digest as a hexadecimal string
  786.      * @since 1.12
  787.      */
  788.     public static String sha3_384Hex(final String data) {
  789.         return Hex.encodeHexString(sha3_384(data));
  790.     }

  791.     /**
  792.      * Calculates the SHA3-512 digest and returns the value as a {@code byte[]}.
  793.      *
  794.      * @param data Data to digest
  795.      * @return SHA3-512 digest
  796.      * @since 1.12
  797.      */
  798.     public static byte[] sha3_512(final byte[] data) {
  799.         return getSha3_512Digest().digest(data);
  800.     }

  801.     /**
  802.      * Calculates the SHA3-512 digest and returns the value as a {@code byte[]}.
  803.      *
  804.      * @param data Data to digest
  805.      * @return SHA3-512 digest
  806.      * @throws IOException On error reading from the stream
  807.      * @since 1.12
  808.      */
  809.     public static byte[] sha3_512(final InputStream data) throws IOException {
  810.         return digest(getSha3_512Digest(), data);
  811.     }

  812.     /**
  813.      * Calculates the SHA3-512 digest and returns the value as a {@code byte[]}.
  814.      *
  815.      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  816.      * @return SHA3-512 digest
  817.      * @since 1.12
  818.      */
  819.     public static byte[] sha3_512(final String data) {
  820.         return sha3_512(StringUtils.getBytesUtf8(data));
  821.     }

  822.     /**
  823.      * Calculates the SHA3-512 digest and returns the value as a hexadecimal string.
  824.      *
  825.      * @param data Data to digest
  826.      * @return SHA3-512 digest as a hexadecimal string
  827.      * @since 1.12
  828.      */
  829.     public static String sha3_512Hex(final byte[] data) {
  830.         return Hex.encodeHexString(sha3_512(data));
  831.     }

  832.     /**
  833.      * Calculates the SHA3-512 digest and returns the value as a hexadecimal string.
  834.      *
  835.      * @param data Data to digest
  836.      * @return SHA3-512 digest as a hexadecimal string
  837.      * @throws IOException On error reading from the stream
  838.      * @since 1.12
  839.      */
  840.     public static String sha3_512Hex(final InputStream data) throws IOException {
  841.         return Hex.encodeHexString(sha3_512(data));
  842.     }

  843.     /**
  844.      * Calculates the SHA3-512 digest and returns the value as a hexadecimal string.
  845.      *
  846.      * @param data Data to digest
  847.      * @return SHA3-512 digest as a hexadecimal string
  848.      * @since 1.12
  849.      */
  850.     public static String sha3_512Hex(final String data) {
  851.         return Hex.encodeHexString(sha3_512(data));
  852.     }

  853.     /**
  854.      * Calculates the SHA-384 digest and returns the value as a {@code byte[]}.
  855.      *
  856.      * @param data Data to digest
  857.      * @return SHA-384 digest
  858.      * @since 1.4
  859.      */
  860.     public static byte[] sha384(final byte[] data) {
  861.         return getSha384Digest().digest(data);
  862.     }

  863.     /**
  864.      * Calculates the SHA-384 digest and returns the value as a {@code byte[]}.
  865.      *
  866.      * @param data Data to digest
  867.      * @return SHA-384 digest
  868.      * @throws IOException On error reading from the stream
  869.      * @since 1.4
  870.      */
  871.     public static byte[] sha384(final InputStream data) throws IOException {
  872.         return digest(getSha384Digest(), data);
  873.     }

  874.     /**
  875.      * Calculates the SHA-384 digest and returns the value as a {@code byte[]}.
  876.      *
  877.      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  878.      * @return SHA-384 digest
  879.      * @since 1.4
  880.      */
  881.     public static byte[] sha384(final String data) {
  882.         return sha384(StringUtils.getBytesUtf8(data));
  883.     }

  884.     /**
  885.      * Calculates the SHA-384 digest and returns the value as a hexadecimal string.
  886.      *
  887.      * @param data Data to digest
  888.      * @return SHA-384 digest as a hexadecimal string
  889.      * @since 1.4
  890.      */
  891.     public static String sha384Hex(final byte[] data) {
  892.         return Hex.encodeHexString(sha384(data));
  893.     }

  894.     /**
  895.      * Calculates the SHA-384 digest and returns the value as a hexadecimal string.
  896.      *
  897.      * @param data Data to digest
  898.      * @return SHA-384 digest as a hexadecimal string
  899.      * @throws IOException On error reading from the stream
  900.      * @since 1.4
  901.      */
  902.     public static String sha384Hex(final InputStream data) throws IOException {
  903.         return Hex.encodeHexString(sha384(data));
  904.     }

  905.     /**
  906.      * Calculates the SHA-384 digest and returns the value as a hexadecimal string.
  907.      *
  908.      * @param data Data to digest
  909.      * @return SHA-384 digest as a hexadecimal string
  910.      * @since 1.4
  911.      */
  912.     public static String sha384Hex(final String data) {
  913.         return Hex.encodeHexString(sha384(data));
  914.     }

  915.     /**
  916.      * Calculates the SHA-512 digest and returns the value as a {@code byte[]}.
  917.      *
  918.      * @param data Data to digest
  919.      * @return SHA-512 digest
  920.      * @since 1.4
  921.      */
  922.     public static byte[] sha512(final byte[] data) {
  923.         return getSha512Digest().digest(data);
  924.     }

  925.     /**
  926.      * Calculates the SHA-512 digest and returns the value as a {@code byte[]}.
  927.      *
  928.      * @param data Data to digest
  929.      * @return SHA-512 digest
  930.      * @throws IOException On error reading from the stream
  931.      * @since 1.4
  932.      */
  933.     public static byte[] sha512(final InputStream data) throws IOException {
  934.         return digest(getSha512Digest(), data);
  935.     }

  936.     /**
  937.      * Calculates the SHA-512 digest and returns the value as a {@code byte[]}.
  938.      *
  939.      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  940.      * @return SHA-512 digest
  941.      * @since 1.4
  942.      */
  943.     public static byte[] sha512(final String data) {
  944.         return sha512(StringUtils.getBytesUtf8(data));
  945.     }

  946.     /**
  947.      * Calculates the SHA-512/224 digest and returns the value as a {@code byte[]}.
  948.      *
  949.      * @param data Data to digest
  950.      * @return SHA-512/224 digest
  951.      * @since 1.14
  952.      */
  953.     public static byte[] sha512_224(final byte[] data) {
  954.         return getSha512_224Digest().digest(data);
  955.     }

  956.     /**
  957.      * Calculates the SHA-512/224 digest and returns the value as a {@code byte[]}.
  958.      *
  959.      * @param data Data to digest
  960.      * @return SHA-512/224 digest
  961.      * @throws IOException On error reading from the stream
  962.      * @since 1.14
  963.      */
  964.     public static byte[] sha512_224(final InputStream data) throws IOException {
  965.         return digest(getSha512_224Digest(), data);
  966.     }

  967.     /**
  968.      * Calculates the SHA-512/224 digest and returns the value as a {@code byte[]}.
  969.      *
  970.      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  971.      * @return SHA-512/224 digest
  972.      * @since 1.14
  973.      */
  974.     public static byte[] sha512_224(final String data) {
  975.         return sha512_224(StringUtils.getBytesUtf8(data));
  976.     }

  977.     /**
  978.      * Calculates the SHA-512/224 digest and returns the value as a hexadecimal string.
  979.      *
  980.      * @param data Data to digest
  981.      * @return SHA-512/224 digest as a hexadecimal string
  982.      * @since 1.14
  983.      */
  984.     public static String sha512_224Hex(final byte[] data) {
  985.         return Hex.encodeHexString(sha512_224(data));
  986.     }

  987.     /**
  988.      * Calculates the SHA-512/224 digest and returns the value as a hexadecimal string.
  989.      *
  990.      * @param data Data to digest
  991.      * @return SHA-512/224 digest as a hexadecimal string
  992.      * @throws IOException On error reading from the stream
  993.      * @since 1.14
  994.      */
  995.     public static String sha512_224Hex(final InputStream data) throws IOException {
  996.         return Hex.encodeHexString(sha512_224(data));
  997.     }

  998.     /**
  999.      * Calculates the SHA-512/224 digest and returns the value as a hexadecimal string.
  1000.      *
  1001.      * @param data Data to digest
  1002.      * @return SHA-512/224 digest as a hexadecimal string
  1003.      * @since 1.14
  1004.      */
  1005.     public static String sha512_224Hex(final String data) {
  1006.         return Hex.encodeHexString(sha512_224(data));
  1007.     }

  1008.     /**
  1009.      * Calculates the SHA-512/256 digest and returns the value as a {@code byte[]}.
  1010.      *
  1011.      * @param data Data to digest
  1012.      * @return SHA-512/256 digest
  1013.      * @since 1.14
  1014.      */
  1015.     public static byte[] sha512_256(final byte[] data) {
  1016.         return getSha512_256Digest().digest(data);
  1017.     }

  1018.     /**
  1019.      * Calculates the SHA-512/256 digest and returns the value as a {@code byte[]}.
  1020.      *
  1021.      * @param data Data to digest
  1022.      * @return SHA-512/256 digest
  1023.      * @throws IOException On error reading from the stream
  1024.      * @since 1.14
  1025.      */
  1026.     public static byte[] sha512_256(final InputStream data) throws IOException {
  1027.         return digest(getSha512_256Digest(), data);
  1028.     }

  1029.     /**
  1030.      * Calculates the SHA-512/256 digest and returns the value as a {@code byte[]}.
  1031.      *
  1032.      * @param data Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  1033.      * @return SHA-512/224 digest
  1034.      * @since 1.14
  1035.      */
  1036.     public static byte[] sha512_256(final String data) {
  1037.         return sha512_256(StringUtils.getBytesUtf8(data));
  1038.     }

  1039.     /**
  1040.      * Calculates the SHA-512/256 digest and returns the value as a hexadecimal string.
  1041.      *
  1042.      * @param data Data to digest
  1043.      * @return SHA-512/256 digest as a hexadecimal string
  1044.      * @since 1.14
  1045.      */
  1046.     public static String sha512_256Hex(final byte[] data) {
  1047.         return Hex.encodeHexString(sha512_256(data));
  1048.     }

  1049.     /**
  1050.      * Calculates the SHA-512/256 digest and returns the value as a hexadecimal string.
  1051.      *
  1052.      * @param data Data to digest
  1053.      * @return SHA-512/256 digest as a hexadecimal string
  1054.      * @throws IOException On error reading from the stream
  1055.      * @since 1.14
  1056.      */
  1057.     public static String sha512_256Hex(final InputStream data) throws IOException {
  1058.         return Hex.encodeHexString(sha512_256(data));
  1059.     }

  1060.     /**
  1061.      * Calculates the SHA-512/256 digest and returns the value as a hexadecimal string.
  1062.      *
  1063.      * @param data Data to digest
  1064.      * @return SHA-512/256 digest as a hexadecimal string
  1065.      * @since 1.14
  1066.      */
  1067.     public static String sha512_256Hex(final String data) {
  1068.         return Hex.encodeHexString(sha512_256(data));
  1069.     }

  1070.     /**
  1071.      * Calculates the SHA-512 digest and returns the value as a hexadecimal string.
  1072.      *
  1073.      * @param data Data to digest
  1074.      * @return SHA-512 digest as a hexadecimal string
  1075.      * @since 1.4
  1076.      */
  1077.     public static String sha512Hex(final byte[] data) {
  1078.         return Hex.encodeHexString(sha512(data));
  1079.     }

  1080.     /**
  1081.      * Calculates the SHA-512 digest and returns the value as a hexadecimal string.
  1082.      *
  1083.      * @param data Data to digest
  1084.      * @return SHA-512 digest as a hexadecimal string
  1085.      * @throws IOException On error reading from the stream
  1086.      * @since 1.4
  1087.      */
  1088.     public static String sha512Hex(final InputStream data) throws IOException {
  1089.         return Hex.encodeHexString(sha512(data));
  1090.     }

  1091.     /**
  1092.      * Calculates the SHA-512 digest and returns the value as a hexadecimal string.
  1093.      *
  1094.      * @param data Data to digest
  1095.      * @return SHA-512 digest as a hexadecimal string
  1096.      * @since 1.4
  1097.      */
  1098.     public static String sha512Hex(final String data) {
  1099.         return Hex.encodeHexString(sha512(data));
  1100.     }

  1101.     /**
  1102.      * Calculates the SHA-1 digest and returns the value as a hexadecimal string.
  1103.      *
  1104.      * @param data Data to digest
  1105.      * @return SHA-1 digest as a hexadecimal string
  1106.      * @deprecated (1.11) Use {@link #sha1Hex(byte[])}
  1107.      */
  1108.     @Deprecated
  1109.     public static String shaHex(final byte[] data) {
  1110.         return sha1Hex(data);
  1111.     }

  1112.     /**
  1113.      * Calculates the SHA-1 digest and returns the value as a hexadecimal string.
  1114.      *
  1115.      * @param data Data to digest
  1116.      * @return SHA-1 digest as a hexadecimal string
  1117.      * @throws IOException On error reading from the stream
  1118.      * @since 1.4
  1119.      * @deprecated (1.11) Use {@link #sha1Hex(InputStream)}
  1120.      */
  1121.     @Deprecated
  1122.     public static String shaHex(final InputStream data) throws IOException {
  1123.         return sha1Hex(data);
  1124.     }

  1125.     /**
  1126.      * Calculates the SHA-1 digest and returns the value as a hexadecimal string.
  1127.      *
  1128.      * @param data Data to digest
  1129.      * @return SHA-1 digest as a hexadecimal string
  1130.      * @deprecated (1.11) Use {@link #sha1Hex(String)}
  1131.      */
  1132.     @Deprecated
  1133.     public static String shaHex(final String data) {
  1134.         return sha1Hex(data);
  1135.     }

  1136.     /**
  1137.      * Updates the given {@link MessageDigest}.
  1138.      *
  1139.      * @param messageDigest the {@link MessageDigest} to update
  1140.      * @param valueToDigest the value to update the {@link MessageDigest} with
  1141.      * @return the updated {@link MessageDigest}
  1142.      * @since 1.7
  1143.      */
  1144.     public static MessageDigest updateDigest(final MessageDigest messageDigest, final byte[] valueToDigest) {
  1145.         messageDigest.update(valueToDigest);
  1146.         return messageDigest;
  1147.     }

  1148.     /**
  1149.      * Updates the given {@link MessageDigest}.
  1150.      *
  1151.      * @param messageDigest the {@link MessageDigest} to update
  1152.      * @param valueToDigest the value to update the {@link MessageDigest} with
  1153.      * @return the updated {@link MessageDigest}
  1154.      * @since 1.11
  1155.      */
  1156.     public static MessageDigest updateDigest(final MessageDigest messageDigest, final ByteBuffer valueToDigest) {
  1157.         messageDigest.update(valueToDigest);
  1158.         return messageDigest;
  1159.     }

  1160.     /**
  1161.      * Reads through a File and updates the digest for the data
  1162.      *
  1163.      * @param digest The MessageDigest to use (for example MD5)
  1164.      * @param data   Data to digest
  1165.      * @return the digest
  1166.      * @throws IOException On error reading from the stream
  1167.      * @since 1.11
  1168.      */
  1169.     public static MessageDigest updateDigest(final MessageDigest digest, final File data) throws IOException {
  1170.         try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(data))) {
  1171.             return updateDigest(digest, inputStream);
  1172.         }
  1173.     }

  1174.     /**
  1175.      * Reads through a RandomAccessFile and updates the digest for the data using non-blocking-io (NIO).
  1176.      *
  1177.      * TODO Decide if this should be public.
  1178.      *
  1179.      * @param digest The MessageDigest to use (for example MD5)
  1180.      * @param data   Data to digest
  1181.      * @return the digest
  1182.      * @throws IOException On error reading from the stream
  1183.      * @since 1.14
  1184.      */
  1185.     private static MessageDigest updateDigest(final MessageDigest digest, final FileChannel data) throws IOException {
  1186.         final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
  1187.         while (data.read(buffer) > 0) {
  1188.             buffer.flip();
  1189.             digest.update(buffer);
  1190.             buffer.clear();
  1191.         }
  1192.         return digest;
  1193.     }

  1194.     /**
  1195.      * Reads through an InputStream and updates the digest for the data
  1196.      *
  1197.      * @param digest      The MessageDigest to use (for example MD5)
  1198.      * @param inputStream Data to digest
  1199.      * @return the digest
  1200.      * @throws IOException On error reading from the stream
  1201.      * @since 1.8
  1202.      */
  1203.     public static MessageDigest updateDigest(final MessageDigest digest, final InputStream inputStream) throws IOException {
  1204.         final byte[] buffer = new byte[BUFFER_SIZE];
  1205.         int read = inputStream.read(buffer, 0, BUFFER_SIZE);

  1206.         while (read > -1) {
  1207.             digest.update(buffer, 0, read);
  1208.             read = inputStream.read(buffer, 0, BUFFER_SIZE);
  1209.         }

  1210.         return digest;
  1211.     }

  1212.     /**
  1213.      * Reads through a Path and updates the digest for the data
  1214.      *
  1215.      * @param digest  The MessageDigest to use (for example MD5)
  1216.      * @param path    Data to digest
  1217.      * @param options options How to open the file
  1218.      * @return the digest
  1219.      * @throws IOException On error reading from the stream
  1220.      * @since 1.14
  1221.      */
  1222.     public static MessageDigest updateDigest(final MessageDigest digest, final Path path, final OpenOption... options) throws IOException {
  1223.         try (BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(path, options))) {
  1224.             return updateDigest(digest, inputStream);
  1225.         }
  1226.     }

  1227.     /**
  1228.      * Reads through a RandomAccessFile and updates the digest for the data using non-blocking-io (NIO)
  1229.      *
  1230.      * @param digest The MessageDigest to use (for example MD5)
  1231.      * @param data   Data to digest
  1232.      * @return the digest
  1233.      * @throws IOException On error reading from the stream
  1234.      * @since 1.14
  1235.      */
  1236.     @SuppressWarnings("resource") // Closing RandomAccessFile closes the channel.
  1237.     public static MessageDigest updateDigest(final MessageDigest digest, final RandomAccessFile data) throws IOException {
  1238.         return updateDigest(digest, data.getChannel());
  1239.     }

  1240.     /**
  1241.      * Updates the given {@link MessageDigest} from a String (converted to bytes using UTF-8).
  1242.      * <p>
  1243.      * To update the digest using a different charset for the conversion, convert the String to a byte array using
  1244.      * {@link String#getBytes(java.nio.charset.Charset)} and pass that to the {@link DigestUtils#updateDigest(MessageDigest, byte[])} method
  1245.      *
  1246.      * @param messageDigest the {@link MessageDigest} to update
  1247.      * @param valueToDigest the value to update the {@link MessageDigest} with; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
  1248.      * @return the updated {@link MessageDigest}
  1249.      * @since 1.7
  1250.      */
  1251.     public static MessageDigest updateDigest(final MessageDigest messageDigest, final String valueToDigest) {
  1252.         messageDigest.update(StringUtils.getBytesUtf8(valueToDigest));
  1253.         return messageDigest;
  1254.     }

  1255.     private final MessageDigest messageDigest;

  1256.     /**
  1257.      * Preserves binary compatibility only. As for previous versions does not provide useful behavior
  1258.      *
  1259.      * @deprecated since 1.11; only useful to preserve binary compatibility
  1260.      */
  1261.     @Deprecated
  1262.     public DigestUtils() {
  1263.         this.messageDigest = null;
  1264.     }

  1265.     /**
  1266.      * Creates an instance using the provided {@link MessageDigest} parameter.
  1267.      *
  1268.      * This can then be used to create digests using methods such as {@link #digest(byte[])} and {@link #digestAsHex(File)}.
  1269.      *
  1270.      * @param digest the {@link MessageDigest} to use
  1271.      * @since 1.11
  1272.      */
  1273.     public DigestUtils(final MessageDigest digest) {
  1274.         this.messageDigest = digest;
  1275.     }

  1276.     /**
  1277.      * Creates an instance using the provided {@link MessageDigest} parameter.
  1278.      *
  1279.      * This can then be used to create digests using methods such as {@link #digest(byte[])} and {@link #digestAsHex(File)}.
  1280.      *
  1281.      * @param name the name of the {@link MessageDigest} to use
  1282.      * @see #getDigest(String)
  1283.      * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
  1284.      * @since 1.11
  1285.      */
  1286.     public DigestUtils(final String name) {
  1287.         this(getDigest(name));
  1288.     }

  1289.     /**
  1290.      * Reads through a byte array and returns the digest for the data.
  1291.      *
  1292.      * @param data Data to digest
  1293.      * @return the digest
  1294.      * @since 1.11
  1295.      */
  1296.     public byte[] digest(final byte[] data) {
  1297.         return updateDigest(messageDigest, data).digest();
  1298.     }

  1299.     /**
  1300.      * Reads through a ByteBuffer and returns the digest for the data
  1301.      *
  1302.      * @param data Data to digest
  1303.      * @return the digest
  1304.      * @since 1.11
  1305.      */
  1306.     public byte[] digest(final ByteBuffer data) {
  1307.         return updateDigest(messageDigest, data).digest();
  1308.     }

  1309.     /**
  1310.      * Reads through a File and returns the digest for the data
  1311.      *
  1312.      * @param data Data to digest
  1313.      * @return the digest
  1314.      * @throws IOException On error reading from the stream
  1315.      * @since 1.11
  1316.      */
  1317.     public byte[] digest(final File data) throws IOException {
  1318.         return updateDigest(messageDigest, data).digest();
  1319.     }

  1320.     /**
  1321.      * Reads through an InputStream and returns the digest for the data
  1322.      *
  1323.      * @param data Data to digest
  1324.      * @return the digest
  1325.      * @throws IOException On error reading from the stream
  1326.      * @since 1.11
  1327.      */
  1328.     public byte[] digest(final InputStream data) throws IOException {
  1329.         return updateDigest(messageDigest, data).digest();
  1330.     }

  1331.     /**
  1332.      * Reads through a File and returns the digest for the data
  1333.      *
  1334.      * @param data    Data to digest
  1335.      * @param options options How to open the file
  1336.      * @return the digest
  1337.      * @throws IOException On error reading from the stream
  1338.      * @since 1.14
  1339.      */
  1340.     public byte[] digest(final Path data, final OpenOption... options) throws IOException {
  1341.         return updateDigest(messageDigest, data, options).digest();
  1342.     }

  1343.     /**
  1344.      * Reads through a byte array and returns the digest for the data.
  1345.      *
  1346.      * @param data Data to digest treated as UTF-8 string
  1347.      * @return the digest
  1348.      * @since 1.11
  1349.      */
  1350.     public byte[] digest(final String data) {
  1351.         return updateDigest(messageDigest, data).digest();
  1352.     }

  1353.     /**
  1354.      * Reads through a byte array and returns the digest for the data.
  1355.      *
  1356.      * @param data Data to digest
  1357.      * @return the digest as a hexadecimal string
  1358.      * @since 1.11
  1359.      */
  1360.     public String digestAsHex(final byte[] data) {
  1361.         return Hex.encodeHexString(digest(data));
  1362.     }

  1363.     /**
  1364.      * Reads through a ByteBuffer and returns the digest for the data
  1365.      *
  1366.      * @param data Data to digest
  1367.      * @return the digest as a hexadecimal string
  1368.      * @since 1.11
  1369.      */
  1370.     public String digestAsHex(final ByteBuffer data) {
  1371.         return Hex.encodeHexString(digest(data));
  1372.     }

  1373.     /**
  1374.      * Reads through a File and returns the digest for the data
  1375.      *
  1376.      * @param data Data to digest
  1377.      * @return the digest as a hexadecimal string
  1378.      * @throws IOException On error reading from the stream
  1379.      * @since 1.11
  1380.      */
  1381.     public String digestAsHex(final File data) throws IOException {
  1382.         return Hex.encodeHexString(digest(data));
  1383.     }

  1384.     /**
  1385.      * Reads through an InputStream and returns the digest for the data
  1386.      *
  1387.      * @param data Data to digest
  1388.      * @return the digest as a hexadecimal string
  1389.      * @throws IOException On error reading from the stream
  1390.      * @since 1.11
  1391.      */
  1392.     public String digestAsHex(final InputStream data) throws IOException {
  1393.         return Hex.encodeHexString(digest(data));
  1394.     }

  1395.     /**
  1396.      * Reads through a File and returns the digest for the data
  1397.      *
  1398.      * @param data    Data to digest
  1399.      * @param options options How to open the file
  1400.      * @return the digest as a hexadecimal string
  1401.      * @throws IOException On error reading from the stream
  1402.      * @since 1.11
  1403.      */
  1404.     public String digestAsHex(final Path data, final OpenOption... options) throws IOException {
  1405.         return Hex.encodeHexString(digest(data, options));
  1406.     }

  1407.     /**
  1408.      * Reads through a byte array and returns the digest for the data.
  1409.      *
  1410.      * @param data Data to digest treated as UTF-8 string
  1411.      * @return the digest as a hexadecimal string
  1412.      * @since 1.11
  1413.      */
  1414.     public String digestAsHex(final String data) {
  1415.         return Hex.encodeHexString(digest(data));
  1416.     }

  1417.     /**
  1418.      * Returns the message digest instance.
  1419.      *
  1420.      * @return the message digest instance
  1421.      * @since 1.11
  1422.      */
  1423.     public MessageDigest getMessageDigest() {
  1424.         return messageDigest;
  1425.     }

  1426. }