zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    __weak&__block&__unsafe__unretain
    Unable to convert MySQL date/time value to System.DateTime 错误
    做最好的自己,人生十件事(事业,人生,情感)
    52张扑克牌的Suit(花色)和Rank(牌面大小)排序算法
    windows service在服务器上部署时的问题
    "Unable to compile template. Check the Errors list for details" 问题解决办法
    Javascript中的一些自有方法
    winform中的webbrowser里面操作html代码问题
    Javascript,Jquery实现页面图片预加载百分比展现
    【delphi XE 】 Margin属性 转
  • 原文地址:https://www.cnblogs.com/cing/p/7908387.html
Copyright © 2011-2022 走看看