Java SHA256 sha256(String s)

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

Description

sha

License

Apache License

Declaration

public static String sha256(String s) 

Method Source Code


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

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

    public static String sha256(String s) {
        MessageDigest md = getMessageDigest("sha-256");
        return bytes2HexString(md.digest(s.getBytes(StandardCharsets.UTF_8)));
    }/*from w w  w.j  a va 2  s.c  o m*/

    static MessageDigest getMessageDigest(String name) {
        try {
            return MessageDigest.getInstance(name);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    static String bytes2HexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length << 2);
        for (byte x : b) {
            int hi = (x & 0xf0) >> 4;
            int lo = x & 0x0f;
            sb.append(HEX_CHARS[hi]);
            sb.append(HEX_CHARS[lo]);
        }
        return sb.toString();
    }
}

Related

  1. SHA256(String input)
  2. sha256(String message)
  3. sha256(String password, String salt)
  4. sha256(String plainText)
  5. sha256(String raw)
  6. sha256(String src)
  7. sha256(String srcStr)
  8. sha256(String str)
  9. sha256(String str)