Skip to content

Commit d415d36

Browse files
committed
Renamed Md5HashUtils to DigestUtils
1 parent 91297d9 commit d415d36

File tree

4 files changed

+123
-93
lines changed

4 files changed

+123
-93
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

org.springframework.core/src/main/java/org/springframework/util/Md5HashUtils.java

Lines changed: 0 additions & 80 deletions
This file was deleted.

org.springframework.core/src/test/java/org/springframework/util/Md5HashUtilsTests.java renamed to org.springframework.core/src/test/java/org/springframework/util/DigestUtilsTests.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright ${YEAR} the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,12 +18,11 @@
1818

1919
import java.io.UnsupportedEncodingException;
2020

21-
import static org.junit.Assert.assertArrayEquals;
22-
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.*;
2322
import org.junit.Before;
2423
import org.junit.Test;
2524

26-
public class Md5HashUtilsTests {
25+
public class DigestUtilsTests {
2726

2827
private byte[] bytes;
2928

@@ -33,23 +32,23 @@ public void createBytes() throws UnsupportedEncodingException {
3332
}
3433

3534
@Test
36-
public void hash() {
37-
byte[] result = Md5HashUtils.getHash(bytes);
35+
public void md5() {
36+
byte[] result = DigestUtils.md5Digest(bytes);
3837
byte[] expected = new byte[]{-0x4f, 0xa, -0x73, -0x4f, 0x64, -0x20, 0x75, 0x41, 0x5, -0x49, -0x57, -0x65, -0x19,
3938
0x2e, 0x3f, -0x1b};
4039
assertArrayEquals("Invalid hash", expected, result);
4140
}
4241

4342
@Test
44-
public void hashString() throws UnsupportedEncodingException {
45-
String hash = Md5HashUtils.getHashString(bytes);
43+
public void md5Hex() throws UnsupportedEncodingException {
44+
String hash = DigestUtils.md5DigestAsHex(bytes);
4645
assertEquals("Invalid hash", "b10a8db164e0754105b7a99be72e3fe5", hash);
4746
}
4847

4948
@Test
50-
public void hashStringBuilder() throws UnsupportedEncodingException {
49+
public void md5StringBuilder() throws UnsupportedEncodingException {
5150
StringBuilder builder = new StringBuilder();
52-
Md5HashUtils.appendHashString(bytes, builder);
51+
DigestUtils.appendMd5DigestAsHex(bytes, builder);
5352
assertEquals("Invalid hash", "b10a8db164e0754105b7a99be72e3fe5", builder.toString());
5453
}
5554

org.springframework.web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import javax.servlet.http.HttpServletResponse;
2929
import javax.servlet.http.HttpServletResponseWrapper;
3030

31+
import org.springframework.util.DigestUtils;
3132
import org.springframework.util.FileCopyUtils;
32-
import org.springframework.util.Md5HashUtils;
3333
import org.springframework.web.util.WebUtils;
3434

3535
/**
@@ -83,11 +83,11 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
8383
*
8484
* @param bytes the response bdoy as byte array
8585
* @return the ETag header value
86-
* @see Md5HashUtils
86+
* @see org.springframework.util.DigestUtils
8787
*/
8888
protected String generateETagHeaderValue(byte[] bytes) {
8989
StringBuilder builder = new StringBuilder("\"0");
90-
Md5HashUtils.appendHashString(bytes, builder);
90+
DigestUtils.appendMd5DigestAsHex(bytes, builder);
9191
builder.append('"');
9292
return builder.toString();
9393
}

0 commit comments

Comments
 (0)