summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Zschocke <2362065+flaix@users.noreply.github.com>2023-10-15 21:26:47 +0200
committerGitHub <noreply@github.com>2023-10-15 21:26:47 +0200
commit24ab9d65e54148d7b96e9401dbc15ec4e0087675 (patch)
tree7e0ecf66df4e33bfa22cbae4e23d38d2ee1fc8da
parentc1b2aec79fe6cba69b8af839ce8c33c58bce2bfd (diff)
parent9499952e791fcdf53c39e95216c19ddb8c84c4cc (diff)
downloadgitblit-24ab9d65e54148d7b96e9401dbc15ec4e0087675.tar.gz
gitblit-24ab9d65e54148d7b96e9401dbc15ec4e0087675.zip
Merge pull request #1456 from flaix/sha256
Add SHA-256 hash calculation to StringUtils
-rw-r--r--src/main/java/com/gitblit/utils/StringUtils.java107
-rw-r--r--src/test/java/com/gitblit/tests/StringUtilsTest.java6
2 files changed, 82 insertions, 31 deletions
diff --git a/src/main/java/com/gitblit/utils/StringUtils.java b/src/main/java/com/gitblit/utils/StringUtils.java
index 0e23d637..cc1936fc 100644
--- a/src/main/java/com/gitblit/utils/StringUtils.java
+++ b/src/main/java/com/gitblit/utils/StringUtils.java
@@ -273,68 +273,113 @@ 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 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");
}
/**
* 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");
}
/**
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
@@ -134,6 +134,12 @@ public class StringUtilsTest extends GitblitUnitTest {
}
@Test
+ public void testSHA256() throws Exception {
+ assertEquals("badf72532e259f2b67a40475486c7e71bf48bc71d7b0d43d8e99acfb3ac24e1b",
+ StringUtils.getSHA256("margaret@london.uk"));
+ }
+
+ @Test
public void testMD5() throws Exception {
assertEquals("77fb8d95331f0d557472f6776d3aedf6",
StringUtils.getMD5("blob 16\000what is up, doc?"));