Java SHA256 sha256(String strSrc)

Here you can find the source of sha256(String strSrc)

Description

sha

License

Open Source License

Declaration

public static String sha256(String strSrc) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static String sha256(String strSrc) {
        return Encrypt(strSrc, "SHA-256");
    }/* w  w w. j  av a2 s .  c  o m*/

    public static String Encrypt(String strSrc, String encName) {
        MessageDigest md = null;
        String strDes = null;

        byte[] bt = strSrc.getBytes();
        try {
            if (encName == null || encName.equals("")) {
                encName = "SHA-256";
            }
            md = MessageDigest.getInstance(encName);
            md.update(bt);
            strDes = bytes2Hex(md.digest()); // to HexString
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        return strDes;
    }

    public static String bytes2Hex(byte[] bts) {
        String des = "";
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }
}

Related

  1. sha256(String srcStr)
  2. sha256(String str)
  3. sha256(String str)
  4. sha256(String string)
  5. sha256(String string, String secret)
  6. SHA256(String text)
  7. SHA256(String text)
  8. sha256(String text)
  9. SHA256(String texto)