@@ -273,68 +273,91 @@ public static String rightPad(String input, int length, char pad) {
273
273
return input ;
274
274
}
275
275
276
+
276
277
/**
277
- * Calculates the SHA1 of the string.
278
+ * Calculates the hash sum of the byte array.
279
+ *
280
+ * @param bytes
281
+ * byte array to hash
282
+ * @param algorithm
283
+ * Message digest algorithm name, e.g MD5, SHA-1 or SHA-256.
284
+ * @return sha sum of the byte array
285
+ */
286
+ private static String getDigest (byte [] bytes , String algorithm )
287
+ {
288
+ try {
289
+ MessageDigest md = MessageDigest .getInstance (algorithm );
290
+ md .update (bytes , 0 , bytes .length );
291
+ byte [] digest = md .digest ();
292
+ return toHex (digest );
293
+ } catch (NoSuchAlgorithmException t ) {
294
+ throw new RuntimeException (t );
295
+ }
296
+ }
297
+
298
+
299
+ /**
300
+ * Calculates the hash of the string.
278
301
*
279
302
* @param text
303
+ * string to hash
304
+ * @param algorithm
305
+ * Message digest algorithm name, e.g MD5, SHA-1 or SHA-256.
280
306
* @return sha1 of the string
281
307
*/
282
- public static String getSHA1 (String text ) {
308
+ private static String getDigest (String text , String algorithm )
309
+ {
283
310
try {
284
311
byte [] bytes = text .getBytes ("iso-8859-1" );
285
- return getSHA1 (bytes );
312
+ return getDigest (bytes , algorithm );
286
313
} catch (UnsupportedEncodingException u ) {
287
314
throw new RuntimeException (u );
288
315
}
289
316
}
290
317
318
+ /**
319
+ * Calculates the SHA1 of the string.
320
+ *
321
+ * @param text
322
+ * @return sha1 of the string
323
+ */
324
+ public static String getSHA1 (String text )
325
+ {
326
+ return getDigest (text , "SHA-1" );
327
+ }
328
+
291
329
/**
292
330
* Calculates the SHA1 of the byte array.
293
331
*
294
332
* @param bytes
295
333
* @return sha1 of the byte array
296
334
*/
297
- public static String getSHA1 (byte [] bytes ) {
298
- try {
299
- MessageDigest md = MessageDigest .getInstance ("SHA-1" );
300
- md .update (bytes , 0 , bytes .length );
301
- byte [] digest = md .digest ();
302
- return toHex (digest );
303
- } catch (NoSuchAlgorithmException t ) {
304
- throw new RuntimeException (t );
305
- }
335
+ public static String getSHA1 (byte [] bytes )
336
+ {
337
+ return getDigest (bytes , "SHA-1" );
338
+ }
306
339
}
307
340
308
341
/**
309
342
* Calculates the MD5 of the string.
310
343
*
311
- * @param string
344
+ * @param text
312
345
* @return md5 of the string
313
346
*/
314
- public static String getMD5 (String string ) {
315
- try {
316
- return getMD5 (string .getBytes ("iso-8859-1" ));
317
- } catch (UnsupportedEncodingException u ) {
318
- throw new RuntimeException (u );
319
- }
347
+ public static String getMD5 (String text )
348
+ {
349
+ return getDigest (text , "MD5" );
320
350
}
321
351
322
352
/**
323
- * Calculates the MD5 of the string .
353
+ * Calculates the MD5 of the byte array .
324
354
*
325
- * @param string
355
+ * @param bytes
326
356
* @return md5 of the string
327
357
*/
328
- public static String getMD5 (byte [] bytes ) {
329
- try {
330
- MessageDigest md = MessageDigest .getInstance ("MD5" );
331
- md .reset ();
332
- md .update (bytes );
333
- byte [] digest = md .digest ();
334
- return toHex (digest );
335
- } catch (NoSuchAlgorithmException t ) {
336
- throw new RuntimeException (t );
337
- }
358
+ public static String getMD5 (byte [] bytes )
359
+ {
360
+ return getDigest (bytes , "MD5" );
338
361
}
339
362
340
363
/**
0 commit comments