Skip to content

Commit 803e5ee

Browse files
committed
[CODEC-193] Support java.nio.ByteBuffer in DigestUtils
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1637907 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4063338 commit 803e5ee

3 files changed

Lines changed: 243 additions & 4 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ The <action> type attribute can be add,update,fix,remove.
4444
<body>
4545
<release version="1.11" date="DD MM 2014" description="Feature and fix release.">
4646
<action dev="ggregory" type="add" issue="CODEC-194">Support java.nio.ByteBuffer in org.apache.commons.codec.binary.Hex</action>
47+
<action dev="ggregory" type="add" issue="CODEC-193">Support java.nio.ByteBuffer in DigestUtils</action>
4748
</release>
4849
<release version="1.10" date="5 November 2014" description="Feature and fix release.">
4950
<action dev="ggregory" type="add" issue="CODEC-192" due-to="Thomas Neidhart">Add Daitch-Mokotoff Soundex</action>

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

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.IOException;
2121
import java.io.InputStream;
22+
import java.nio.ByteBuffer;
2223
import java.security.MessageDigest;
2324
import java.security.NoSuchAlgorithmException;
2425

@@ -33,6 +34,22 @@
3334
*/
3435
public class DigestUtils {
3536

37+
/**
38+
* Read through an ByteBuffer and returns the digest for the data
39+
*
40+
* @param digest
41+
* The MessageDigest to use (e.g. MD5)
42+
* @param data
43+
* Data to digest
44+
* @return the digest
45+
* @throws IOException
46+
* On error reading from the stream
47+
*/
48+
private static byte[] digest(final MessageDigest messageDigest, final ByteBuffer data) {
49+
messageDigest.update(data);
50+
return messageDigest.digest();
51+
}
52+
3653
/**
3754
* Read through an InputStream and returns the digest for the data
3855
*
@@ -221,6 +238,18 @@ public static String md2Hex(final byte[] data) {
221238
return Hex.encodeHexString(md2(data));
222239
}
223240

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+
* @since 1.11
248+
*/
249+
public static String md2Hex(final ByteBuffer data) {
250+
return Hex.encodeHexString(md2(data));
251+
}
252+
224253
/**
225254
* Calculates the MD2 digest and returns the value as a 32 character hex string.
226255
*
@@ -258,6 +287,78 @@ public static byte[] md5(final byte[] data) {
258287
return getMd5Digest().digest(data);
259288
}
260289

290+
/**
291+
* Calculates the MD2 digest and returns the value as a 16 element <code>byte[]</code>.
292+
*
293+
* @param data
294+
* Data to digest
295+
* @return MD2 digest
296+
* @since 1.11
297+
*/
298+
public static byte[] md2(final ByteBuffer data) {
299+
return digest(getMd2Digest(), data);
300+
}
301+
302+
/**
303+
* Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
304+
*
305+
* @param data
306+
* Data to digest
307+
* @return SHA-1 digest
308+
* @since 1.11
309+
*/
310+
public static byte[] sha1(final ByteBuffer data) {
311+
return digest(getSha1Digest(), data);
312+
}
313+
314+
/**
315+
* Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.
316+
*
317+
* @param data
318+
* Data to digest
319+
* @return SHA-256 digest
320+
* @since 1.11
321+
*/
322+
public static byte[] sha256(final ByteBuffer data) {
323+
return digest(getSha256Digest(), data);
324+
}
325+
326+
/**
327+
* Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.
328+
*
329+
* @param data
330+
* Data to digest
331+
* @return SHA-384 digest
332+
* @since 1.11
333+
*/
334+
public static byte[] sha384(final ByteBuffer data) {
335+
return digest(getSha384Digest(), data);
336+
}
337+
338+
/**
339+
* Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>.
340+
*
341+
* @param data
342+
* Data to digest
343+
* @return SHA-512 digest
344+
* @since 1.11
345+
*/
346+
public static byte[] sha512(final ByteBuffer data) {
347+
return digest(getSha512Digest(), data);
348+
}
349+
350+
/**
351+
* Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
352+
*
353+
* @param data
354+
* Data to digest
355+
* @return MD5 digest
356+
* @since 1.11
357+
*/
358+
public static byte[] md5(final ByteBuffer data) {
359+
return digest(getMd5Digest(), data);
360+
}
361+
261362
/**
262363
* Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
263364
*
@@ -295,6 +396,18 @@ public static String md5Hex(final byte[] data) {
295396
}
296397

297398
/**
399+
* Calculates the MD5 digest and returns the value as a 32 character hex string.
400+
*
401+
* @param data
402+
* Data to digest
403+
* @return MD5 digest as a hex string
404+
* @since 1.11
405+
*/
406+
public static String md5Hex(final ByteBuffer data) {
407+
return Hex.encodeHexString(md5(data));
408+
}
409+
410+
/**
298411
* Calculates the MD5 digest and returns the value as a 32 character hex string.
299412
*
300413
* @param data
@@ -410,6 +523,18 @@ public static String sha1Hex(final byte[] data) {
410523
return Hex.encodeHexString(sha1(data));
411524
}
412525

526+
/**
527+
* Calculates the SHA-1 digest and returns the value as a hex string.
528+
*
529+
* @param data
530+
* Data to digest
531+
* @return SHA-1 digest as a hex string
532+
* @since 1.11
533+
*/
534+
public static String sha1Hex(final ByteBuffer data) {
535+
return Hex.encodeHexString(sha1(data));
536+
}
537+
413538
/**
414539
* Calculates the SHA-1 digest and returns the value as a hex string.
415540
*
@@ -498,6 +623,18 @@ public static String sha256Hex(final byte[] data) {
498623
return Hex.encodeHexString(sha256(data));
499624
}
500625

626+
/**
627+
* Calculates the SHA-256 digest and returns the value as a hex string.
628+
*
629+
* @param data
630+
* Data to digest
631+
* @return SHA-256 digest as a hex string
632+
* @since 1.11
633+
*/
634+
public static String sha256Hex(final ByteBuffer data) {
635+
return Hex.encodeHexString(sha256(data));
636+
}
637+
501638
/**
502639
* Calculates the SHA-256 digest and returns the value as a hex string.
503640
* <p>
@@ -592,6 +729,18 @@ public static String sha384Hex(final byte[] data) {
592729
return Hex.encodeHexString(sha384(data));
593730
}
594731

732+
/**
733+
* Calculates the SHA-384 digest and returns the value as a hex string.
734+
*
735+
* @param data
736+
* Data to digest
737+
* @return SHA-384 digest as a hex string
738+
* @since 1.11
739+
*/
740+
public static String sha384Hex(final ByteBuffer data) {
741+
return Hex.encodeHexString(sha384(data));
742+
}
743+
595744
/**
596745
* Calculates the SHA-384 digest and returns the value as a hex string.
597746
* <p>
@@ -686,6 +835,18 @@ public static String sha512Hex(final byte[] data) {
686835
return Hex.encodeHexString(sha512(data));
687836
}
688837

838+
/**
839+
* Calculates the SHA-512 digest and returns the value as a hex string.
840+
*
841+
* @param data
842+
* Data to digest
843+
* @return SHA-512 digest as a hex string
844+
* @since 1.11
845+
*/
846+
public static String sha512Hex(final ByteBuffer data) {
847+
return Hex.encodeHexString(sha512(data));
848+
}
849+
689850
/**
690851
* Calculates the SHA-512 digest and returns the value as a hex string.
691852
* <p>
@@ -775,6 +936,21 @@ public static MessageDigest updateDigest(final MessageDigest messageDigest, fina
775936
return messageDigest;
776937
}
777938

939+
/**
940+
* Updates the given {@link MessageDigest}.
941+
*
942+
* @param messageDigest
943+
* the {@link MessageDigest} to update
944+
* @param valueToDigest
945+
* the value to update the {@link MessageDigest} with
946+
* @return the updated {@link MessageDigest}
947+
* @since 1.11
948+
*/
949+
public static MessageDigest updateDigest(final MessageDigest messageDigest, final ByteBuffer valueToDigest) {
950+
messageDigest.update(valueToDigest);
951+
return messageDigest;
952+
}
953+
778954
/**
779955
* Reads through an InputStream and updates the digest for the data
780956
*

0 commit comments

Comments
 (0)