From b2833e553e8f4cf7bc7f7b1b1276d1c5b73b76ac Mon Sep 17 00:00:00 2001 From: wayn <1669738430@qq.com> Date: Thu, 26 Nov 2020 23:11:26 +0800 Subject: [PATCH] =?UTF-8?q?perf(=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0md5=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wayn/common/util/security/Md5Utils.java | 41 ++++++++----------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/waynboot-common/src/main/java/com/wayn/common/util/security/Md5Utils.java b/waynboot-common/src/main/java/com/wayn/common/util/security/Md5Utils.java index 3757f9f..82a8e73 100644 --- a/waynboot-common/src/main/java/com/wayn/common/util/security/Md5Utils.java +++ b/waynboot-common/src/main/java/com/wayn/common/util/security/Md5Utils.java @@ -7,44 +7,35 @@ import java.security.MessageDigest; /** * Md5加密方法 - * + * * @author ruoyi */ -public class Md5Utils -{ +public class Md5Utils { private static final Logger log = LoggerFactory.getLogger(Md5Utils.class); - private static byte[] md5(String s) - { + private static byte[] md5(String s) { MessageDigest algorithm; - try - { + try { algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); algorithm.update(s.getBytes("UTF-8")); byte[] messageDigest = algorithm.digest(); return messageDigest; - } - catch (Exception e) - { + } catch (Exception e) { log.error("MD5 Error...", e); } return null; } - private static final String toHex(byte hash[]) - { - if (hash == null) - { + 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) - { + for (i = 0; i < hash.length; i++) { + if ((hash[i] & 0xff) < 0x10) { buf.append("0"); } buf.append(Long.toString(hash[i] & 0xff, 16)); @@ -52,16 +43,16 @@ public class Md5Utils return buf.toString(); } - public static String hash(String s) - { - try - { + public static String hash(String s) { + try { return new String(toHex(md5(s)).getBytes("UTF-8"), "UTF-8"); - } - catch (Exception e) - { + } catch (Exception e) { log.error("not supported charset...{}", e); return s; } } + + public static void main(String[] args) { + System.out.println(hash("123456")); + } }