]> source.dussan.org Git - gitblit.git/commitdiff
Refactor StringUtils to provide message digest in common function
authorFlorian Zschocke <f.zschocke+git@gmail.com>
Sun, 15 Oct 2023 14:25:58 +0000 (16:25 +0200)
committerFlorian Zschocke <f.zschocke+git@gmail.com>
Sun, 15 Oct 2023 14:25:58 +0000 (16:25 +0200)
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.

src/main/java/com/gitblit/utils/StringUtils.java

index 0e23d637831ca1ff4a5d3aec0e3568a94b8d949c..1a4952f348b7e63568489ed4e040f1db88a3f594 100644 (file)
@@ -273,68 +273,91 @@ public class StringUtils {
                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