groovy 3.0.7
代碼實作
實作方式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 = https://www.cnblogs.com/shouke/archive/2023/05/29/s.getBytes("utf-8"); // 獲取待加密字符的位元組表示
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']; // 十六進制的字符
為
char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; // 十六進制的字符
實作方式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 = https://www.cnblogs.com/shouke/archive/2023/05/29/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
實作方式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 = https://www.cnblogs.com/shouke/archive/2023/05/29/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
===提示文字過少,占位行
===提示文字過少,占位行
作者:授客
微信/QQ:1033553122
全國軟體測驗QQ交流群:7156436
Git地址:https://gitee.com/ishouke
友情提示:限于時間倉促,文中可能存在錯誤,歡迎指正、評論!
作者五行缺錢,如果覺得文章對您有幫助,請掃描下邊的二維碼打賞作者,金額隨意,您的支持將是我繼續創作的源動力,打賞后如有任何疑問,請聯系我!!!
微信打賞
支付寶打賞 全國軟體測驗交流QQ群
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/553748.html
標籤:其他
上一篇:【QCustomPlot】性能提升之修改原始碼(版本 V2.x.x)
下一篇:返回列表