001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.util;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.security.MessageDigest;
022import java.security.NoSuchAlgorithmException;
023
024/**
025 * Miscellaneous methods for calculating digests.
026 *
027 * <p>Mainly for internal use within the framework; consider
028 * <a href="https://commons.apache.org/codec/">Apache Commons Codec</a>
029 * for a more comprehensive suite of digest utilities.
030 *
031 * @author Arjen Poutsma
032 * @author Juergen Hoeller
033 * @author Craig Andrews
034 * @since 3.0
035 */
036public abstract class DigestUtils {
037
038        private static final String MD5_ALGORITHM_NAME = "MD5";
039
040        private static final char[] HEX_CHARS =
041                        {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
042
043
044        /**
045         * Calculate the MD5 digest of the given bytes.
046         * @param bytes the bytes to calculate the digest over
047         * @return the digest
048         */
049        public static byte[] md5Digest(byte[] bytes) {
050                return digest(MD5_ALGORITHM_NAME, bytes);
051        }
052
053        /**
054         * Calculate the MD5 digest of the given stream.
055         * @param inputStream the InputStream to calculate the digest over
056         * @return the digest
057         * @since 4.2
058         */
059        public static byte[] md5Digest(InputStream inputStream) throws IOException {
060                return digest(MD5_ALGORITHM_NAME, inputStream);
061        }
062
063        /**
064         * Return a hexadecimal string representation of the MD5 digest of the given bytes.
065         * @param bytes the bytes to calculate the digest over
066         * @return a hexadecimal digest string
067         */
068        public static String md5DigestAsHex(byte[] bytes) {
069                return digestAsHexString(MD5_ALGORITHM_NAME, bytes);
070        }
071
072        /**
073         * Return a hexadecimal string representation of the MD5 digest of the given stream.
074         * @param inputStream the InputStream to calculate the digest over
075         * @return a hexadecimal digest string
076         * @since 4.2
077         */
078        public static String md5DigestAsHex(InputStream inputStream) throws IOException {
079                return digestAsHexString(MD5_ALGORITHM_NAME, inputStream);
080        }
081
082        /**
083         * Append a hexadecimal string representation of the MD5 digest of the given
084         * bytes to the given {@link StringBuilder}.
085         * @param bytes the bytes to calculate the digest over
086         * @param builder the string builder to append the digest to
087         * @return the given string builder
088         */
089        public static StringBuilder appendMd5DigestAsHex(byte[] bytes, StringBuilder builder) {
090                return appendDigestAsHex(MD5_ALGORITHM_NAME, bytes, builder);
091        }
092
093        /**
094         * Append a hexadecimal string representation of the MD5 digest of the given
095         * inputStream to the given {@link StringBuilder}.
096         * @param inputStream the inputStream to calculate the digest over
097         * @param builder the string builder to append the digest to
098         * @return the given string builder
099         * @since 4.2
100         */
101        public static StringBuilder appendMd5DigestAsHex(InputStream inputStream, StringBuilder builder) throws IOException {
102                return appendDigestAsHex(MD5_ALGORITHM_NAME, inputStream, builder);
103        }
104
105
106        /**
107         * Create a new {@link MessageDigest} with the given algorithm.
108         * Necessary because {@code MessageDigest} is not thread-safe.
109         */
110        private static MessageDigest getDigest(String algorithm) {
111                try {
112                        return MessageDigest.getInstance(algorithm);
113                }
114                catch (NoSuchAlgorithmException ex) {
115                        throw new IllegalStateException("Could not find MessageDigest with algorithm \"" + algorithm + "\"", ex);
116                }
117        }
118
119        private static byte[] digest(String algorithm, byte[] bytes) {
120                return getDigest(algorithm).digest(bytes);
121        }
122
123        private static byte[] digest(String algorithm, InputStream inputStream) throws IOException {
124                MessageDigest messageDigest = getDigest(algorithm);
125                if (inputStream instanceof UpdateMessageDigestInputStream){
126                        ((UpdateMessageDigestInputStream) inputStream).updateMessageDigest(messageDigest);
127                        return messageDigest.digest();
128                }
129                else {
130                        final byte[] buffer = new byte[StreamUtils.BUFFER_SIZE];
131                        int bytesRead = -1;
132                        while ((bytesRead = inputStream.read(buffer)) != -1) {
133                                messageDigest.update(buffer, 0, bytesRead);
134                        }
135                        return messageDigest.digest();
136                }
137        }
138
139        private static String digestAsHexString(String algorithm, byte[] bytes) {
140                char[] hexDigest = digestAsHexChars(algorithm, bytes);
141                return new String(hexDigest);
142        }
143
144        private static String digestAsHexString(String algorithm, InputStream inputStream) throws IOException {
145                char[] hexDigest = digestAsHexChars(algorithm, inputStream);
146                return new String(hexDigest);
147        }
148
149        private static StringBuilder appendDigestAsHex(String algorithm, byte[] bytes, StringBuilder builder) {
150                char[] hexDigest = digestAsHexChars(algorithm, bytes);
151                return builder.append(hexDigest);
152        }
153
154        private static StringBuilder appendDigestAsHex(String algorithm, InputStream inputStream, StringBuilder builder)
155                        throws IOException {
156
157                char[] hexDigest = digestAsHexChars(algorithm, inputStream);
158                return builder.append(hexDigest);
159        }
160
161        private static char[] digestAsHexChars(String algorithm, byte[] bytes) {
162                byte[] digest = digest(algorithm, bytes);
163                return encodeHex(digest);
164        }
165
166        private static char[] digestAsHexChars(String algorithm, InputStream inputStream) throws IOException {
167                byte[] digest = digest(algorithm, inputStream);
168                return encodeHex(digest);
169        }
170
171        private static char[] encodeHex(byte[] bytes) {
172                char[] chars = new char[32];
173                for (int i = 0; i < chars.length; i = i + 2) {
174                        byte b = bytes[i / 2];
175                        chars[i] = HEX_CHARS[(b >>> 0x4) & 0xf];
176                        chars[i + 1] = HEX_CHARS[b & 0xf];
177                }
178                return chars;
179        }
180
181}