From e9b5cc98d06565854763b569fff8e0228c3fed61 Mon Sep 17 00:00:00 2001 From: XCSDN Date: Sat, 16 Apr 2022 05:30:24 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8spring=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E7=9A=84md5=E8=BF=9B=E8=A1=8C=E4=BC=98=E5=8C=96=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E7=9B=B4=E6=8E=A5=E6=95=88=E9=AA=8C=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=20=E5=AD=97=E8=8A=82=20=E6=96=87=E4=BB=B6=E6=B5=81=20?= =?UTF-8?q?=E6=96=B9=E4=BE=BF=E5=8F=88=E5=8D=AB=E7=94=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/utils/sign/Md5Utils.java | 81 +++++++------------ 1 file changed, 28 insertions(+), 53 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/sign/Md5Utils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/sign/Md5Utils.java index 8fb0fc5e4..2320dab04 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/sign/Md5Utils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/sign/Md5Utils.java @@ -1,67 +1,42 @@ package com.ruoyi.common.utils.sign; -import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.springframework.util.DigestUtils; +import java.io.IOException; +import java.io.InputStream; /** * Md5加密方法 - * + * * @author ruoyi */ -public class Md5Utils -{ - private static final Logger log = LoggerFactory.getLogger(Md5Utils.class); +public class Md5Utils { - private static byte[] md5(String s) - { - MessageDigest algorithm; - try - { - algorithm = MessageDigest.getInstance("MD5"); - algorithm.reset(); - algorithm.update(s.getBytes("UTF-8")); - byte[] messageDigest = algorithm.digest(); - return messageDigest; - } - catch (Exception e) - { - log.error("MD5 Error...", e); - } - return null; + /** + * 效验MD5 + * @param s 任意字符串 + * @return md5的值 + */ + public static String hash(String s) { + return hash(s.getBytes()); } - private static final String toHex(byte hash[]) - { - if (hash == null) - { - return null; - } - StringBuffer buf = new StringBuffer(hash.length * 2); - 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(); + /** + * 效验MD5 + * @param s 字节 + * @return md5的值 + */ + public static String hash(byte s[]) { + return DigestUtils.md5DigestAsHex(s); } - public static String hash(String s) - { - try - { - return new String(toHex(md5(s)).getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8); - } - catch (Exception e) - { - log.error("not supported charset...{}", e); - return s; - } + /** + * 效验MD5 + * @param s 文件流 + * @return md5的值 + */ + public static String hash(InputStream s) throws IOException { + return DigestUtils.md5DigestAsHex(s); } + + }