From 73a49ddd0516e5c9a4b697c593d62c74e420403b Mon Sep 17 00:00:00 2001 From: mac <user@users-MacBook-Pro.local> Date: 星期四, 24 十月 2024 12:27:31 +0800 Subject: [PATCH] 2024年10月24日12:27:28 --- HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/utils/HDLMD5Utils.java | 185 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 185 insertions(+), 0 deletions(-) diff --git a/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/utils/HDLMD5Utils.java b/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/utils/HDLMD5Utils.java new file mode 100644 index 0000000..6559b86 --- /dev/null +++ b/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/utils/HDLMD5Utils.java @@ -0,0 +1,185 @@ +package com.hdl.linkpm.sdk.utils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.UnsupportedEncodingException; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * Created by Tong on 2021/11/3. + */ +public final class HDLMD5Utils { + + + /** + * MD5杞瓧绗︿覆 + */ + public static String encodeMD5(byte[] f) { + try { + MessageDigest digest = MessageDigest.getInstance("MD5"); + digest.update(f); + byte[] messageDigest = digest.digest(); + return toHexString(messageDigest); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return ""; + } + /** + * MD5杞瓧绗︿覆 + */ + public static String encodeMD5(String s) { + try { + MessageDigest digest = MessageDigest.getInstance("MD5"); + digest.update(s.getBytes(StandardCharsets.UTF_8)); + byte[] messageDigest = digest.digest(); + return toHexString(messageDigest); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return ""; + } + + private static String toHexString(byte[] keyData) { + if (keyData == null) { + return null; + } + int expectedStringLen = keyData.length * 2; + StringBuilder sb = new StringBuilder(expectedStringLen); + for (byte keyDatum : keyData) { + String hexStr = Integer.toString(keyDatum & 0x00FF, 16); + if (hexStr.length() == 1) { + hexStr = "0" + hexStr; + } + sb.append(hexStr); + } + return sb.toString(); + } + + /** + * MD5杞瓧绗︿覆 + * 棰濆鍊� + */ + public static String encodeMD52(String s) { + char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + try { + byte[] btInput = s.getBytes(StandardCharsets.UTF_8); + MessageDigest digest = MessageDigest.getInstance("MD5"); + digest.update(btInput); + byte[] md = digest.digest(); + int j = md.length; + char[] str = new char[j * 2]; + int k = 0; + for (byte byte0 : md) { + str[k++] = hexDigits[byte0 >>> 4 & 0xf]; + str[k++] = hexDigits[byte0 & 0xf]; + } + return new String(str); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * MD5鏂囦欢杞瓧绗︿覆 + */ + public static String encodeMD5(File f) { + if (!f.isFile()) { + return null; + } + MessageDigest digest; + byte[] buffer = new byte[1024]; + int len; + try { + digest = MessageDigest.getInstance("MD5"); + FileInputStream in = new FileInputStream(f); + while ((len = in.read(buffer, 0, 1024)) != -1) { + digest.update(buffer, 0, len); + } + in.close(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + BigInteger bigInt = new BigInteger(1, digest.digest()); + return bigInt.toString(16); + } + + + + + + public final static String md5(String plainText) { + +// 杩斿洖瀛楃涓� + + String md5Str = null; + + try { + +// 鎿嶄綔瀛楃涓� + + StringBuffer buf = new StringBuffer(); + + MessageDigest md = + + MessageDigest.getInstance("MD5"); + +// 娣诲姞瑕佽繘琛岃绠楁憳瑕佺殑淇℃伅,浣跨敤 plainText 鐨� byte + + + md.update(plainText.getBytes()); + +// 璁$畻鍑烘憳瑕�,瀹屾垚鍝堝笇璁$畻銆� + + byte b[] = md.digest(); + + int i; + + for (int offset = 0; offset < b.length; offset++) { + + i = b[offset]; + + if (i < 0) { + + i += 256; + + } + + if (i < 16) { + + buf.append("0"); + + } + +// 灏嗘暣鍨� 鍗佽繘鍒� i + + + buf.append(Integer.toHexString(i)); + + } + +// 32浣嶇殑鍔犲瘑 + + md5Str = buf.toString(); + +// 16浣嶇殑鍔犲瘑 + +// md5Str = buf.toString().md5Strstring(8,24); + + } catch (Exception e) { + + e.printStackTrace(); + + } + + return md5Str; + + } + + +} -- Gitblit v1.8.0