Java MD5算法

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public static String getDigest(String msg) throws NoSuchAlgorithmException, UnsupportedEncodingException
{
  
final MessageDigest md5 = MessageDigest.getInstance("MD5");
final byte[] byteArray = msg.getBytes("UTF-8");
final byte[] md5Bytes = md5.digest(byteArray);

//将生成的md5Bytes转成16进制形式
final StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++)
{
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}

posted @ 2022-07-15 16:34  DarryRing  阅读(591)  评论(0编辑  收藏  举报