瀏覽代碼

Merge pull request #1456 from flaix/sha256

Add SHA-256 hash calculation to StringUtils
pull/1457/head
Florian Zschocke 7 月之前
父節點
當前提交
24ab9d65e5
沒有連結到貢獻者的電子郵件帳戶。
共有 2 個檔案被更改,包括 82 行新增31 行删除
  1. 76
    31
      src/main/java/com/gitblit/utils/StringUtils.java
  2. 6
    0
      src/test/java/com/gitblit/tests/StringUtilsTest.java

+ 76
- 31
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");
}
/**

+ 6
- 0
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",

Loading…
取消
儲存