Skip to content

Commit e941b7e

Browse files
committed
[CODEC-211] Create enum MessageDigestAlgorithm and deprecate class MessageDigestAlgorithms. Add "Hex" APIs.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1744140 13f79535-47bb-0310-9956-ffa450edef68
1 parent c116dde commit e941b7e

3 files changed

Lines changed: 755 additions & 1 deletion

File tree

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.security.MessageDigest;
2525
import java.security.NoSuchAlgorithmException;
2626

27+
import org.apache.commons.codec.binary.Hex;
28+
2729
/**
2830
* Standard {@link MessageDigest} algorithm names from the <cite>Java Cryptography Architecture Standard Algorithm Name
2931
* Documentation</cite>.
@@ -40,45 +42,69 @@ public enum MessageDigestAlgorithm {
4042

4143
/**
4244
* The MD2 message digest algorithm defined in RFC 1319.
45+
* <p>
46+
* This MAC algorithm is <em>optional</em>; not all implementations support it.
47+
* </p>
4348
*/
4449
MD2("MD2"),
4550

4651
/**
4752
* The MD5 message digest algorithm defined in RFC 1321.
53+
* <p>
54+
* Every implementation of the Java platform is required to support this standard MAC algorithm.
55+
* </p>
4856
*/
4957
MD5("MD5"),
5058

5159
/**
5260
* The SHA-1 hash algorithm defined in the FIPS PUB 180-2.
61+
* <p>
62+
* Every implementation of the Java platform is required to support this standard MAC algorithm.
63+
* </p>
5364
*/
5465
SHA_1("SHA-1"),
5566

5667
/**
5768
* The SHA-224 hash algorithm defined in the FIPS PUB 180-4.
5869
* <p>
70+
* This MAC algorithm is <em>optional</em>; not all implementations support it.
71+
* </p>
72+
* <p>
5973
* Java 8 only.
6074
* </p>
6175
*/
6276
SHA_224("SHA-224"),
6377

6478
/**
6579
* The SHA-256 hash algorithm defined in the FIPS PUB 180-2.
80+
* <p>
81+
* Every implementation of the Java platform is required to support this standard MAC algorithm.
82+
* </p>
6683
*/
6784
SHA_256("SHA-256"),
6885

6986
/**
7087
* The SHA-384 hash algorithm defined in the FIPS PUB 180-2.
88+
* <p>
89+
* This MAC algorithm is <em>optional</em>; not all implementations support it.
90+
* </p>
7191
*/
7292
SHA_384("SHA-384"),
7393

7494
/**
7595
* The SHA-512 hash algorithm defined in the FIPS PUB 180-2.
96+
* <p>
97+
* This MAC algorithm is <em>optional</em>; not all implementations support it.
98+
* </p>
7699
*/
77100
SHA_512("SHA-512"),
78101

79102
/**
80103
* The SHA3-224 hash algorithm defined in the NIST FIPS 202.
81104
* <p>
105+
* This MAC algorithm is <em>optional</em>; not all implementations support it.
106+
* </p>
107+
* <p>
82108
* Java 9 only.
83109
* </p>
84110
*/
@@ -87,6 +113,9 @@ public enum MessageDigestAlgorithm {
87113
/**
88114
* The SHA3-256 hash algorithm defined in the NIST FIPS 202.
89115
* <p>
116+
* This MAC algorithm is <em>optional</em>; not all implementations support it.
117+
* </p>
118+
* <p>
90119
* Java 9 only.
91120
* </p>
92121
*/
@@ -95,6 +124,9 @@ public enum MessageDigestAlgorithm {
95124
/**
96125
* The SHA3-384 hash algorithm defined in the NIST FIPS 202.
97126
* <p>
127+
* This MAC algorithm is <em>optional</em>; not all implementations support it.
128+
* </p>
129+
* <p>
98130
* Java 9 only.
99131
* </p>
100132
*/
@@ -103,6 +135,9 @@ public enum MessageDigestAlgorithm {
103135
/**
104136
* The SHA3-512 hash algorithm defined in the NIST FIPS 202.
105137
* <p>
138+
* This MAC algorithm is <em>optional</em>; not all implementations support it.
139+
* </p>
140+
* <p>
106141
* Java 9 only.
107142
* </p>
108143
*/
@@ -127,6 +162,19 @@ public byte[] digest(byte[] data) throws IOException {
127162
return getMessageDigest().digest(data);
128163
}
129164

165+
/**
166+
* Read through a byte[] and returns the digest for the data
167+
*
168+
* @param data
169+
* Data to digest
170+
* @return the digest
171+
* @throws IOException
172+
* On error reading from the stream
173+
*/
174+
public String digestHex(byte[] data) throws IOException {
175+
return Hex.encodeHexString(digest(data));
176+
}
177+
130178
/**
131179
* Read through a ByteBuffer and returns the digest for the data
132180
*
@@ -140,6 +188,19 @@ public byte[] digest(ByteBuffer data) throws IOException {
140188
return DigestUtils.digest(getMessageDigest(), data);
141189
}
142190

191+
/**
192+
* Read through a ByteBuffer and returns the digest for the data
193+
*
194+
* @param data
195+
* Data to digest
196+
* @return the digest
197+
* @throws IOException
198+
* On error reading from the stream
199+
*/
200+
public String digestHex(ByteBuffer data) throws IOException {
201+
return Hex.encodeHexString(digest(data));
202+
}
203+
143204
/**
144205
* Read through a File and returns the digest for the data
145206
*
@@ -153,6 +214,19 @@ public byte[] digest(File data) throws IOException {
153214
return DigestUtils.digest(getMessageDigest(), data);
154215
}
155216

217+
/**
218+
* Read through a File and returns the digest for the data
219+
*
220+
* @param data
221+
* Data to digest
222+
* @return the digest
223+
* @throws IOException
224+
* On error reading from the stream
225+
*/
226+
public String digestHex(File data) throws IOException {
227+
return Hex.encodeHexString(digest(data));
228+
}
229+
156230
/**
157231
* Read through an InputStream and returns the digest for the data
158232
*
@@ -166,6 +240,19 @@ public byte[] digest(InputStream data) throws IOException {
166240
return DigestUtils.digest(getMessageDigest(), data);
167241
}
168242

243+
/**
244+
* Read through an InputStream and returns the digest for the data
245+
*
246+
* @param data
247+
* Data to digest
248+
* @return the digest
249+
* @throws IOException
250+
* On error reading from the stream
251+
*/
252+
public String digestHex(InputStream data) throws IOException {
253+
return Hex.encodeHexString(digest(data));
254+
}
255+
169256
/**
170257
* Gets the algorithm name.
171258
*

0 commit comments

Comments
 (0)