|
19 | 19 |
|
20 | 20 | import java.io.IOException; |
21 | 21 | import java.io.InputStream; |
| 22 | +import java.nio.ByteBuffer; |
22 | 23 | import java.security.MessageDigest; |
23 | 24 | import java.security.NoSuchAlgorithmException; |
24 | 25 |
|
|
33 | 34 | */ |
34 | 35 | public class DigestUtils { |
35 | 36 |
|
| 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 | + |
36 | 53 | /** |
37 | 54 | * Read through an InputStream and returns the digest for the data |
38 | 55 | * |
@@ -221,6 +238,18 @@ public static String md2Hex(final byte[] data) { |
221 | 238 | return Hex.encodeHexString(md2(data)); |
222 | 239 | } |
223 | 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 | + * @since 1.11 |
| 248 | + */ |
| 249 | + public static String md2Hex(final ByteBuffer data) { |
| 250 | + return Hex.encodeHexString(md2(data)); |
| 251 | + } |
| 252 | + |
224 | 253 | /** |
225 | 254 | * Calculates the MD2 digest and returns the value as a 32 character hex string. |
226 | 255 | * |
@@ -258,6 +287,78 @@ public static byte[] md5(final byte[] data) { |
258 | 287 | return getMd5Digest().digest(data); |
259 | 288 | } |
260 | 289 |
|
| 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 | + |
261 | 362 | /** |
262 | 363 | * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>. |
263 | 364 | * |
@@ -295,6 +396,18 @@ public static String md5Hex(final byte[] data) { |
295 | 396 | } |
296 | 397 |
|
297 | 398 | /** |
| 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 | +/** |
298 | 411 | * Calculates the MD5 digest and returns the value as a 32 character hex string. |
299 | 412 | * |
300 | 413 | * @param data |
@@ -410,6 +523,18 @@ public static String sha1Hex(final byte[] data) { |
410 | 523 | return Hex.encodeHexString(sha1(data)); |
411 | 524 | } |
412 | 525 |
|
| 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 | + |
413 | 538 | /** |
414 | 539 | * Calculates the SHA-1 digest and returns the value as a hex string. |
415 | 540 | * |
@@ -498,6 +623,18 @@ public static String sha256Hex(final byte[] data) { |
498 | 623 | return Hex.encodeHexString(sha256(data)); |
499 | 624 | } |
500 | 625 |
|
| 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 | + |
501 | 638 | /** |
502 | 639 | * Calculates the SHA-256 digest and returns the value as a hex string. |
503 | 640 | * <p> |
@@ -592,6 +729,18 @@ public static String sha384Hex(final byte[] data) { |
592 | 729 | return Hex.encodeHexString(sha384(data)); |
593 | 730 | } |
594 | 731 |
|
| 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 | + |
595 | 744 | /** |
596 | 745 | * Calculates the SHA-384 digest and returns the value as a hex string. |
597 | 746 | * <p> |
@@ -686,6 +835,18 @@ public static String sha512Hex(final byte[] data) { |
686 | 835 | return Hex.encodeHexString(sha512(data)); |
687 | 836 | } |
688 | 837 |
|
| 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 | + |
689 | 850 | /** |
690 | 851 | * Calculates the SHA-512 digest and returns the value as a hex string. |
691 | 852 | * <p> |
@@ -775,6 +936,21 @@ public static MessageDigest updateDigest(final MessageDigest messageDigest, fina |
775 | 936 | return messageDigest; |
776 | 937 | } |
777 | 938 |
|
| 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 | + |
778 | 954 | /** |
779 | 955 | * Reads through an InputStream and updates the digest for the data |
780 | 956 | * |
|
0 commit comments