2323import java .security .NoSuchAlgorithmException ;
2424
2525import org .apache .commons .codec .binary .Hex ;
26+ import org .apache .commons .codec .binary .StringUtils ;
2627
2728/**
2829 * Operations to simplifiy common {@link java.security.MessageDigest} tasks. This class is thread safe.
@@ -37,23 +38,37 @@ public class DigestUtils {
3738 /**
3839 * Read through an InputStream and returns the digest for the data
3940 *
40- * @param digest The MessageDigest to use (e.g. MD5)
41- * @param data Data to digest
41+ * @param digest
42+ * The MessageDigest to use (e.g. MD5)
43+ * @param data
44+ * Data to digest
4245 * @return MD5 digest
43- * @throws IOException On error reading from the stream
46+ * @throws IOException
47+ * On error reading from the stream
4448 */
4549 private static byte [] digest (MessageDigest digest , InputStream data ) throws IOException {
4650 byte [] buffer = new byte [STREAM_BUFFER_LENGTH ];
4751 int read = data .read (buffer , 0 , STREAM_BUFFER_LENGTH );
48-
49- while (read > -1 ) {
52+
53+ while (read > -1 ) {
5054 digest .update (buffer , 0 , read );
5155 read = data .read (buffer , 0 , STREAM_BUFFER_LENGTH );
5256 }
53-
57+
5458 return digest .digest ();
5559 }
5660
61+ /**
62+ * Calls {@link StringUtils#getBytesUtf8(String)}
63+ *
64+ * @param string
65+ * the String to encode
66+ * @return encoded bytes
67+ */
68+ private static byte [] getBytesUtf8 (String data ) {
69+ return StringUtils .getBytesUtf8 (data );
70+ }
71+
5772 /**
5873 * Returns a <code>MessageDigest</code> for the given <code>algorithm</code>.
5974 *
@@ -138,7 +153,7 @@ private static MessageDigest getSha512Digest() {
138153 private static MessageDigest getShaDigest () {
139154 return getDigest ("SHA" );
140155 }
141-
156+
142157 /**
143158 * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
144159 *
@@ -156,7 +171,8 @@ public static byte[] md5(byte[] data) {
156171 * @param data
157172 * Data to digest
158173 * @return MD5 digest
159- * @throws IOException On error reading from the stream
174+ * @throws IOException
175+ * On error reading from the stream
160176 * @since 1.4
161177 */
162178 public static byte [] md5 (InputStream data ) throws IOException {
@@ -171,7 +187,7 @@ public static byte[] md5(InputStream data) throws IOException {
171187 * @return MD5 digest
172188 */
173189 public static byte [] md5 (String data ) {
174- return md5 (data . getBytes ( ));
190+ return md5 (getBytesUtf8 ( data ));
175191 }
176192
177193 /**
@@ -191,7 +207,8 @@ public static String md5Hex(byte[] data) {
191207 * @param data
192208 * Data to digest
193209 * @return MD5 digest as a hex string
194- * @throws IOException On error reading from the stream
210+ * @throws IOException
211+ * On error reading from the stream
195212 * @since 1.4
196213 */
197214 public static String md5Hex (InputStream data ) throws IOException {
@@ -226,7 +243,8 @@ public static byte[] sha(byte[] data) {
226243 * @param data
227244 * Data to digest
228245 * @return SHA-1 digest
229- * @throws IOException On error reading from the stream
246+ * @throws IOException
247+ * On error reading from the stream
230248 */
231249 public static byte [] sha (InputStream data ) throws IOException {
232250 return digest (getShaDigest (), data );
@@ -240,7 +258,7 @@ public static byte[] sha(InputStream data) throws IOException {
240258 * @return SHA-1 digest
241259 */
242260 public static byte [] sha (String data ) {
243- return sha (data . getBytes ( ));
261+ return sha (getBytesUtf8 ( data ));
244262 }
245263
246264 /**
@@ -267,7 +285,8 @@ public static byte[] sha256(byte[] data) {
267285 * @param data
268286 * Data to digest
269287 * @return SHA-256 digest
270- * @throws IOException On error reading from the stream
288+ * @throws IOException
289+ * On error reading from the stream
271290 * @since 1.4
272291 */
273292 public static byte [] sha256 (InputStream data ) throws IOException {
@@ -286,7 +305,7 @@ public static byte[] sha256(InputStream data) throws IOException {
286305 * @since 1.4
287306 */
288307 public static byte [] sha256 (String data ) {
289- return sha256 (data . getBytes ( ));
308+ return sha256 (getBytesUtf8 ( data ));
290309 }
291310
292311 /**
@@ -313,7 +332,8 @@ public static String sha256Hex(byte[] data) {
313332 * @param data
314333 * Data to digest
315334 * @return SHA-256 digest as a hex string
316- * @throws IOException On error reading from the stream
335+ * @throws IOException
336+ * On error reading from the stream
317337 * @since 1.4
318338 */
319339 public static String sha256Hex (InputStream data ) throws IOException {
@@ -359,13 +379,14 @@ public static byte[] sha384(byte[] data) {
359379 * @param data
360380 * Data to digest
361381 * @return SHA-384 digest
362- * @throws IOException On error reading from the stream
382+ * @throws IOException
383+ * On error reading from the stream
363384 * @since 1.4
364385 */
365386 public static byte [] sha384 (InputStream data ) throws IOException {
366387 return digest (getSha384Digest (), data );
367388 }
368-
389+
369390 /**
370391 * Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.
371392 * <p>
@@ -378,7 +399,7 @@ public static byte[] sha384(InputStream data) throws IOException {
378399 * @since 1.4
379400 */
380401 public static byte [] sha384 (String data ) {
381- return sha384 (data . getBytes ( ));
402+ return sha384 (getBytesUtf8 ( data ));
382403 }
383404
384405 /**
@@ -405,7 +426,8 @@ public static String sha384Hex(byte[] data) {
405426 * @param data
406427 * Data to digest
407428 * @return SHA-384 digest as a hex string
408- * @throws IOException On error reading from the stream
429+ * @throws IOException
430+ * On error reading from the stream
409431 * @since 1.4
410432 */
411433 public static String sha384Hex (InputStream data ) throws IOException {
@@ -451,7 +473,8 @@ public static byte[] sha512(byte[] data) {
451473 * @param data
452474 * Data to digest
453475 * @return SHA-512 digest
454- * @throws IOException On error reading from the stream
476+ * @throws IOException
477+ * On error reading from the stream
455478 * @since 1.4
456479 */
457480 public static byte [] sha512 (InputStream data ) throws IOException {
@@ -470,7 +493,7 @@ public static byte[] sha512(InputStream data) throws IOException {
470493 * @since 1.4
471494 */
472495 public static byte [] sha512 (String data ) {
473- return sha512 (data . getBytes ( ));
496+ return sha512 (getBytesUtf8 ( data ));
474497 }
475498
476499 /**
@@ -497,7 +520,8 @@ public static String sha512Hex(byte[] data) {
497520 * @param data
498521 * Data to digest
499522 * @return SHA-512 digest as a hex string
500- * @throws IOException On error reading from the stream
523+ * @throws IOException
524+ * On error reading from the stream
501525 * @since 1.4
502526 */
503527 public static String sha512Hex (InputStream data ) throws IOException {
@@ -536,7 +560,8 @@ public static String shaHex(byte[] data) {
536560 * @param data
537561 * Data to digest
538562 * @return SHA-1 digest as a hex string
539- * @throws IOException On error reading from the stream
563+ * @throws IOException
564+ * On error reading from the stream
540565 */
541566 public static String shaHex (InputStream data ) throws IOException {
542567 return new String (Hex .encodeHex (sha (data )));
0 commit comments