Java SHA256 sha256(String srcStr)

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

Description

sha

License

Open Source License

Declaration

public static String sha256(String srcStr) 

Method Source Code

//package com.java2s;
import java.security.MessageDigest;

public class Main {
    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static String sha256(String srcStr) {
        return hash("SHA-256", srcStr);
    }//from   w  ww . ja v a2s. c om

    public static String hash(String algorithm, String srcStr) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            byte[] bytes = md.digest(srcStr.getBytes("utf-8"));
            return toHex(bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static String toHex(byte[] bytes) {
        StringBuilder ret = new StringBuilder(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
            ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
        }
        return ret.toString();
    }
}

Related

  1. sha256(String password, String salt)
  2. sha256(String plainText)
  3. sha256(String raw)
  4. sha256(String s)
  5. sha256(String src)
  6. sha256(String str)
  7. sha256(String str)
  8. sha256(String string)
  9. sha256(String string, String secret)