@@ -273,68 +273,113 @@ 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
+ }
339
+
340
+
341
+ /**
342
+ * Calculates the SHA256 of the string.
343
+ *
344
+ * @param text
345
+ * @return sha256 of the string
346
+ */
347
+ public static String getSHA256 (String text )
348
+ {
349
+ return getDigest (text , "SHA-256" );
350
+ }
351
+
352
+ /**
353
+ * Calculates the SHA256 of the byte array.
354
+ *
355
+ * @param bytes
356
+ * @return sha256 of the byte array
357
+ */
358
+ public static String getSHA256 (byte [] bytes )
359
+ {
360
+ return getDigest (bytes , "SHA-256" );
306
361
}
307
362
308
363
/**
309
364
* Calculates the MD5 of the string.
310
365
*
311
- * @param string
366
+ * @param text
312
367
* @return md5 of the string
313
368
*/
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
- }
369
+ public static String getMD5 (String text )
370
+ {
371
+ return getDigest (text , "MD5" );
320
372
}
321
373
322
374
/**
323
- * Calculates the MD5 of the string .
375
+ * Calculates the MD5 of the byte array .
324
376
*
325
- * @param string
377
+ * @param bytes
326
378
* @return md5 of the string
327
379
*/
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
- }
380
+ public static String getMD5 (byte [] bytes )
381
+ {
382
+ return getDigest (bytes , "MD5" );
338
383
}
339
384
340
385
/**
0 commit comments