@@ -273,68 +273,113 @@ public static String rightPad(String input, int length, char pad) {
273273 return input ;
274274 }
275275
276+
276277 /**
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.
278301 *
279302 * @param text
303+ * string to hash
304+ * @param algorithm
305+ * Message digest algorithm name, e.g MD5, SHA-1 or SHA-256.
280306 * @return sha1 of the string
281307 */
282- public static String getSHA1 (String text ) {
308+ private static String getDigest (String text , String algorithm )
309+ {
283310 try {
284311 byte [] bytes = text .getBytes ("iso-8859-1" );
285- return getSHA1 (bytes );
312+ return getDigest (bytes , algorithm );
286313 } catch (UnsupportedEncodingException u ) {
287314 throw new RuntimeException (u );
288315 }
289316 }
290317
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+
291329 /**
292330 * Calculates the SHA1 of the byte array.
293331 *
294332 * @param bytes
295333 * @return sha1 of the byte array
296334 */
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" );
306361 }
307362
308363 /**
309364 * Calculates the MD5 of the string.
310365 *
311- * @param string
366+ * @param text
312367 * @return md5 of the string
313368 */
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" );
320372 }
321373
322374 /**
323- * Calculates the MD5 of the string .
375+ * Calculates the MD5 of the byte array .
324376 *
325- * @param string
377+ * @param bytes
326378 * @return md5 of the string
327379 */
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" );
338383 }
339384
340385 /**
0 commit comments