lijunping 2 лет назад
Родитель
Сommit
ee5890cd6c

+ 73 - 0
blade-service/trade-purchase/src/main/java/com/trade/purchase/config/MD5Util.java

@@ -0,0 +1,73 @@
+package com.trade.purchase.config;
+
+import org.apache.commons.codec.binary.Base64;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+
+/**
+ * @desc: md5工具类
+ * @author: zongren_zhang
+ * @time: 2021/3/20
+ */
+public class MD5Util {
+    public static final String HMAC_SHA_256 = "HmacSHA256";
+
+    /**
+     * 获取HashMAC
+     * @param data
+     * @param secret
+     * @return
+     */
+    public static String hashMAC(String data,String secret) {
+        try {
+            Mac kdmac = Mac.getInstance(HMAC_SHA_256);
+            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), HMAC_SHA_256);
+            kdmac.init(secret_key);
+            byte[] rawHmac = kdmac.doFinal(data.getBytes());
+            return Base64.encodeBase64String(byteToHexString(rawHmac).getBytes());
+        }catch(Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+
+
+    /**
+     * 字节数组转16进制
+     * @param hashInBytes
+     * @return
+     */
+    public static String bytesToHex(byte[] hashInBytes) {
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < hashInBytes.length; i++) {
+            String hex = Integer.toHexString(hashInBytes[i] & 0xFF);
+            if(hex.length() < 2){
+                hex = "0" + hex;
+            }
+            sb.append(hex);
+        }
+        return sb.toString();
+    }
+
+    public static String byteToHexString(byte[] b) {
+        StringBuilder hs = new StringBuilder();
+
+        for(int n = 0; b != null && n < b.length; ++n) {
+            String stmp = Integer.toHexString(b[n] & 255);
+            if (stmp.length() == 1) {
+                hs.append('0');
+            }
+
+            hs.append(stmp);
+        }
+
+        return hs.toString();
+
+
+    }
+
+
+
+}