1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 *http://www.apache.org/licenses/LICENSE-2.0
10 *11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */
17
18 packageorg.apache.commons.codec.digest;19
20 importjava.io.IOException;21 importjava.io.InputStream;22 importjava.security.MessageDigest;23 importjava.security.NoSuchAlgorithmException;24
25 importorg.apache.commons.codec.binary.Hex;26 importorg.apache.commons.codec.binary.StringUtils;27
28 /**
29 * Operations to simplify common {@linkjava.security.MessageDigest} tasks.30 * This class is immutable and thread-safe.31 *32 *@version$Id: DigestUtils.java 1634433 2014-10-27 01:10:47Z ggregory $33 */
34 public classDigestUtils {35
36 private static final int STREAM_BUFFER_LENGTH = 1024;37
38 /**
39 * Read through an InputStream and returns the digest for the data40 *41 *@paramdigest42 * The MessageDigest to use (e.g. MD5)43 *@paramdata44 * Data to digest45 *@returnthe digest46 *@throwsIOException47 * On error reading from the stream48 */
49 private static byte[] digest(final MessageDigest digest, final InputStream data) throwsIOException {50 returnupdateDigest(digest, data).digest();51 }52
53 /**
54 * Returns a MessageDigest
for the given algorithm
.55 *56 *@paramalgorithm57 * the name of the algorithm requested. See Appendix A in the Java Cryptography Architecture Reference Guide for information about standard60 * algorithm names.61 *@returnA digest instance.62 *@seeMessageDigest#getInstance(String)63 *@throwsIllegalArgumentException64 * when a {@linkNoSuchAlgorithmException} is caught.65 */
66 public static MessageDigest getDigest(finalString algorithm) {67 try{68 returnMessageDigest.getInstance(algorithm);69 } catch (finalNoSuchAlgorithmException e) {70 throw newIllegalArgumentException(e);71 }72 }73
74 /**
75 * Returns an MD2 MessageDigest.76 *77 *@returnAn MD2 digest instance.78 *@throwsIllegalArgumentException79 * when a {@linkNoSuchAlgorithmException} is caught, which should never happen because MD2 is a80 * built-in algorithm81 *@seeMessageDigestAlgorithms#MD282 *@since1.783 */
84 public staticMessageDigest getMd2Digest() {85 returngetDigest(MessageDigestAlgorithms.MD2);86 }87
88 /**
89 * Returns an MD5 MessageDigest.90 *91 *@returnAn MD5 digest instance.92 *@throwsIllegalArgumentException93 * when a {@linkNoSuchAlgorithmException} is caught, which should never happen because MD5 is a94 * built-in algorithm95 *@seeMessageDigestAlgorithms#MD596 */
97 public staticMessageDigest getMd5Digest() {98 returngetDigest(MessageDigestAlgorithms.MD5);99 }100
101 /**
102 * Returns an SHA-1 digest.103 *104 *@returnAn SHA-1 digest instance.105 *@throwsIllegalArgumentException106 * when a {@linkNoSuchAlgorithmException} is caught, which should never happen because SHA-1 is a107 * built-in algorithm108 *@seeMessageDigestAlgorithms#SHA_1109 *@since1.7110 */
111 public staticMessageDigest getSha1Digest() {112 returngetDigest(MessageDigestAlgorithms.SHA_1);113 }114
115 /**
116 * Returns an SHA-256 digest.117 *
118 * Throws a RuntimeException
on JRE versions prior to 1.4.0.119 *
127 public staticMessageDigest getSha256Digest() {128 returngetDigest(MessageDigestAlgorithms.SHA_256);129 }130
131 /**
132 * Returns an SHA-384 digest.133 *
134 * Throws a RuntimeException
on JRE versions prior to 1.4.0.135 *
143 public staticMessageDigest getSha384Digest() {144 returngetDigest(MessageDigestAlgorithms.SHA_384);145 }146
147 /**
148 * Returns an SHA-512 digest.149 *
150 * Throws a RuntimeException
on JRE versions prior to 1.4.0.151 *
159 public staticMessageDigest getSha512Digest() {160 returngetDigest(MessageDigestAlgorithms.SHA_512);161 }162
163 /**
164 * Returns an SHA-1 digest.165 *166 *@returnAn SHA-1 digest instance.167 *@throwsIllegalArgumentException168 * when a {@linkNoSuchAlgorithmException} is caught169 *@deprecatedUse {@link#getSha1Digest()}170 */
171 @Deprecated172 public staticMessageDigest getShaDigest() {173 returngetSha1Digest();174 }175
176 /**
177 * Calculates the MD2 digest and returns the value as a 16 element byte[]
.178 *179 *@paramdata180 * Data to digest181 *@returnMD2 digest182 *@since1.7183 */
184 public static byte[] md2(final byte[] data) {185 returngetMd2Digest().digest(data);186 }187
188 /**
189 * Calculates the MD2 digest and returns the value as a 16 element byte[]
.190 *191 *@paramdata192 * Data to digest193 *@returnMD2 digest194 *@throwsIOException195 * On error reading from the stream196 *@since1.7197 */
198 public static byte[] md2(final InputStream data) throwsIOException {199 returndigest(getMd2Digest(), data);200 }201
202 /**
203 * Calculates the MD2 digest and returns the value as a 16 element byte[]
.204 *205 *@paramdata206 * Data to digest; converted to bytes using {@linkStringUtils#getBytesUtf8(String)}207 *@returnMD2 digest208 *@since1.7209 */
210 public static byte[] md2(finalString data) {211 returnmd2(StringUtils.getBytesUtf8(data));212 }213
214 /**
215 * Calculates the MD2 digest and returns the value as a 32 character hex string.216 *217 *@paramdata218 * Data to digest219 *@returnMD2 digest as a hex string220 *@since1.7221 */
222 public static String md2Hex(final byte[] data) {223 returnHex.encodeHexString(md2(data));224 }225
226 /**
227 * Calculates the MD2 digest and returns the value as a 32 character hex string.228 *229 *@paramdata230 * Data to digest231 *@returnMD2 digest as a hex string232 *@throwsIOException233 * On error reading from the stream234 *@since1.7235 */
236 public static String md2Hex(final InputStream data) throwsIOException {237 returnHex.encodeHexString(md2(data));238 }239
240 /**
241 * Calculates the MD2 digest and returns the value as a 32 character hex string.242 *243 *@paramdata244 * Data to digest245 *@returnMD2 digest as a hex string246 *@since1.7247 */
248 public static String md2Hex(finalString data) {249 returnHex.encodeHexString(md2(data));250 }251
252 /**
253 * Calculates the MD5 digest and returns the value as a 16 element byte[]
.254 *255 *@paramdata256 * Data to digest257 *@returnMD5 digest258 */
259 public static byte[] md5(final byte[] data) {260 returngetMd5Digest().digest(data);261 }262
263 /**
264 * Calculates the MD5 digest and returns the value as a 16 element byte[]
.265 *266 *@paramdata267 * Data to digest268 *@returnMD5 digest269 *@throwsIOException270 * On error reading from the stream271 *@since1.4272 */
273 public static byte[] md5(final InputStream data) throwsIOException {274 returndigest(getMd5Digest(), data);275 }276
277 /**
278 * Calculates the MD5 digest and returns the value as a 16 element byte[]
.279 *280 *@paramdata281 * Data to digest; converted to bytes using {@linkStringUtils#getBytesUtf8(String)}282 *@returnMD5 digest283 */
284 public static byte[] md5(finalString data) {285 returnmd5(StringUtils.getBytesUtf8(data));286 }287
288 /**
289 * Calculates the MD5 digest and returns the value as a 32 character hex string.290 *291 *@paramdata292 * Data to digest293 *@returnMD5 digest as a hex string294 */
295 public static String md5Hex(final byte[] data) {296 returnHex.encodeHexString(md5(data));297 }298
299 /**
300 * Calculates the MD5 digest and returns the value as a 32 character hex string.301 *302 *@paramdata303 * Data to digest304 *@returnMD5 digest as a hex string305 *@throwsIOException306 * On error reading from the stream307 *@since1.4308 */
309 public static String md5Hex(final InputStream data) throwsIOException {310 returnHex.encodeHexString(md5(data));311 }312
313 /**
314 * Calculates the MD5 digest and returns the value as a 32 character hex string.315 *316 *@paramdata317 * Data to digest318 *@returnMD5 digest as a hex string319 */
320 public static String md5Hex(finalString data) {321 returnHex.encodeHexString(md5(data));322 }323
324 /**
325 * Calculates the SHA-1 digest and returns the value as a byte[]
.326 *327 *@paramdata328 * Data to digest329 *@returnSHA-1 digest330 *@deprecatedUse {@link#sha1(byte[])}331 */
332 @Deprecated333 public static byte[] sha(final byte[] data) {334 returnsha1(data);335 }336
337 /**
338 * Calculates the SHA-1 digest and returns the value as a byte[]
.339 *340 *@paramdata341 * Data to digest342 *@returnSHA-1 digest343 *@throwsIOException344 * On error reading from the stream345 *@since1.4346 *@deprecatedUse {@link#sha1(InputStream)}347 */
348 @Deprecated349 public static byte[] sha(final InputStream data) throwsIOException {350 returnsha1(data);351 }352
353 /**
354 * Calculates the SHA-1 digest and returns the value as a byte[]
.355 *356 *@paramdata357 * Data to digest358 *@returnSHA-1 digest359 *@deprecatedUse {@link#sha1(String)}360 */
361 @Deprecated362 public static byte[] sha(finalString data) {363 returnsha1(data);364 }365
366 /**
367 * Calculates the SHA-1 digest and returns the value as a byte[]
.368 *369 *@paramdata370 * Data to digest371 *@returnSHA-1 digest372 *@since1.7373 */
374 public static byte[] sha1(final byte[] data) {375 returngetSha1Digest().digest(data);376 }377
378 /**
379 * Calculates the SHA-1 digest and returns the value as a byte[]
.380 *381 *@paramdata382 * Data to digest383 *@returnSHA-1 digest384 *@throwsIOException385 * On error reading from the stream386 *@since1.7387 */
388 public static byte[] sha1(final InputStream data) throwsIOException {389 returndigest(getSha1Digest(), data);390 }391
392 /**
393 * Calculates the SHA-1 digest and returns the value as a byte[]
.394 *395 *@paramdata396 * Data to digest; converted to bytes using {@linkStringUtils#getBytesUtf8(String)}397 *@returnSHA-1 digest398 */
399 public static byte[] sha1(finalString data) {400 returnsha1(StringUtils.getBytesUtf8(data));401 }402
403 /**
404 * Calculates the SHA-1 digest and returns the value as a hex string.405 *406 *@paramdata407 * Data to digest408 *@returnSHA-1 digest as a hex string409 *@since1.7410 */
411 public static String sha1Hex(final byte[] data) {412 returnHex.encodeHexString(sha1(data));413 }414
415 /**
416 * Calculates the SHA-1 digest and returns the value as a hex string.417 *418 *@paramdata419 * Data to digest420 *@returnSHA-1 digest as a hex string421 *@throwsIOException422 * On error reading from the stream423 *@since1.7424 */
425 public static String sha1Hex(final InputStream data) throwsIOException {426 returnHex.encodeHexString(sha1(data));427 }428
429 /**
430 * Calculates the SHA-1 digest and returns the value as a hex string.431 *432 *@paramdata433 * Data to digest434 *@returnSHA-1 digest as a hex string435 *@since1.7436 */
437 public static String sha1Hex(finalString data) {438 returnHex.encodeHexString(sha1(data));439 }440
441 /**
442 * Calculates the SHA-256 digest and returns the value as a byte[]
.443 *
444 * Throws a RuntimeException
on JRE versions prior to 1.4.0.445 *
452 public static byte[] sha256(final byte[] data) {453 returngetSha256Digest().digest(data);454 }455
456 /**
457 * Calculates the SHA-256 digest and returns the value as a byte[]
.458 *
459 * Throws a RuntimeException
on JRE versions prior to 1.4.0.460 *
469 public static byte[] sha256(final InputStream data) throwsIOException {470 returndigest(getSha256Digest(), data);471 }472
473 /**
474 * Calculates the SHA-256 digest and returns the value as a byte[]
.475 *
476 * Throws a RuntimeException
on JRE versions prior to 1.4.0.477 *
484 public static byte[] sha256(finalString data) {485 returnsha256(StringUtils.getBytesUtf8(data));486 }487
488 /**
489 * Calculates the SHA-256 digest and returns the value as a hex string.490 *
491 * Throws a RuntimeException
on JRE versions prior to 1.4.0.492 *
499 public static String sha256Hex(final byte[] data) {500 returnHex.encodeHexString(sha256(data));501 }502
503 /**
504 * Calculates the SHA-256 digest and returns the value as a hex string.505 *
506 * Throws a RuntimeException
on JRE versions prior to 1.4.0.507 *
516 public static String sha256Hex(final InputStream data) throwsIOException {517 returnHex.encodeHexString(sha256(data));518 }519
520 /**
521 * Calculates the SHA-256 digest and returns the value as a hex string.522 *
523 * Throws a RuntimeException
on JRE versions prior to 1.4.0.524 *
531 public static String sha256Hex(finalString data) {532 returnHex.encodeHexString(sha256(data));533 }534
535 /**
536 * Calculates the SHA-384 digest and returns the value as a byte[]
.537 *
538 * Throws a RuntimeException
on JRE versions prior to 1.4.0.539 *
546 public static byte[] sha384(final byte[] data) {547 returngetSha384Digest().digest(data);548 }549
550 /**
551 * Calculates the SHA-384 digest and returns the value as a byte[]
.552 *
553 * Throws a RuntimeException
on JRE versions prior to 1.4.0.554 *
563 public static byte[] sha384(final InputStream data) throwsIOException {564 returndigest(getSha384Digest(), data);565 }566
567 /**
568 * Calculates the SHA-384 digest and returns the value as a byte[]
.569 *
570 * Throws a RuntimeException
on JRE versions prior to 1.4.0.571 *
578 public static byte[] sha384(finalString data) {579 returnsha384(StringUtils.getBytesUtf8(data));580 }581
582 /**
583 * Calculates the SHA-384 digest and returns the value as a hex string.584 *
585 * Throws a RuntimeException
on JRE versions prior to 1.4.0.586 *
593 public static String sha384Hex(final byte[] data) {594 returnHex.encodeHexString(sha384(data));595 }596
597 /**
598 * Calculates the SHA-384 digest and returns the value as a hex string.599 *
600 * Throws a RuntimeException
on JRE versions prior to 1.4.0.601 *
610 public static String sha384Hex(final InputStream data) throwsIOException {611 returnHex.encodeHexString(sha384(data));612 }613
614 /**
615 * Calculates the SHA-384 digest and returns the value as a hex string.616 *
617 * Throws a RuntimeException
on JRE versions prior to 1.4.0.618 *
625 public static String sha384Hex(finalString data) {626 returnHex.encodeHexString(sha384(data));627 }628
629 /**
630 * Calculates the SHA-512 digest and returns the value as a byte[]
.631 *
632 * Throws a RuntimeException
on JRE versions prior to 1.4.0.633 *
640 public static byte[] sha512(final byte[] data) {641 returngetSha512Digest().digest(data);642 }643
644 /**
645 * Calculates the SHA-512 digest and returns the value as a byte[]
.646 *
647 * Throws a RuntimeException
on JRE versions prior to 1.4.0.648 *
657 public static byte[] sha512(final InputStream data) throwsIOException {658 returndigest(getSha512Digest(), data);659 }660
661 /**
662 * Calculates the SHA-512 digest and returns the value as a byte[]
.663 *
664 * Throws a RuntimeException
on JRE versions prior to 1.4.0.665 *
672 public static byte[] sha512(finalString data) {673 returnsha512(StringUtils.getBytesUtf8(data));674 }675
676 /**
677 * Calculates the SHA-512 digest and returns the value as a hex string.678 *
679 * Throws a RuntimeException
on JRE versions prior to 1.4.0.680 *
687 public static String sha512Hex(final byte[] data) {688 returnHex.encodeHexString(sha512(data));689 }690
691 /**
692 * Calculates the SHA-512 digest and returns the value as a hex string.693 *
694 * Throws a RuntimeException
on JRE versions prior to 1.4.0.695 *
704 public static String sha512Hex(final InputStream data) throwsIOException {705 returnHex.encodeHexString(sha512(data));706 }707
708 /**
709 * Calculates the SHA-512 digest and returns the value as a hex string.710 *
711 * Throws a RuntimeException
on JRE versions prior to 1.4.0.712 *
719 public static String sha512Hex(finalString data) {720 returnHex.encodeHexString(sha512(data));721 }722
723 /**
724 * Calculates the SHA-1 digest and returns the value as a hex string.725 *726 *@paramdata727 * Data to digest728 *@returnSHA-1 digest as a hex string729 *@deprecatedUse {@link#sha1Hex(byte[])}730 */
731 @Deprecated732 public static String shaHex(final byte[] data) {733 returnsha1Hex(data);734 }735
736 /**
737 * Calculates the SHA-1 digest and returns the value as a hex string.738 *739 *@paramdata740 * Data to digest741 *@returnSHA-1 digest as a hex string742 *@throwsIOException743 * On error reading from the stream744 *@since1.4745 *@deprecatedUse {@link#sha1Hex(InputStream)}746 */
747 @Deprecated748 public static String shaHex(final InputStream data) throwsIOException {749 returnsha1Hex(data);750 }751
752 /**
753 * Calculates the SHA-1 digest and returns the value as a hex string.754 *755 *@paramdata756 * Data to digest757 *@returnSHA-1 digest as a hex string758 *@deprecatedUse {@link#sha1Hex(String)}759 */
760 @Deprecated761 public static String shaHex(finalString data) {762 returnsha1Hex(data);763 }764
765 /**
766 * Updates the given {@linkMessageDigest}.767 *768 *@parammessageDigest769 * the {@linkMessageDigest} to update770 *@paramvalueToDigest771 * the value to update the {@linkMessageDigest} with772 *@returnthe updated {@linkMessageDigest}773 *@since1.7774 */
775 public static MessageDigest updateDigest(final MessageDigest messageDigest, final byte[] valueToDigest) {776 messageDigest.update(valueToDigest);777 returnmessageDigest;778 }779
780 /**
781 * Reads through an InputStream and updates the digest for the data782 *783 *@paramdigest784 * The MessageDigest to use (e.g. MD5)785 *@paramdata786 * Data to digest787 *@returnthe digest788 *@throwsIOException789 * On error reading from the stream790 *@since1.8791 */
792 public static MessageDigest updateDigest(final MessageDigest digest, final InputStream data) throwsIOException {793 final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];794 int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);795
796 while (read > -1) {797 digest.update(buffer, 0, read);798 read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);799 }800
801 returndigest;802 }803
804 /**
805 * Updates the given {@linkMessageDigest}.806 *807 *@parammessageDigest808 * the {@linkMessageDigest} to update809 *@paramvalueToDigest810 * the value to update the {@linkMessageDigest} with;811 * converted to bytes using {@linkStringUtils#getBytesUtf8(String)}812 *@returnthe updated {@linkMessageDigest}813 *@since1.7814 */
815 public static MessageDigest updateDigest(final MessageDigest messageDigest, finalString valueToDigest) {816 messageDigest.update(StringUtils.getBytesUtf8(valueToDigest));817 returnmessageDigest;818 }819 }