36°

Java MD5加密工具类

Java MD5 摘要算法工具类

package com.mew.kittyapi.util;

import java.nio.charset.Charset; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;

public class Md5Utils {

private static Charset charset_utf8 = Charset.forName("UTF-8");

private static Charset charset_gbk = Charset.forName("GBK");

public static String md5(String str) {
    String md5 = "";
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] bytes = str.getBytes(charset_utf8);
        bytes = digest.digest(bytes);
        md5 = toHexStr(bytes);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return md5;
}


private static String toHexStr(byte[] bytes) {
    StringBuilder sb = new StringBuilder();
    int var;
    for (byte b : bytes) {
        var = ((int) b) & 0xFF;
        if (var < 16) {
            sb.append("0");
        }
        sb.append(Integer.toHexString(var));
    }
    return sb.toString().toUpperCase();
}

}

 

本文由【皮皮猫32】发布于开源中国,原文链接:https://my.oschina.net/pipimao/blog/3071972

全部评论: 0

    我有话说: