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