当前位置 : 主页 > 编程语言 > java >

SHA256

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class SHA256 { public static String encrypt(String strSrc) { MessageDigest md; String strDes; String encName = "SHA-256"; byte[] bt = strS
gistfile1.txt
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class SHA256 {

    public static String encrypt(String strSrc) {
        MessageDigest md;
        String strDes;
        String encName = "SHA-256";

        byte[] bt = strSrc.getBytes();
        try {
            md = MessageDigest.getInstance(encName);
            md.update(bt);
            strDes = bytes2Hex(md.digest());
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        return strDes;
    }

    private static String bytes2Hex(byte[] bts) {
        StringBuilder des = new StringBuilder();
        String tmp;
        for (byte bt : bts) {
            tmp = (Integer.toHexString(bt & 0xFF));
            if (tmp.length() == 1) {
                des.append("0");
            }
            des.append(tmp);
        }
        return des.toString();
    }
}
网友评论