Java SHA sha(byte[] data)

Here you can find the source of sha(byte[] data)

Description

Calculates the SHA digest and returns the symbol as a byte[].

License

Open Source License

Parameter

Parameter Description
data Data to digest

Return

SHA digest

Declaration

public static byte[] sha(byte[] data) 

Method Source Code

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

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static final String SHA_ALGORITHM = "SHA-1";

    /**//from ww  w .j  a  v  a2 s.co  m
     * Calculates the SHA digest and returns the symbol as a
     * <code>byte[]</code>.
     *
     * @param data Data to digest
     * @return SHA digest
     */
    public static byte[] sha(byte[] data) {
        return getSHA().digest(data);
    }

    /**
     * Calculates the SHA digest and returns the symbol as a
     * <code>byte[]</code>.
     *
     * @param data Data to digest
     * @return SHA digest
     */
    public static byte[] sha(String data) {
        return sha(data.getBytes());
    }

    /**
     * Returns an SHA digest.
     *
     * @return An SHA digest creating.
     * @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
     */
    private static MessageDigest getSHA() {
        return getMessageDigest(SHA_ALGORITHM);
    }

    public static MessageDigest getMessageDigest(String algorithm) {
        try {
            return MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}

Related

  1. sha(byte data[])
  2. SHA(String s)
  3. sha(String source)
  4. sha(String str)
  5. sha(String strPlain)