Java SHA SHAsum(byte[] input)

Here you can find the source of SHAsum(byte[] input)

Description

Creates the SHA1 hash for a given array of bytes.

License

Open Source License

Parameter

Parameter Description
input array of bytes.

Return

String representation of SHA1 hash.

Declaration

public static String SHAsum(byte[] input) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

public class Main {
    /**/*from   w ww.j  av  a  2s .c  om*/
     * Creates the SHA1 hash for a given array of bytes.
     * @param input array of bytes.
     * @return String representation of SHA1 hash.
     */
    public static String SHAsum(byte[] input) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("SHA-1");
            return byteArray2Hex(md.digest(input));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Takes an array of bytes that should represent a hexadecimal value.
     * Returns string representation of these values.
     * @param bytes bytes containing hex symbols.
     * @return String representation of the byte[]
     */
    private static String byteArray2Hex(final byte[] bytes) {
        Formatter formatter = new Formatter();
        for (byte b : bytes) {
            formatter.format("%02x", b);
        }
        return formatter.toString();
    }
}

Related

  1. SHAHash(byte[] input)
  2. shaHash(String message)
  3. shaPsw(String inputText)
  4. SHAsum(byte[] convertme)
  5. SHAsum(byte[] convertme, boolean prefix, boolean uppercase)