groovy 3.0.7
代碼實現(xiàn)
實現(xiàn)方式1
import java.security.MessageDigest;
public class MD5Utils {
public final static String MD5(String s) {
char[] hexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; // 十六進制的字符
try {
byte[] byteData = s.getBytes("utf-8"); // 獲取待加密字符的字節(jié)表示
MessageDigest md5 = MessageDigest.getInstance("MD5"); //指定加密方式,獲取加密對象
byte[] digest = md5.digest(byteData); // 加密
StringBuffer sb = new StringBuffer();
// 處理成十六進制的字符串(通常)
for (byte b : digest) {
sb.append(hexChars[(b >> 4) & 15]);
sb.append(hexChars[b & 15]);
}
return new String(sb);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
String md5Str = MD5Utils.MD5("2023, hello, mr授客"); // 獲取32位小寫md5值
println(md5Str); // 52d4eb68f09f4a8eae0b0b02adc748f3
md5Str = md5Str.substring(8, 24); // 獲取16位小寫md5值
println(md5Str); // f09f4a8eae0b0b02
說明:如果在JAVA中運行,則需要修改
char[] hexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; // 十六進制的字符
為文章來源:http://www.zghlxwxcb.cn/news/detail-463733.html
char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; // 十六進制的字符
實現(xiàn)方式2
import java.security.MessageDigest;
public class MD5Utils {
public final static String MD5(String s) {
char[] hexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
try {
byte[] byteData = s.getBytes("utf-8");
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(byteData);
return new BigInteger(1, digest).toString(16);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
String md5Str = MD5Utils.MD5("2023, hello, mr授客"); // 獲取32位小寫md5值
println(md5Str); // 52d4eb68f09f4a8eae0b0b02adc748f3
md5Str = md5Str.substring(8, 24); // 獲取16位小寫md5值
println(md5Str); // f09f4a8eae0b0b02
實現(xiàn)方式3
import java.security.MessageDigest;
public class MD5Utils {
public final static String MD5(String s) {
char[] hexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; // 十六進制的字符
try {
byte[] byteData = s.getBytes("utf-8");
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(byteData);
return new BigInteger(1, digest).toString(16);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
String md5Str = MD5Utils.MD5("2023, hello, mr授客"); // 獲取32位小寫md5值
println(md5Str); // 52d4eb68f09f4a8eae0b0b02adc748f3
md5Str = md5Str.substring(8, 24); // 獲取16位小寫md5值
println(md5Str); // f09f4a8eae0b0b02
===提示文字過少,占位行
===提示文字過少,占位行文章來源地址http://www.zghlxwxcb.cn/news/detail-463733.html
到了這里,關(guān)于Groovy 基于Groovy實現(xiàn)MD5加密的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!