return input;\r
}\r
\r
+\r
/**\r
- * Calculates the SHA1 of the string.\r
+ * Calculates the hash sum of the byte array.\r
+ *\r
+ * @param bytes\r
+ * byte array to hash\r
+ * @param algorithm\r
+ * Message digest algorithm name, e.g MD5, SHA-1 or SHA-256.\r
+ * @return sha sum of the byte array\r
+ */\r
+ private static String getDigest(byte[] bytes, String algorithm)\r
+ {\r
+ try {\r
+ MessageDigest md = MessageDigest.getInstance(algorithm);\r
+ md.update(bytes, 0, bytes.length);\r
+ byte[] digest = md.digest();\r
+ return toHex(digest);\r
+ } catch (NoSuchAlgorithmException t) {\r
+ throw new RuntimeException(t);\r
+ }\r
+ }\r
+\r
+\r
+ /**\r
+ * Calculates the hash of the string.\r
*\r
* @param text\r
+ * string to hash\r
+ * @param algorithm\r
+ * Message digest algorithm name, e.g MD5, SHA-1 or SHA-256.\r
* @return sha1 of the string\r
*/\r
- public static String getSHA1(String text) {\r
+ private static String getDigest(String text, String algorithm)\r
+ {\r
try {\r
byte[] bytes = text.getBytes("iso-8859-1");\r
- return getSHA1(bytes);\r
+ return getDigest(bytes, algorithm);\r
} catch (UnsupportedEncodingException u) {\r
throw new RuntimeException(u);\r
}\r
}\r
\r
+ /**\r
+ * Calculates the SHA1 of the string.\r
+ *\r
+ * @param text\r
+ * @return sha1 of the string\r
+ */\r
+ public static String getSHA1(String text)\r
+ {\r
+ return getDigest(text, "SHA-1");\r
+ }\r
+\r
/**\r
* Calculates the SHA1 of the byte array.\r
*\r
* @param bytes\r
* @return sha1 of the byte array\r
*/\r
- public static String getSHA1(byte[] bytes) {\r
- try {\r
- MessageDigest md = MessageDigest.getInstance("SHA-1");\r
- md.update(bytes, 0, bytes.length);\r
- byte[] digest = md.digest();\r
- return toHex(digest);\r
- } catch (NoSuchAlgorithmException t) {\r
- throw new RuntimeException(t);\r
- }\r
+ public static String getSHA1(byte[] bytes)\r
+ {\r
+ return getDigest(bytes, "SHA-1");\r
+ }\r
}\r
\r
/**\r
* Calculates the MD5 of the string.\r
*\r
- * @param string\r
+ * @param text\r
* @return md5 of the string\r
*/\r
- public static String getMD5(String string) {\r
- try {\r
- return getMD5(string.getBytes("iso-8859-1"));\r
- } catch (UnsupportedEncodingException u) {\r
- throw new RuntimeException(u);\r
- }\r
+ public static String getMD5(String text)\r
+ {\r
+ return getDigest(text, "MD5");\r
}\r
\r
/**\r
- * Calculates the MD5 of the string.\r
+ * Calculates the MD5 of the byte array.\r
*\r
- * @param string\r
+ * @param bytes\r
* @return md5 of the string\r
*/\r
- public static String getMD5(byte [] bytes) {\r
- try {\r
- MessageDigest md = MessageDigest.getInstance("MD5");\r
- md.reset();\r
- md.update(bytes);\r
- byte[] digest = md.digest();\r
- return toHex(digest);\r
- } catch (NoSuchAlgorithmException t) {\r
- throw new RuntimeException(t);\r
- }\r
+ public static String getMD5(byte [] bytes)\r
+ {\r
+ return getDigest(bytes, "MD5");\r
}\r
\r
/**\r