Kaynağa Gözat

MD5和Appkey和AppScrect上次工具类

gaodm 6 yıl önce
ebeveyn
işleme
62179a8541

+ 2 - 1
common/src/main/java/com/diagbot/enums/VisibleIdTypeEnum.java

@@ -9,7 +9,8 @@ import lombok.Setter;
  * @time: 2018/9/5 10:30
  */
 public enum VisibleIdTypeEnum implements KeyedNamed {
-    IS_IMG_VER(1, "图片验证码");
+    IS_IMG_VER(1, "图片验证码"),
+    IS_ORDER(2, "订单编号");
 
     @Setter
     private int key;

+ 1 - 1
common/src/main/java/com/diagbot/util/IdcardValidator.java

@@ -13,7 +13,7 @@ import java.util.Date;
  * @author: gaodm
  * @time: 2018/9/6 13:35
  */
-public class IdcardValidator {
+public class IdcardValidatorUtil {
 
     /**
      * <pre>

+ 48 - 0
diagbotman-service/src/main/java/com/diagbot/util/GuidUtil.java

@@ -0,0 +1,48 @@
+package com.diagbot.util;
+
+import java.util.UUID;
+
+/**
+ * @Description: Appkey和Appscrect生成工具
+ * @author: gaodm
+ * @time: 2018/9/11 10:27
+ */
+public class GuidUtil {
+    public String app_key;
+    /**
+     * @description:随机获取key值
+     * @return
+     */
+    public String guid() {
+        UUID uuid = UUID.randomUUID();
+        String key = uuid.toString();
+        return key;
+    }
+    /**
+     * 这是其中一个url的参数,是GUID的,全球唯一标志符
+     * @return
+     */
+    public String App_key() {
+        GuidUtil g = new GuidUtil();
+        String guid = g.guid();
+        app_key = MD5Util.MD5(guid).toUpperCase();
+        return app_key;
+    }
+    /**
+     * 根据md5加密
+     * @return
+     */
+    public String App_screct() {
+        String mw = "key" + app_key ;
+        String app_sign = MD5Util.MD5Salt(mw).toUpperCase();// 得到以后还要用MD5加密。
+        return app_sign;
+    }
+
+    public static void main(String[] args) {
+        GuidUtil gd = new GuidUtil();
+        String app_key=gd.App_key();
+        System.out.println("app_key: "+app_key);
+        String app_screct=gd.App_screct();
+        System.out.println("app_screct: "+app_screct);
+    }
+}

+ 152 - 0
diagbotman-service/src/main/java/com/diagbot/util/MD5Util.java

@@ -0,0 +1,152 @@
+package com.diagbot.util;
+
+import org.apache.commons.codec.binary.Hex;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Random;
+
+/**
+ * @Description: MD5加密工具类
+ * @author: gaodm
+ * @time: 2018/9/11 10:33
+ */
+public class MD5Util {
+    public static final String CODES = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+    /**
+     * 使用系统默认字符源生成验证码
+     * @param verifySize    验证码长度
+     * @return
+     */
+    public static String generateCode(int verifySize){
+        return generateCode(verifySize, CODES);
+    }
+    /**
+     * 使用指定源生成验证码
+     * @param verifySize    验证码长度
+     * @param sources   验证码字符源
+     * @return
+     */
+    public static String generateCode(int verifySize, String sources){
+        if(sources == null || sources.length() == 0){
+            sources = CODES;
+        }
+        int codesLen = sources.length();
+        Random rand = new Random(System.currentTimeMillis());
+        StringBuilder code = new StringBuilder(verifySize);
+        for(int i = 0; i < verifySize; i++){
+            code.append(sources.charAt(rand.nextInt(codesLen-1)));
+        }
+        return code.toString();
+    }
+    /**
+     * 普通MD5
+     * @param input
+     * @return
+     */
+    public static String MD5(String input) {
+        MessageDigest md5 = null;
+        try {
+            md5 = MessageDigest.getInstance("MD5");
+        } catch (NoSuchAlgorithmException e) {
+            return "check jdk";
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "";
+        }
+        char[] charArray = input.toCharArray();
+        byte[] byteArray = new byte[charArray.length];
+
+        for (int i = 0; i < charArray.length; i++)
+            byteArray[i] = (byte) charArray[i];
+        byte[] md5Bytes = md5.digest(byteArray);
+        StringBuffer hexValue = new StringBuffer();
+        for (int i = 0; i < md5Bytes.length; i++) {
+            int val = ((int) md5Bytes[i]) & 0xff;
+            if (val < 16)
+                hexValue.append("0");
+            hexValue.append(Integer.toHexString(val));
+        }
+        return hexValue.toString();
+    }
+
+    /**
+     * 加盐MD5
+     * @param password
+     * @return
+     */
+    public static String MD5Salt(String password) {
+        StringBuilder sb = new StringBuilder(16);
+        sb.append(generateCode(8)).append(generateCode(8));
+        int len = sb.length();
+        if (len < 16) {
+            for (int i = 0; i < 16 - len; i++) {
+                sb.append("0");
+            }
+        }
+        String salt = sb.toString();
+        password = md5Hex(password + salt);
+        char[] cs = new char[48];
+        for (int i = 0; i < 48; i += 3) {
+            cs[i] = password.charAt(i / 3 * 2);
+            char c = salt.charAt(i / 3);
+            cs[i + 1] = c;
+            cs[i + 2] = password.charAt(i / 3 * 2 + 1);
+        }
+        return new String(cs);
+    }
+    /**
+     * 校验加盐后是否和原文一致
+     * @author daniel
+     * @time 2016-6-11 下午8:45:39
+     * @param password
+     * @param md5
+     * @return
+     */
+    public static boolean verify(String password, String md5) {
+        char[] cs1 = new char[32];
+        char[] cs2 = new char[16];
+        for (int i = 0; i < 48; i += 3) {
+            cs1[i / 3 * 2] = md5.charAt(i);
+            cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);
+            cs2[i / 3] = md5.charAt(i + 1);
+        }
+        String salt = new String(cs2);
+        return md5Hex(password + salt).equals(new String(cs1));
+    }
+    /**
+     * 获取十六进制字符串形式的MD5摘要
+     */
+    private static String md5Hex(String src) {
+        try {
+            MessageDigest md5 = MessageDigest.getInstance("MD5");
+            byte[] bs = md5.digest(src.getBytes());
+            return new String(new Hex().encode(bs));
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+
+    // 测试主函数
+    public static void main(String args[]) {
+            // 原文
+            String plaintext = "DingSai";
+            //  plaintext = "123456";
+            System.out.println("原始:" + plaintext);
+            System.out.println("普通MD5后:" + MD5Util.MD5(plaintext));
+
+            // 获取加盐后的MD5值
+            String ciphertext = MD5Util.MD5Salt(plaintext);
+            System.out.println("加盐后MD5:" + ciphertext);
+            System.out.println("是否是同一字符串:" + MD5Util.verify(plaintext, ciphertext));
+            /**
+             * 其中某次DingSai字符串的MD5值
+             */
+            String[] tempSalt = { "c4d980d6905a646d27c0c437b1f046d4207aa2396df6af86", "66db82d9da2e35c95416471a147d12e46925d38e1185c043", "61a718e4c15d914504a41d95230087a51816632183732b5a" };
+
+            for (String temp : tempSalt) {
+                System.out.println("是否是同一字符串:" + MD5Util.verify(plaintext, temp));
+            }
+    }
+}