Pre Merge pull request !470 from XCSDN/N/A

This commit is contained in:
XCSDN 2022-05-01 12:24:25 +00:00 committed by Gitee
commit b7f2a567fe
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -1,67 +1,42 @@
package com.ruoyi.common.utils.sign; package com.ruoyi.common.utils.sign;
import java.nio.charset.StandardCharsets; import org.springframework.util.DigestUtils;
import java.security.MessageDigest; import java.io.IOException;
import org.slf4j.Logger; import java.io.InputStream;
import org.slf4j.LoggerFactory;
/** /**
* Md5加密方法 * Md5加密方法
* *
* @author ruoyi * @author ruoyi
*/ */
public class Md5Utils public class Md5Utils {
{
private static final Logger log = LoggerFactory.getLogger(Md5Utils.class);
private static byte[] md5(String s) /**
{ * 效验MD5
MessageDigest algorithm; * @param s 任意字符串
try * @return md5的值
{ */
algorithm = MessageDigest.getInstance("MD5"); public static String hash(String s) {
algorithm.reset(); return hash(s.getBytes());
algorithm.update(s.getBytes("UTF-8"));
byte[] messageDigest = algorithm.digest();
return messageDigest;
}
catch (Exception e)
{
log.error("MD5 Error...", e);
}
return null;
} }
private static final String toHex(byte hash[]) /**
{ * 效验MD5
if (hash == null) * @param s 字节
{ * @return md5的值
return null; */
} public static String hash(byte s[]) {
StringBuffer buf = new StringBuffer(hash.length * 2); return DigestUtils.md5DigestAsHex(s);
int i;
for (i = 0; i < hash.length; i++)
{
if ((hash[i] & 0xff) < 0x10)
{
buf.append("0");
}
buf.append(Long.toString(hash[i] & 0xff, 16));
}
return buf.toString();
} }
public static String hash(String s) /**
{ * 效验MD5
try * @param s 文件流
{ * @return md5的值
return new String(toHex(md5(s)).getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8); */
} public static String hash(InputStream s) throws IOException {
catch (Exception e) return DigestUtils.md5DigestAsHex(s);
{
log.error("not supported charset...{}", e);
return s;
}
} }
} }