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