Skip to content

Commit fdb6743

Browse files
committed
[CODEC-157] DigestUtils: Add MD2 APIs.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1380018 13f79535-47bb-0310-9956-ffa450edef68
1 parent 531b5dc commit fdb6743

3 files changed

Lines changed: 159 additions & 14 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ The <action> type attribute can be add,update,fix,remove.
5151
</release>
5252
-->
5353
<release version="1.7" date="TBD" description="Feature and fix release.">
54+
<action issue="CODEC-157" dev="ggregory" type="add" due-to="ggregory">
55+
DigestUtils: Add MD2 APIs.
56+
</action>
5457
<action issue="CODEC-156" dev="ggregory" type="add" due-to="ggregory">
5558
DigestUtils: add APIs named after standard alg name SHA-1.
5659
</action>

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

Lines changed: 103 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ public static MessageDigest getDigest(String algorithm) {
8989
}
9090
}
9191

92+
/**
93+
* Returns an MD2 MessageDigest.
94+
*
95+
* @return An MD2 digest instance.
96+
* @throws IllegalArgumentException
97+
* when a {@link NoSuchAlgorithmException} is caught, which should never happen because MD2 is a
98+
* built-in algorithm
99+
* @see MessageDigestAlgorithms#MD2
100+
*/
101+
public static MessageDigest getMd2Digest() {
102+
return getDigest(MessageDigestAlgorithms.MD2);
103+
}
104+
92105
/**
93106
* Returns an MD5 MessageDigest.
94107
*
@@ -175,6 +188,82 @@ public static MessageDigest getShaDigest() {
175188
return getDigest("SHA");
176189
}
177190

191+
/**
192+
* Calculates the MD2 digest and returns the value as a 16 element <code>byte[]</code>.
193+
*
194+
* @param data
195+
* Data to digest
196+
* @return MD2 digest
197+
* @since 1.7
198+
*/
199+
public static byte[] md2(byte[] data) {
200+
return getMd2Digest().digest(data);
201+
}
202+
203+
/**
204+
* Calculates the MD2 digest and returns the value as a 16 element <code>byte[]</code>.
205+
*
206+
* @param data
207+
* Data to digest
208+
* @return MD2 digest
209+
* @throws IOException
210+
* On error reading from the stream
211+
* @since 1.7
212+
*/
213+
public static byte[] md2(InputStream data) throws IOException {
214+
return digest(getMd2Digest(), data);
215+
}
216+
217+
/**
218+
* Calculates the MD2 digest and returns the value as a 16 element <code>byte[]</code>.
219+
*
220+
* @param data
221+
* Data to digest
222+
* @return MD2 digest
223+
* @since 1.7
224+
*/
225+
public static byte[] md2(String data) {
226+
return md2(getBytesUtf8(data));
227+
}
228+
229+
/**
230+
* Calculates the MD2 digest and returns the value as a 32 character hex string.
231+
*
232+
* @param data
233+
* Data to digest
234+
* @return MD2 digest as a hex string
235+
* @since 1.7
236+
*/
237+
public static String md2Hex(byte[] data) {
238+
return Hex.encodeHexString(md2(data));
239+
}
240+
241+
/**
242+
* Calculates the MD2 digest and returns the value as a 32 character hex string.
243+
*
244+
* @param data
245+
* Data to digest
246+
* @return MD2 digest as a hex string
247+
* @throws IOException
248+
* On error reading from the stream
249+
* @since 1.7
250+
*/
251+
public static String md2Hex(InputStream data) throws IOException {
252+
return Hex.encodeHexString(md2(data));
253+
}
254+
255+
/**
256+
* Calculates the MD2 digest and returns the value as a 32 character hex string.
257+
*
258+
* @param data
259+
* Data to digest
260+
* @return MD2 digest as a hex string
261+
* @since 1.7
262+
*/
263+
public static String md2Hex(String data) {
264+
return Hex.encodeHexString(md2(data));
265+
}
266+
178267
/**
179268
* Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
180269
*
@@ -264,10 +353,12 @@ public static byte[] sha(byte[] data) {
264353
* @param data
265354
* Data to digest
266355
* @return SHA-1 digest
267-
* @since 1.7
356+
* @throws IOException
357+
* On error reading from the stream
358+
* @since 1.4
268359
*/
269-
public static byte[] sha1(byte[] data) {
270-
return getSha1Digest().digest(data);
360+
public static byte[] sha(InputStream data) throws IOException {
361+
return digest(getShaDigest(), data);
271362
}
272363

273364
/**
@@ -276,12 +367,9 @@ public static byte[] sha1(byte[] data) {
276367
* @param data
277368
* Data to digest
278369
* @return SHA-1 digest
279-
* @throws IOException
280-
* On error reading from the stream
281-
* @since 1.4
282370
*/
283-
public static byte[] sha(InputStream data) throws IOException {
284-
return digest(getShaDigest(), data);
371+
public static byte[] sha(String data) {
372+
return sha(getBytesUtf8(data));
285373
}
286374

287375
/**
@@ -290,12 +378,10 @@ public static byte[] sha(InputStream data) throws IOException {
290378
* @param data
291379
* Data to digest
292380
* @return SHA-1 digest
293-
* @throws IOException
294-
* On error reading from the stream
295381
* @since 1.7
296382
*/
297-
public static byte[] sha1(InputStream data) throws IOException {
298-
return digest(getSha1Digest(), data);
383+
public static byte[] sha1(byte[] data) {
384+
return getSha1Digest().digest(data);
299385
}
300386

301387
/**
@@ -304,9 +390,12 @@ public static byte[] sha1(InputStream data) throws IOException {
304390
* @param data
305391
* Data to digest
306392
* @return SHA-1 digest
393+
* @throws IOException
394+
* On error reading from the stream
395+
* @since 1.7
307396
*/
308-
public static byte[] sha(String data) {
309-
return sha(getBytesUtf8(data));
397+
public static byte[] sha1(InputStream data) throws IOException {
398+
return digest(getSha1Digest(), data);
310399
}
311400

312401
/**

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,31 @@ public void testInternalNoSuchAlgorithmException() {
6565
}
6666
}
6767

68+
@Test
69+
public void testMd2Hex() throws IOException {
70+
// Examples from RFC 1319
71+
assertEquals("8350e5a3e24c153df2275c9f80692773", DigestUtils.md2Hex(""));
72+
73+
assertEquals("32ec01ec4a6dac72c0ab96fb34c0b5d1", DigestUtils.md2Hex("a"));
74+
75+
assertEquals("da853b0d3f88d99b30283a69e6ded6bb", DigestUtils.md2Hex("abc"));
76+
77+
assertEquals("ab4f496bfb2a530b219ff33031fe06b0", DigestUtils.md2Hex("message digest"));
78+
79+
assertEquals("4e8ddff3650292ab5a4108c3aa47940b", DigestUtils.md2Hex("abcdefghijklmnopqrstuvwxyz"));
80+
81+
assertEquals(
82+
"da33def2a42df13975352846c30338cd",
83+
DigestUtils.md2Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"));
84+
85+
assertEquals(
86+
"d5976f79d83d3a0dc9806c3c66f3efd8",
87+
DigestUtils.md2Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890"));
88+
89+
assertEquals(DigestUtils.md2Hex(testData),
90+
DigestUtils.md2Hex(new ByteArrayInputStream(testData)));
91+
}
92+
6893
@Test
6994
public void testMd5Hex() throws IOException {
7095
// Examples from RFC 1321
@@ -90,6 +115,20 @@ public void testMd5Hex() throws IOException {
90115
DigestUtils.md5Hex(new ByteArrayInputStream(testData)));
91116
}
92117

118+
/**
119+
* An MD2 hash converted to hex should always be 32 characters.
120+
*/
121+
@Test
122+
public void testMd2HexLength() {
123+
String hashMe = "this is some string that is longer than 32 characters";
124+
String hash = DigestUtils.md2Hex(getBytesUtf8(hashMe));
125+
assertEquals(32, hash.length());
126+
127+
hashMe = "length < 32";
128+
hash = DigestUtils.md2Hex(getBytesUtf8(hashMe));
129+
assertEquals(32, hash.length());
130+
}
131+
93132
/**
94133
* An MD5 hash converted to hex should always be 32 characters.
95134
*/
@@ -104,6 +143,20 @@ public void testMD5HexLength() {
104143
assertEquals(32, hash.length());
105144
}
106145

146+
/**
147+
* An MD2 hash should always be a 16 element byte[].
148+
*/
149+
@Test
150+
public void testMd2Length() {
151+
String hashMe = "this is some string that is longer than 16 characters";
152+
byte[] hash = DigestUtils.md2(getBytesUtf8(hashMe));
153+
assertEquals(16, hash.length);
154+
155+
hashMe = "length < 16";
156+
hash = DigestUtils.md2(getBytesUtf8(hashMe));
157+
assertEquals(16, hash.length);
158+
}
159+
107160
/**
108161
* An MD5 hash should always be a 16 element byte[].
109162
*/

0 commit comments

Comments
 (0)