Java SHA256 sha256HashHex(String data)

Here you can find the source of sha256HashHex(String data)

Description

sha Hash Hex

License

Open Source License

Declaration

static public String sha256HashHex(String data) 

Method Source Code

//package com.java2s;
/*/*  w  w w  .j a  v  a2 s. c  om*/
 * Copyright 2014-2017.
 * Distributed under the terms of the GPLv3 License.
 *
 * Authors:
 *      Clemens Zeidler <czei002@aucklanduni.ac.nz>
 */

import java.security.*;

public class Main {
    static public String sha256HashHex(byte data[]) {
        return toHex(sha256Hash(data));
    }

    static public String sha256HashHex(String data) {
        return sha256HashHex(data.getBytes());
    }

    public static String toHex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < bytes.length; i++)
            stringBuffer.append(String.format("%02X", bytes[i]));

        return stringBuffer.toString().toLowerCase();
    }

    static public MessageDigest sha256Hash() throws NoSuchAlgorithmException {
        return MessageDigest.getInstance("SHA-256");
    }

    static public byte[] sha256Hash(byte data[]) {
        try {
            return hash(data, sha256Hash());
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    static public byte[] hash(byte[] data, MessageDigest messageDigest) {
        messageDigest.reset();
        messageDigest.update(data);
        return messageDigest.digest();
    }
}

Related

  1. sha256encrypt(String phrase)
  2. sha256Hash()
  3. sha256Hash(byte[] data)
  4. sha256Hash(File file)
  5. sha256Hash(final String tohash)
  6. SHA256Sum(final @Nonnull byte[] bytes)