FEAT StringUtils.digestString

master
Niclas Thobaben 2020-12-04 15:40:50 +01:00
parent 7cc05720fc
commit 52302b4d44
1 changed files with 27 additions and 0 deletions

View File

@ -1,6 +1,9 @@
package de.nclazz.commons;
import lombok.NonNull;
import lombok.SneakyThrows;
import java.security.MessageDigest;
public abstract class StringUtils {
@ -94,4 +97,28 @@ public abstract class StringUtils {
return sb.toString();
}
/**
* Hashes given String with given hash algorithm
*
* @param input to hash
* @param md hash algorithm
* @return hash (hex-)String
*/
@SneakyThrows
public static String digestString(@NonNull String input, MessageDigest md) {
md.update(input.getBytes());
String hash = toHex(md.digest());
return hash;
}
@SneakyThrows
public static String toSHA1(String input) {
return digestString(input, MessageDigest.getInstance("SHA-1"));
}
@SneakyThrows
public static String toMD(String input) {
return digestString(input, MessageDigest.getInstance("MD5"));
}
}