MessageDigest類實現md5加密


項目中用到的md5工具類:

 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

調用:

(MD5Util.MD5EncodeUtf8(passwordNew)

1、消息摘要的簡介

     1.1消息摘要的概念

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

     1.2 消息摘要的分類

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

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

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

2、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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM