From 9499952e791fcdf53c39e95216c19ddb8c84c4cc Mon Sep 17 00:00:00 2001 From: Florian Zschocke Date: Sun, 15 Oct 2023 16:26:35 +0200 Subject: [PATCH] Add function to calculate SHA-256 has sum to StingUtils. --- .../java/com/gitblit/utils/StringUtils.java | 22 +++++++++++++++++++ .../com/gitblit/tests/StringUtilsTest.java | 6 +++++ 2 files changed, 28 insertions(+) diff --git a/src/main/java/com/gitblit/utils/StringUtils.java b/src/main/java/com/gitblit/utils/StringUtils.java index 1a4952f3..cc1936fc 100644 --- a/src/main/java/com/gitblit/utils/StringUtils.java +++ b/src/main/java/com/gitblit/utils/StringUtils.java @@ -336,6 +336,28 @@ public class StringUtils { { return getDigest(bytes, "SHA-1"); } + + + /** + * Calculates the SHA256 of the string. + * + * @param text + * @return sha256 of the string + */ + public static String getSHA256(String text) + { + return getDigest(text, "SHA-256"); + } + + /** + * Calculates the SHA256 of the byte array. + * + * @param bytes + * @return sha256 of the byte array + */ + public static String getSHA256(byte[] bytes) + { + return getDigest(bytes, "SHA-256"); } /** diff --git a/src/test/java/com/gitblit/tests/StringUtilsTest.java b/src/test/java/com/gitblit/tests/StringUtilsTest.java index cc579888..723a8930 100644 --- a/src/test/java/com/gitblit/tests/StringUtilsTest.java +++ b/src/test/java/com/gitblit/tests/StringUtilsTest.java @@ -133,6 +133,12 @@ public class StringUtilsTest extends GitblitUnitTest { StringUtils.getSHA1("blob 16\000what is up, doc?")); } + @Test + public void testSHA256() throws Exception { + assertEquals("badf72532e259f2b67a40475486c7e71bf48bc71d7b0d43d8e99acfb3ac24e1b", + StringUtils.getSHA256("margaret@london.uk")); + } + @Test public void testMD5() throws Exception { assertEquals("77fb8d95331f0d557472f6776d3aedf6", -- 2.39.5