From: Florian Zschocke Date: Sun, 15 Oct 2023 14:25:58 +0000 (+0200) Subject: Refactor StringUtils to provide message digest in common function X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fd2eb897e1386ded3b55f5a6075b5fc3258a5d31;p=gitblit.git Refactor StringUtils to provide message digest in common function The calculation of a MD5 and SHA-1 sum are all message digest implementations. Instead or replicating the same code over and over again, provide a common function for message digest calculation which can do this for different algorithms based on the algorithm name passed as a parameter. Then replace the existing `getMD5` and `getSHA1` functions by calling the common function passing the respective algorithm name. --- diff --git a/src/main/java/com/gitblit/utils/StringUtils.java b/src/main/java/com/gitblit/utils/StringUtils.java index 0e23d637..1a4952f3 100644 --- a/src/main/java/com/gitblit/utils/StringUtils.java +++ b/src/main/java/com/gitblit/utils/StringUtils.java @@ -273,68 +273,91 @@ public class StringUtils { return input; } + /** - * Calculates the SHA1 of the string. + * Calculates the hash sum of the byte array. + * + * @param bytes + * byte array to hash + * @param algorithm + * Message digest algorithm name, e.g MD5, SHA-1 or SHA-256. + * @return sha sum of the byte array + */ + private static String getDigest(byte[] bytes, String algorithm) + { + try { + MessageDigest md = MessageDigest.getInstance(algorithm); + md.update(bytes, 0, bytes.length); + byte[] digest = md.digest(); + return toHex(digest); + } catch (NoSuchAlgorithmException t) { + throw new RuntimeException(t); + } + } + + + /** + * Calculates the hash of the string. * * @param text + * string to hash + * @param algorithm + * Message digest algorithm name, e.g MD5, SHA-1 or SHA-256. * @return sha1 of the string */ - public static String getSHA1(String text) { + private static String getDigest(String text, String algorithm) + { try { byte[] bytes = text.getBytes("iso-8859-1"); - return getSHA1(bytes); + return getDigest(bytes, algorithm); } catch (UnsupportedEncodingException u) { throw new RuntimeException(u); } } + /** + * Calculates the SHA1 of the string. + * + * @param text + * @return sha1 of the string + */ + public static String getSHA1(String text) + { + return getDigest(text, "SHA-1"); + } + /** * Calculates the SHA1 of the byte array. * * @param bytes * @return sha1 of the byte array */ - public static String getSHA1(byte[] bytes) { - try { - MessageDigest md = MessageDigest.getInstance("SHA-1"); - md.update(bytes, 0, bytes.length); - byte[] digest = md.digest(); - return toHex(digest); - } catch (NoSuchAlgorithmException t) { - throw new RuntimeException(t); - } + public static String getSHA1(byte[] bytes) + { + return getDigest(bytes, "SHA-1"); + } } /** * Calculates the MD5 of the string. * - * @param string + * @param text * @return md5 of the string */ - public static String getMD5(String string) { - try { - return getMD5(string.getBytes("iso-8859-1")); - } catch (UnsupportedEncodingException u) { - throw new RuntimeException(u); - } + public static String getMD5(String text) + { + return getDigest(text, "MD5"); } /** - * Calculates the MD5 of the string. + * Calculates the MD5 of the byte array. * - * @param string + * @param bytes * @return md5 of the string */ - public static String getMD5(byte [] bytes) { - try { - MessageDigest md = MessageDigest.getInstance("MD5"); - md.reset(); - md.update(bytes); - byte[] digest = md.digest(); - return toHex(digest); - } catch (NoSuchAlgorithmException t) { - throw new RuntimeException(t); - } + public static String getMD5(byte [] bytes) + { + return getDigest(bytes, "MD5"); } /**