Skip to content

Commit a82070e

Browse files
committed
[CODEC-211] Create enum MessageDigestAlgorithm and deprecate class MessageDigestAlgorithms
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1743778 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2d31526 commit a82070e

6 files changed

Lines changed: 342 additions & 19 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The <action> type attribute can be add,update,fix,remove.
4747
<action dev="ggregory" type="fix" issue="CODEC-207" due-to="Gary Gregory">Charsets Javadoc breaks build when using Java 8</action>
4848
<action dev="ggregory" type="fix" issue="CODEC-199" due-to="Yossi Tamari">Bug in HW rule in Soundex</action>
4949
<action dev="ggregory" type="fix" issue="CODEC-209" due-to="Gary Gregory">Javadoc for SHA-224 DigestUtils methods should mention Java 1.8.0 restriction instead of 1.4.0.</action>
50+
<action dev="ggregory" type="add" issue="CODEC-211" due-to="Gary Gregory">Create enum MessageDigestAlgorithm and deprecate class MessageDigestAlgorithms</action>
5051
<action dev="ggregory" type="add" issue="CODEC-210" due-to="Gary Gregory">Add DigestUtils.getDigest(String, MessageDigest)</action>
5152
<action dev="ggregory" type="add" issue="CODEC-208" due-to="Gary Gregory">Make some DigestUtils APIs public</action>
5253
<action dev="ggregory" type="add" issue="CODEC-206" due-to="Gary Gregory">Add java.io.File APIs to DigestUtils</action>

