Android SHA256 Hash Create sha256(String string)

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

Description

Generate a SHA256 checksum of a string.

Parameter

Parameter Description
string to SHA256

Return

A SHA256 string

Declaration

public static String sha256(String string) 

Method Source Code

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

public class Main {
    /**//from w w w  .j av a  2s  .  co m
     * Generate a SHA256 checksum of a string.
     * 
     * @param string to SHA256
     * @return A SHA256 string
     */
    public static String sha256(String string) {

        MessageDigest digest = null;
        String hash = "";
        try {
            digest = MessageDigest.getInstance("SHA-256");
            digest.update(string.getBytes());
            byte[] bytes = digest.digest();

            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < bytes.length; i++) {
                String hex = Integer.toHexString(0xFF & bytes[i]);
                if (hex.length() == 1) {
                    sb.append('0');
                }
                sb.append(hex);
            }
            hash = sb.toString();

        } catch (NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        }
        return hash;
    }
}

Related

  1. sha256(String input)
  2. sha256(byte[] data)
  3. sha256(byte[] data, int offset, int length)
  4. sha256(byte[] data1, byte[] data2)
  5. sha256(final String aString)