Browse Source

Refactor StringUtils to provide message digest in common function

The calculation of a MD5 and SHA-1 sum are all message digest implementations.
Instead or replicating the same code over and over again, provide a
common function for message digest calculation which can do this for
different algorithms based on the algorithm name passed as a parameter.

Then replace the existing `getMD5` and `getSHA1` functions by calling
the common function passing the respective algorithm name.
pull/1456/head
Florian Zschocke 6 months ago
parent
commit
fd2eb897e1
1 changed files with 54 additions and 31 deletions
  1. 54
    31
      src/main/java/com/gitblit/utils/StringUtils.java

+ 54
- 31
src/main/java/com/gitblit/utils/StringUtils.java View File

@@ -273,68 +273,91 @@ 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 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");
}
/**

Loading…
Cancel
Save