]> source.dussan.org Git - gitblit.git/commitdiff
Add function to calculate SHA-256 has sum to StingUtils. 1456/head
authorFlorian Zschocke <f.zschocke+git@gmail.com>
Sun, 15 Oct 2023 14:26:35 +0000 (16:26 +0200)
committerFlorian Zschocke <f.zschocke+git@gmail.com>
Sun, 15 Oct 2023 14:26:35 +0000 (16:26 +0200)
src/main/java/com/gitblit/utils/StringUtils.java
src/test/java/com/gitblit/tests/StringUtilsTest.java

index 1a4952f348b7e63568489ed4e040f1db88a3f594..cc1936fc3510683ec099e025e40c674e49b01fef 100644 (file)
@@ -336,6 +336,28 @@ public class StringUtils {
        {\r
                return getDigest(bytes, "SHA-1");\r
        }\r
+\r
+\r
+       /**\r
+        * Calculates the SHA256 of the string.\r
+        *\r
+        * @param text\r
+        * @return sha256 of the string\r
+        */\r
+       public static String getSHA256(String text)\r
+       {\r
+               return getDigest(text, "SHA-256");\r
+       }\r
+\r
+       /**\r
+        * Calculates the SHA256 of the byte array.\r
+        *\r
+        * @param bytes\r
+        * @return sha256 of the byte array\r
+        */\r
+       public static String getSHA256(byte[] bytes)\r
+       {\r
+               return getDigest(bytes, "SHA-256");\r
        }\r
 \r
        /**\r
index cc579888b6701d20a3fbba54eb208fa5bfe69ef6..723a893037564acb2afdbac02b54f03c89f35b3f 100644 (file)
@@ -133,6 +133,12 @@ public class StringUtilsTest extends GitblitUnitTest {
                                StringUtils.getSHA1("blob 16\000what is up, doc?"));\r
        }\r
 \r
+       @Test\r
+       public void testSHA256() throws Exception {\r
+               assertEquals("badf72532e259f2b67a40475486c7e71bf48bc71d7b0d43d8e99acfb3ac24e1b",\r
+                                        StringUtils.getSHA256("margaret@london.uk"));\r
+       }\r
+\r
        @Test\r
        public void testMD5() throws Exception {\r
                assertEquals("77fb8d95331f0d557472f6776d3aedf6",\r