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