当前位置:网站首页>MD5Util

MD5Util

2022-06-11 00:12:00 Mr. No

package com.sinosoft.platform.common.util;

import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;

/**
 * @Auther:hcg
 * @Description:
 */
@Slf4j
public class MD5Util {

    public static String md5(String content) {
        //  Characters for encryption 
        char[] md5String = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        try {
            //  Use the platform default character set to md5String Encoded as byte Sequence , And store the results in a new byte Array 
            byte[] byteInput = content.getBytes();

            //  Information digest is a secure one-way hash function , It receives data of any size , And output fixed length hash value 
            MessageDigest mdInst = MessageDigest.getInstance("MD5");

            // MessageDigest Object by using update Method processing data , Use specified byte Array update summary 
            mdInst.update(byteInput);

            // After the summary is updated, call digest()  Perform hash calculation , Get the ciphertext 
            byte[] md = mdInst.digest();

            // Convert ciphertext into 16 Hexadecimal string form 
            int j = md.length;
            char[] str = new char[j*2];
            int k = 0;
            for (int i=0;i<j;i++) {
                byte byte0 = md[i];
                str[k++] = md5String[byte0 >>> 4 & 0xf];
                str[k++] = md5String[byte0 & 0xf];
            }
            //  Returns the encrypted string 
            return new String(str);

        }catch (Exception e) {
            log.warn(e.getMessage(),e);
            return null;
        }

    }
}
原网站

版权声明
本文为[Mr. No]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206102252108519.html