src/main/java/org/apache/commons/codec/digest/DigestUtils.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ public class DigestUtils {
3939

4040
private static final int STREAM_BUFFER_LENGTH = 1024;
4141

42+
/**
43+
* Read through an ByteBuffer and returns the digest for the data. Provided for symmetry with other methods.
44+
*
45+
* @param messageDigest
46+
* The MessageDigest to use (e.g. MD5)
47+
* @param data
48+
* Data to digest
49+
* @return the digest
50+
* @throws IOException
51+
* On error reading from the stream
52+
* @since 1.11
53+
*/
54+
public static byte[] digest(final MessageDigest messageDigest, final byte[] data) {
55+
return messageDigest.digest(data);
56+
}
57+
4258
/**
4359
* Read through an ByteBuffer and returns the digest for the data
4460
*
@@ -110,14 +126,16 @@ public static MessageDigest getDigest(final String algorithm) {
110126
}
111127

112128
/**
113-
* Returns a <code>MessageDigest</code> for the given <code>algorithm</code> or a default if there is a problem getting the algorithm.
129+
* Returns a <code>MessageDigest</code> for the given <code>algorithm</code> or a default if there is a problem
130+
* getting the algorithm.
114131
*
115132
* @param algorithm
116-
* the name of the algorithm requested. See <a
117-
* href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA"
118-
* >Appendix A in the Java Cryptography Architecture Reference Guide</a> for information about standard
133+
* the name of the algorithm requested. See
134+
* <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA" >
135+
* Appendix A in the Java Cryptography Architecture Reference Guide</a> for information about standard
119136
* algorithm names.
120-
* @param defaultMessageDigest The default MessageDigest.
137+
* @param defaultMessageDigest
138+
* The default MessageDigest.
121139
* @return A digest instance.
122140
* @see MessageDigest#getInstance(String)
123141
* @throws IllegalArgumentException
@@ -143,7 +161,7 @@ public static MessageDigest getDigest(final String algorithm, MessageDigest defa
143161
* @since 1.7
144162
*/
145163
public static MessageDigest getMd2Digest() {
146-
return getDigest(MessageDigestAlgorithms.MD2);
164+
return MessageDigestAlgorithm.MD2.getMessageDigest();
147165
}
148166

149167
/**
@@ -156,7 +174,7 @@ public static MessageDigest getMd2Digest() {
156174
* @see MessageDigestAlgorithms#MD5
157175
*/
158176
public static MessageDigest getMd5Digest() {
159-
return getDigest(MessageDigestAlgorithms.MD5);
177+
return MessageDigestAlgorithm.MD5.getMessageDigest();
160178
}
161179

162180
/**
@@ -170,7 +188,7 @@ public static MessageDigest getMd5Digest() {
170188
* @since 1.7
171189
*/
172190
public static MessageDigest getSha1Digest() {
173-
return getDigest(MessageDigestAlgorithms.SHA_1);
191+
return MessageDigestAlgorithm.SHA_1.getMessageDigest();
174192
}
175193

176194
/**
@@ -186,7 +204,7 @@ public static MessageDigest getSha1Digest() {
186204
* @see MessageDigestAlgorithms#SHA_224
187205
*/
188206
public static MessageDigest getSha224Digest() {
189-
return getDigest(MessageDigestAlgorithms.SHA_224);
207+
return MessageDigestAlgorithm.SHA_224.getMessageDigest();
190208
}
191209

192210
/**
@@ -202,7 +220,7 @@ public static MessageDigest getSha224Digest() {
202220
* @see MessageDigestAlgorithms#SHA_256
203221
*/
204222
public static MessageDigest getSha256Digest() {
205-
return getDigest(MessageDigestAlgorithms.SHA_256);
223+
return MessageDigestAlgorithm.SHA_256.getMessageDigest();
206224
}
207225

208226
/**
@@ -218,7 +236,7 @@ public static MessageDigest getSha256Digest() {
218236
* @see MessageDigestAlgorithms#SHA_384
219237
*/
220238
public static MessageDigest getSha384Digest() {
221-
return getDigest(MessageDigestAlgorithms.SHA_384);
239+
return MessageDigestAlgorithm.SHA_384.getMessageDigest();
222240
}
223241

224242
/**
@@ -234,7 +252,7 @@ public static MessageDigest getSha384Digest() {
234252
* @see MessageDigestAlgorithms#SHA_512
235253
*/
236254
public static MessageDigest getSha512Digest() {
237-
return getDigest(MessageDigestAlgorithms.SHA_512);
255+
return MessageDigestAlgorithm.SHA_512.getMessageDigest();
238256
}
239257

240258
/**
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.codec.digest;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.nio.ByteBuffer;
24+
import java.security.MessageDigest;
25+
import java.security.NoSuchAlgorithmException;
26+
27+
/**
28+
* Standard {@link MessageDigest} algorithm names from the <cite>Java Cryptography Architecture Standard Algorithm Name
29+
* Documentation</cite>.
30+
* <p>
31+
* This enum is immutable and thread-safe.
32+
* </p>
33+
*
34+
* @see <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html">Java Cryptography
35+
* Architecture Standard Algorithm Name Documentation</a>
36+
* @since 1.11
37+
* @version $Id: MessageDigestAlgorithm.java 1637936 2014-11-10 16:47:29Z ggregory $
38+
*/
39+
public enum MessageDigestAlgorithm {
40+
41+
/**
42+
* The MD2 message digest algorithm defined in RFC 1319.
43+
*/
44+
MD2("MD2"),
45+
46+
/**
47+
* The MD5 message digest algorithm defined in RFC 1321.
48+
*/
49+
MD5("MD5"),
50+
51+
/**
52+
* The SHA-1 hash algorithm defined in the FIPS PUB 180-2.
53+
*/
54+
SHA_1("SHA-1"),
55+
56+
/**
57+
* The SHA-224 hash algorithm defined in the FIPS PUB 180-4.
58+
* <p>
59+
* Java 8 only.
60+
* </p>
61+
*/
62+
SHA_224("SHA-224"),
63+
64+
/**
65+
* The SHA-256 hash algorithm defined in the FIPS PUB 180-2.
66+
*/
67+
SHA_256("SHA-256"),
68+
69+
/**
70+
* The SHA-384 hash algorithm defined in the FIPS PUB 180-2.
71+
*/
72+
SHA_384("SHA-384"),
73+
74+
/**
75+
* The SHA-512 hash algorithm defined in the FIPS PUB 180-2.
76+
*/
77+
SHA_512("SHA-512");
78+
79+
private final String algorithm;
80+
81+
private MessageDigestAlgorithm(String algorithm) {
82+
this.algorithm = algorithm;
83+
}
84+
85+
/**
86+
* Read through a byte[] and returns the digest for the data
87+
*
88+
* @param data
89+
* Data to digest
90+
* @return the digest
91+
* @throws IOException
92+
* On error reading from the stream
93+
*/
94+
public byte[] digest(byte[] data) throws IOException {
95+
return getMessageDigest().digest(data);
96+
}
97+
98+
/**
99+
* Read through a ByteBuffer and returns the digest for the data
100+
*
101+
* @param data
102+
* Data to digest
103+
* @return the digest
104+
* @throws IOException
105+
* On error reading from the stream
106+
*/
107+
public byte[] digest(ByteBuffer data) throws IOException {
108+
return DigestUtils.digest(getMessageDigest(), data);
109+
}
110+
111+
/**
112+
* Read through a File and returns the digest for the data
113+
*
114+
* @param data
115+
* Data to digest
116+
* @return the digest
117+
* @throws IOException
118+
* On error reading from the stream
119+
*/
120+
public byte[] digest(File data) throws IOException {
121+
return DigestUtils.digest(getMessageDigest(), data);
122+
}
123+
124+
/**
125+
* Read through an InputStream and returns the digest for the data
126+
*
127+
* @param data
128+
* Data to digest
129+
* @return the digest
130+
* @throws IOException
131+
* On error reading from the stream
132+
*/
133+
public byte[] digest(InputStream data) throws IOException {
134+
return DigestUtils.digest(getMessageDigest(), data);
135+
}
136+
137+
/**
138+
* Gets the algorithm name.
139+
*
140+
* @return the algorithm name.
141+
*/
142+
public String getAlgorithm() {
143+
return algorithm;
144+
}
145+
146+
/**
147+
* Returns a <code>MessageDigest</code> for this <code>algorithm</code>.
148+
*
149+
* @return A digest instance.
150+
* @see MessageDigest#getInstance(String)
151+
* @throws IllegalArgumentException
152+
* when a {@link NoSuchAlgorithmException} is caught.
153+
*/
154+
public MessageDigest getMessageDigest() {
155+
return DigestUtils.getDigest(algorithm);
156+
}
157+
158+
/**
159+
* Whether a MessageDigest for this algorithm can be created.
160+
*
161+
* @return Whether a MessageDigest for this algorithm can be created.
162+
*/
163+
public boolean isAvailable() {
164+
return DigestUtils.getDigest(algorithm, null) != null;
165+
}
166+
167+
}

src/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
* <p>
2626
* This class is immutable and thread-safe.
2727
* </p>
28-
* TODO 2.0 This should be an enum.
2928
*
3029
* @see <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html">Java Cryptography
3130
* Architecture Standard Algorithm Name Documentation</a>
3231
* @since 1.7
32+
* @deprecated Use the enum {@link MessageDigestAlgorithm}.
3333
* @version $Id$
3434
*/
35+
@Deprecated
3536
public class MessageDigestAlgorithms {
3637

3738
private MessageDigestAlgorithms() {
@@ -54,11 +55,11 @@ private MessageDigestAlgorithms() {
5455
public static final String SHA_1 = "SHA-1";
5556

5657
/**
57-
* The SHA-224 hash algorithm defined in the FIPS PUB 180-4.
58+
* The SHA-224 hash algorithm defined in the FIPS PUB 180-4.
5859
* <p>
5960
* Java 8 only.
6061
* </p>
61-
*
62+
*
6263
* @since 1.11
6364
*/
6465
public static final String SHA_224 = "SHA-224";

src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ public class DigestUtilsTest {
5050

5151
private File testFile;
5252

53+
private void assumeJava8() {
54+
Assume.assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8));
55+
}
56+
57+
byte[] getTestData() {
58+
return testData;
59+
}
60+
61+
File getTestFile() {
62+
return testFile;
63+
}
64+
5365
@Before
5466
public void setUp() throws Exception {
5567
new Random().nextBytes(testData);
@@ -311,10 +323,6 @@ public void testSha224() throws IOException {
311323
// Examples from FIPS 180-4?
312324
}
313325

314-
private void assumeJava8() {
315-
Assume.assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8));
316-
}
317-
318326
@Test
319327
public void testSha224HexFile() throws IOException {
320328
assumeJava8();

0 commit comments

Comments
 (0)