Java SHA256 sha256(String str)

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

Description

sha

License

Apache License

Declaration

public static String sha256(String str) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.security.MessageDigest;

public class Main {
    public static String sha256(String str) {
        try {/*w  w  w. j a va 2 s .  com*/
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(str.getBytes());

            byte[] b = md.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < b.length; i++) {
                int v = (int) b[i];
                v = v < 0 ? 0x100 + v : v;
                String cc = Integer.toHexString(v);
                if (cc.length() == 1)
                    sb.append('0');
                sb.append(cc);
            }

            return sb.toString();
        } catch (Exception e) {
        }
        return "";
    }
}

Related

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