Android SHA256 Hash Create sha256Byte(byte[] in)

Here you can find the source of sha256Byte(byte[] in)

Description

Returns a byte array representation of the hash of a byte array input using the SHA256 hashing algorithm.

Parameter

Parameter Description
in - The array of bytes to be hashed.

Return

A byte array representation of the input.

Declaration

public static byte[] sha256Byte(byte[] in) 

Method Source Code

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

public class Main {
    /**/*from   ww  w  .  j  av  a 2 s.co  m*/
     * Returns a byte array representation of the hash 
     * of a string input using the SHA256 hashing algorithm.
     * 
     * @param in
     *            - The String to be hashed.
     * @return A byte array of the hashed input.
     */
    public static byte[] sha256Byte(String in) {
        return digestByte("SHA256", in.getBytes());
    }

    /**
     * Returns a byte array representation of the hash 
     * of a byte array input using the SHA256 hashing algorithm.
     * 
     * @param in
     *            - The array of bytes to be hashed.
     * @return A byte array representation of the input.
     */
    public static byte[] sha256Byte(byte[] in) {
        return digestByte("SHA256", in);
    }

    /**
     * Returns a digest, in the form of an array of bytes, using various hashing
     * algorithms. This is a private function used by many of the public
     * functions within this class.
     * 
     * @param algorithm
     *            - The hashing algorithm to be used.
     * @param input
     *            - The array of bytes to be hashed.
     * @return An array of bytes representing the hash result.
     */
    private static byte[] digestByte(String algorithm, byte[] input) {
        MessageDigest m;
        try {
            m = MessageDigest.getInstance(algorithm);
            m.update(input, 0, input.length);
            return m.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. sha256(byte[] data)
  2. sha256(byte[] data, int offset, int length)
  3. sha256(byte[] data1, byte[] data2)
  4. sha256(final String aString)
  5. sha256Byte(String in)
  6. doubleSha256(byte[] data)
  7. doubleSha256(byte[] data, int offset, int length)
  8. doubleSha256TwoBuffers(byte[] data1, byte[] data2)
  9. SHA256(String text)