All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.boot.common.encrypt.MD5 Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.boot.common.encrypt;

import matrix.boot.common.exception.ServiceException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5工具
 * @author wangcheng
 */
public class MD5 {

    /**
     * 加密为32位的MD5
     * @param content 内容
     * @return MD5 32位
     */
    public static String get32(String content) {
        StringBuilder str = new StringBuilder();
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new ServiceException(e);
        }
        md.update(content.getBytes());
        byte[] b = md.digest();
        int i;
        for (byte value : b) {
            i = value;
            if (i < 0) {
                i += 256;
            }
            if (i < 16) {
                str.append("0");
            }
            str.append(Integer.toHexString(i));
        }
        return str.toString().toUpperCase();
    }

    /**
     * 加密为16位的MD5
     * @param content 内容
     * @return MD5 16位
     */
    public static String get16(String content) {
        String md5 = MD5.get32(content);
        return md5.substring(8, 24);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy