|
| 1 | +/* |
| 2 | + * Copyright 2002-2009 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.util; |
| 18 | + |
| 19 | +import java.security.MessageDigest; |
| 20 | +import java.security.NoSuchAlgorithmException; |
| 21 | + |
| 22 | +/** |
| 23 | + * Miscellaneous method for calculating digests. |
| 24 | +
|
| 25 | + * <p>Mainly for internal use within the framework; consider |
| 26 | + * <a href="http://jakarta.apache.org/commons/codec/">Jakarta's Commons Codec</a> |
| 27 | + * for a more comprehensive suite of Digest utilities. |
| 28 | + * |
| 29 | + * @author Arjen Poutsma |
| 30 | + * @since 3.0 |
| 31 | + * @see org.apache.commons.codec.digest.DigestUtils |
| 32 | + */ |
| 33 | +public abstract class DigestUtils { |
| 34 | + |
| 35 | + private static final String MD5_ALGORITHM_NAME = "MD5"; |
| 36 | + |
| 37 | + private static final char[] HEX_CHARS = |
| 38 | + {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
| 39 | + |
| 40 | + /** |
| 41 | + * Calculate the MD5 digest of the given bytes. |
| 42 | + * @param bytes the bytes to calculate the digest over |
| 43 | + * @return the digest |
| 44 | + */ |
| 45 | + public static byte[] md5Digest(byte[] bytes) { |
| 46 | + return digest(MD5_ALGORITHM_NAME, bytes); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Return a hexadecimal string representation of the MD5 digest of the given bytes. |
| 51 | + * @param bytes the bytes to calculate the digest over |
| 52 | + * @return a hexadecimal digest string |
| 53 | + */ |
| 54 | + public static String md5DigestAsHex(byte[] bytes) { |
| 55 | + return digestAsHexString(MD5_ALGORITHM_NAME, bytes); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Append a hexadecima string representation of the MD5 digest of the given bytes to the given {@link StringBuilder}. |
| 60 | + * @param bytes the bytes to calculate the digest over |
| 61 | + * @param builder the string builder to append the digest to |
| 62 | + * @return the given string builder |
| 63 | + */ |
| 64 | + public static StringBuilder appendMd5DigestAsHex(byte[] bytes, StringBuilder builder) { |
| 65 | + return appendDigestAsHex(MD5_ALGORITHM_NAME, bytes, builder); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates a new {@link MessageDigest} with the given algorithm. Necessary because {@code MessageDigest} is not |
| 70 | + * thread-safe. |
| 71 | + */ |
| 72 | + private static MessageDigest getDigest(String algorithm) { |
| 73 | + try { |
| 74 | + return MessageDigest.getInstance(algorithm); |
| 75 | + } |
| 76 | + catch (NoSuchAlgorithmException ex) { |
| 77 | + throw new IllegalStateException("Could not find MessageDigest with algorithm \"" + algorithm + "\"", ex); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static byte[] digest(String algorithm, byte[] bytes) { |
| 82 | + return getDigest(algorithm).digest(bytes); |
| 83 | + } |
| 84 | + |
| 85 | + private static String digestAsHexString(String algorithm, byte[] bytes) { |
| 86 | + char[] hexDigest = digestAsHexChars(algorithm, bytes); |
| 87 | + return new String(hexDigest); |
| 88 | + } |
| 89 | + |
| 90 | + private static StringBuilder appendDigestAsHex(String algorithm, byte[] bytes, StringBuilder builder) { |
| 91 | + char[] hexDigest = digestAsHexChars(algorithm, bytes); |
| 92 | + return builder.append(hexDigest); |
| 93 | + } |
| 94 | + |
| 95 | + private static char[] digestAsHexChars(String algorithm, byte[] bytes) { |
| 96 | + byte[] digest = digest(algorithm, bytes); |
| 97 | + return encodeHex(digest); |
| 98 | + } |
| 99 | + |
| 100 | + private static char[] encodeHex(byte[] bytes) { |
| 101 | + char chars[] = new char[32]; |
| 102 | + for (int i = 0; i < chars.length; i = i + 2) { |
| 103 | + byte b = bytes[i / 2]; |
| 104 | + chars[i] = HEX_CHARS[(b >>> 0x4) & 0xf]; |
| 105 | + chars[i + 1] = HEX_CHARS[b & 0xf]; |
| 106 | + } |
| 107 | + return chars; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | +} |
0 commit comments