美文网首页
Java实现SHA-256

Java实现SHA-256

作者: coderTG | 来源:发表于2018-04-04 15:45 被阅读0次

    源码:


    public class ShaUtil {

    private ShaUtil() {

    }

    public static String SHA256(String str) {

    MessageDigest messageDigest;

    String encodeStr = "";

    try {

    messageDigest = MessageDigest.getInstance("SHA-256");

    messageDigest.update(str.getBytes("UTF-8"));

    encodeStr = byte2Hex(messageDigest.digest());

    } catch (Exception e) {

    System.out.println("getSHA256 is error" + e.getMessage());

    }

    return encodeStr;

    }

    public static String MD5(String str) {

    String resultStr = MD5.digest(str);

    return resultStr.substring(4, resultStr.length());

    }

    public static String UUID() {

    return UUID.randomUUID().toString().replaceAll("\\-", "");

    }

    private static String byte2Hex(byte[] bytes) {

    StringBuilder builder = new StringBuilder();

    String temp;

    for (int i = 0; i < bytes.length; i++) {

    temp = Integer.toHexString(bytes[i] & 0xFF);

    if (temp.length() == 1) {

    builder.append("0");

    }

    builder.append(temp);

    }

    return builder.toString();

    }

    }

    相关文章

      网友评论

          本文标题:Java实现SHA-256

          本文链接:https://www.haomeiwen.com/subject/zsqkhftx.html