Java SHA256 sha256(String src)

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

Description

Calculates the SHA-256 digest and returns the value as a 64 character hex string.

License

Apache License

Parameter

Parameter Description
src a string to digest

Return

SHA-256 digest as a hex string

Declaration

public static String sha256(String src) 

Method Source Code


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

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

public class Main {
    private static MessageDigest md;

    /**//from  w w w . ja  v a  2s  . c  o  m
     * Calculates the SHA-256 digest and returns the value as a 64 character hex
     * string.
     * 
     * @param src
     *            a string to digest
     * @return SHA-256 digest as a hex string
     */
    public static String sha256(String src) {
        try {
            md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        md.update(src.getBytes(StandardCharsets.UTF_8));

        return String.format("%064x", new BigInteger(1, md.digest()));
    }
}

Related

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