MD5 加盐(Java)

本文转载自:https://blog.csdn.net/dingsai88/article/details/51637977

jar:https://pan.baidu.com/s/1-8JaRoUtzc1fV8lqhFCBoA

  1 package util;
  2 
  3 import java.security.MessageDigest;
  4 import java.security.NoSuchAlgorithmException;
  5 import java.util.Random;
  6 
  7 import org.apache.commons.codec.binary.Hex;
  8 
  9 public class MD5Util {
 10     /**
 11      * 普通MD5
 12      * 
 13      * @author daniel
 14      * @time 2016-6-11 下午8:00:28
 15      * @param inStr
 16      * @return
 17      */
 18     public static String MD5(String input) {
 19         MessageDigest md5 = null;
 20         try {
 21             md5 = MessageDigest.getInstance("MD5");
 22         } catch (NoSuchAlgorithmException e) {
 23             return "check jdk";
 24         } catch (Exception e) {
 25             e.printStackTrace();
 26             return "";
 27         }
 28         char[] charArray = input.toCharArray();
 29         byte[] byteArray = new byte[charArray.length];
 30 
 31         for (int i = 0; i < charArray.length; i++)
 32             byteArray[i] = (byte) charArray[i];
 33         byte[] md5Bytes = md5.digest(byteArray);
 34         StringBuffer hexValue = new StringBuffer();
 35         for (int i = 0; i < md5Bytes.length; i++) {
 36             int val = ((int) md5Bytes[i]) & 0xff;
 37             if (val < 16)
 38                 hexValue.append("0");
 39             hexValue.append(Integer.toHexString(val));
 40         }
 41         return hexValue.toString();
 42 
 43     }
 44 
 45     /**
 46      * 加盐MD5
 47      * 
 48      * @author daniel
 49      * @time 2016-6-11 下午8:45:04
 50      * @param password
 51      * @return
 52      */
 53     public static String generate(String password) {
 54         Random r = new Random();
 55         StringBuilder sb = new StringBuilder(16);
 56         sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));
 57         int len = sb.length();
 58         if (len < 16) {
 59             for (int i = 0; i < 16 - len; i++) {
 60                 sb.append("0");
 61             }
 62         }
 63         String salt = sb.toString();
 64         password = md5Hex(password + salt);
 65         char[] cs = new char[48];
 66         for (int i = 0; i < 48; i += 3) {
 67             cs[i] = password.charAt(i / 3 * 2);
 68             char c = salt.charAt(i / 3);
 69             cs[i + 1] = c;
 70             cs[i + 2] = password.charAt(i / 3 * 2 + 1);
 71         }
 72         return new String(cs);
 73     }
 74 
 75     /**
 76      * 校验加盐后是否和原文一致
 77      * 
 78      * @author daniel
 79      * @time 2016-6-11 下午8:45:39
 80      * @param password
 81      * @param md5
 82      * @return
 83      */
 84     public static boolean verify(String password, String md5) {
 85         char[] cs1 = new char[32];
 86         char[] cs2 = new char[16];
 87         for (int i = 0; i < 48; i += 3) {
 88             cs1[i / 3 * 2] = md5.charAt(i);
 89             cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);
 90             cs2[i / 3] = md5.charAt(i + 1);
 91         }
 92         String salt = new String(cs2);
 93         return md5Hex(password + salt).equals(new String(cs1));
 94     }
 95 
 96     /**
 97      * 获取十六进制字符串形式的MD5摘要
 98      */
 99     private static String md5Hex(String src) {
100         try {
101             MessageDigest md5 = MessageDigest.getInstance("MD5");
102             byte[] bs = md5.digest(src.getBytes());
103             return new String(new Hex().encode(bs));
104         } catch (Exception e) {
105             return null;
106         }
107     }
108 
109     public static void main(String[] args) {
110         String text = "zxcvbnm";
111         System.out.println("原始的:" + MD5(text));
112         System.out.println("加盐后:" + generate(text));
113 
114         System.out.println("比较后:" + verify("zxcvbnm0", generate(text)));
115         
116         //tempSalt 某一次加盐后的值
117         String[] tempSalt = { 
118                 "66b120750d59952c8755b66eb3f59242e95ba51d2648bb09",
119                 "e58361192f98839e10c6b64ce8b688301b4e722a8f672505",
120                 "52817d504304212a46f58f81b9bd42a57f2468fc73b79b61" 
121         };
122 
123         for (String temp : tempSalt) {
124             System.out.println("是否是同一字符串:" + verify("zxcvbnm", temp));
125         }
126     }
127 }

 

posted @ 2018-06-14 11:43  得一小丑足矣  阅读(1003)  评论(0编辑  收藏  举报