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.IOException;
  21. import java.io.InputStream;
  22. import java.io.RandomAccessFile;
  23. import java.nio.ByteBuffer;
  24. import java.nio.channels.FileChannel;
  25. import java.nio.file.Files;
  26. import java.nio.file.OpenOption;
  27. import java.nio.file.Path;
  28. import java.security.MessageDigest;
  29. import java.security.NoSuchAlgorithmException;

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

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

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

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

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

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

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

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

  118.     /**
  119.      * Reads through a RandomAccessFile using non-blocking-io (NIO) and returns the digest for the data
  120.      *
  121.      * @param messageDigest The MessageDigest to use (for example MD5)
  122.      * @param data          Data to digest
  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 RandomAccessFile data) throws IOException {
  128.         return updateDigest(messageDigest, data).digest();
  129.     }

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

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

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

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

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

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

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

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

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

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

  254.     /**
  255.      * Gets an SHA3-512 digest.
  256.      *
  257.      * @return An SHA3-512 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_512
  260.      * @since 1.12
  261.      */
  262.     public static MessageDigest getSha3_512Digest() {
  263.         return getDigest(MessageDigestAlgorithms.SHA3_512);
  264.     }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  1252.     private final MessageDigest messageDigest;

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

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

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

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

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

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

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

  1328.     /**
  1329.      * Reads through a File and returns the digest for the data
  1330.      *
  1331.      * @param data    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 byte[] digest(final Path data, final OpenOption... options) throws IOException {
  1338.         return updateDigest(messageDigest, data, options).digest();
  1339.     }

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

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

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

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

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

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

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

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

  1423. }