MessageDigest類實現md5加密

項目中用到的md5工具類:java

 1 package com.mall.util;
 2 
 3 import org.springframework.util.StringUtils;
 4 
 5 import java.security.MessageDigest;
 6 
 7 /**
 8  * Created by geely
 9  */
10 public class MD5Util {
11 
12     /**
13      * 將字節數組轉爲十六進制數
14      * @param b
15      * @return
16      */
17     private static String byteArrayToHexString(byte b[]) {
18         StringBuffer resultSb = new StringBuffer();
19         for (int i = 0; i < b.length; i++) {
20             //每一個字節轉爲十六進制數後進行拼接
21             resultSb.append(byteToHexString(b[i]));
22         }
23         return resultSb.toString();
24     }
25 
26     /**
27      * 將某一個字節轉爲十六進制數
28      * @param b
29      * @return
30      */
31     private static String byteToHexString(byte b) {
32         int n = b;
33         if (n < 0)
34             n += 256;
35         int d1 = n / 16;
36         int d2 = n % 16;
37         return hexDigits[d1] + hexDigits[d2];
38     }
39 
40     /**
41      * 返回大寫MD5
42      *
43      * @param origin 要加密的原字符串
44      * @param charsetname 加密算法使用的字符集
45      * @return
46      */
47     private static String MD5Encode(String origin, String charsetname) {
48         String resultString = null;
49         try {
50             resultString = new String(origin);
51             //初始化md5算法
52             MessageDigest md = MessageDigest.getInstance("MD5");
53             //md.digest(resultString.getBytes())獲取數據的信息摘要,返回字節數組
54             //byteArrayToHexString()將字節數組轉爲十六進制數
55             if (charsetname == null || "".equals(charsetname)) {
56                 //若是不傳入字符集,則調用默認字符集
57                 resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
58             }
59             else {
60                 resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
61             }
62         } catch (Exception exception) {
63         }
64         return resultString.toUpperCase();
65     }
66 
67     /**
68      * 惟一public方法對外公開
69      * @param origin
70      * @return
71      */
72     public static String MD5EncodeUtf8(String origin) {
73         //對原字符串加鹽值返回
74         origin = origin + PropertiesUtil.getProperty("password.salt", "");
75         //傳入utf-8字符集
76         return MD5Encode(origin, "utf-8");
77     }
78 
79     //十六進制數組值
80     private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5",
81             "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
82 
83 }
View Code

調用:git

(MD5Util.MD5EncodeUtf8(passwordNew)

一、消息摘要的簡介算法

     1.1消息摘要的概念spring

              惟一對應一個消息或文本的固定長度的值,由一個單向Hash加密函數對消息進行做用而產生。編程

     1.2 消息摘要的分類數組

            (1) MD (Message Digest)  消息摘要算法安全

            (2) SHA(Secure Hash Algorithm) 安全散列算法app

            (3) MAC(Message Authentication Code) 消息認證碼算法ide

二、MD算法系列函數

       2.1  MD算法的基本概念

              爲計算機安全領域普遍使用的一種散列函數,用以提供消息的完整性保護。

       2.2   MD算法的種類

               MD算法系列(JDK)

      

       2.3  MD 算法編程使用

            

MD5算法,能夠用來保存用戶的密碼信息。爲了更好的保存,能夠在保存的過程當中,加入鹽。/在保存用戶密碼的時候,鹽能夠利用生成的隨機數。能夠將密碼結合MD5加鹽,生成的數據摘要和鹽保存起來 。以便於下次用戶驗證使用。在用戶表裏面,也保存salt。

MD5和SHA比較:http://blog.csdn.net/xiaokui_wingfly/article/details/38045871?utm_source=tuicool&utm_medium=referral

相關文章
相關標籤/搜索