org.unlaxer.util.digest.MD5 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tinyExpression Show documentation
Show all versions of tinyExpression Show documentation
TinyExpression implemented with Unlaxer
package org.unlaxer.util.digest;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
// static ThreadLocal messageDigest = new ThreadLocal<>() {
// @Override
// public MessageDigest initialValue() {
// try {
// return MessageDigest.getInstance("MD5");
// } catch (NoSuchAlgorithmException e) {
// throw new RuntimeException(e);
// }
// }
// };
public static byte[] toBytes(byte[] bytes) {
MessageDigest instance;
try {
instance = MessageDigest.getInstance("MD5");
instance.update(bytes);
return instance.digest();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static byte[] toBytes(String utf8String) {
return toBytes(utf8String.getBytes(StandardCharsets.UTF_8));
}
public static String toHex(byte[] bytes) {
byte[] bytes2 = toBytes(bytes);
return HEX.encode(bytes2);
}
public static String toHex(String utf8String) {
return toHex(utf8String.getBytes(StandardCharsets.UTF_8));
}
}