Skip to content

Commit 9499952

Browse files
committed
Add function to calculate SHA-256 has sum to StingUtils.
1 parent fd2eb89 commit 9499952

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,28 @@ public static String getSHA1(byte[] bytes)
336336
{
337337
return getDigest(bytes, "SHA-1");
338338
}
339+
340+
341+
/**
342+
* Calculates the SHA256 of the string.
343+
*
344+
* @param text
345+
* @return sha256 of the string
346+
*/
347+
public static String getSHA256(String text)
348+
{
349+
return getDigest(text, "SHA-256");
350+
}
351+
352+
/**
353+
* Calculates the SHA256 of the byte array.
354+
*
355+
* @param bytes
356+
* @return sha256 of the byte array
357+
*/
358+
public static String getSHA256(byte[] bytes)
359+
{
360+
return getDigest(bytes, "SHA-256");
339361
}
340362

341363
/**

src/test/java/com/gitblit/tests/StringUtilsTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ public void testSHA1() throws Exception {
133133
StringUtils.getSHA1("blob 16\000what is up, doc?"));
134134
}
135135

136+
@Test
137+
public void testSHA256() throws Exception {
138+
assertEquals("badf72532e259f2b67a40475486c7e71bf48bc71d7b0d43d8e99acfb3ac24e1b",
139+
StringUtils.getSHA256("[email protected]"));
140+
}
141+
136142
@Test
137143
public void testMD5() throws Exception {
138144
assertEquals("77fb8d95331f0d557472f6776d3aedf6",

0 commit comments

Comments
 (0)