Java SHA1 sha1(byte[] bytes)

Here you can find the source of sha1(byte[] bytes)

Description

Returns a 20-byte SHA-1 hash of a set of bytes.

License

Open Source License

Parameter

Parameter Description
bytes the input bytes.

Return

the resultant 20-byte digest.

Declaration

public static byte[] sha1(byte[] bytes) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009-2016 Black Rook Software
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 ******************************************************************************/

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

public class Main {
    /**//from  w  ww. j  av  a2s.c  o  m
     * Returns a 20-byte SHA-1 hash of a set of bytes.
     * Can return null if this Java implementation cannot perform this,
     * but it shouldn't, since SHA-1 is mandatorily implemented for all implementations.
     * @param bytes the input bytes.
     * @return the resultant 20-byte digest.
     * @since 2.9.0
     * @see #digest(byte[], String)
     */
    public static byte[] sha1(byte[] bytes) {
        return digest(bytes, "SHA-1");
    }

    /**
     * Returns a hash of a set of bytes digested by an encryption algorithm.
     * Can return null if this Java implementation cannot perform this.
     * Do not use this if you care if the algorithm is provided or not.
     * @param bytes the bytes to encode.
     * @param algorithmName the name to the algorithm to use.
     * @return the resultant byte digest, or null if the algorithm is not supported.
     * @since 2.10.0
     */
    public static byte[] digest(byte[] bytes, String algorithmName) {
        try {
            return MessageDigest.getInstance(algorithmName).digest(bytes);
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }
}

Related

  1. generateSHA1ChecksumFile(String filename)
  2. generateSHA1Key(String input)
  3. generateSHA1RSASignature(RSAPrivateKey privKey, byte[] text)
  4. generateSHA1String(String stringToEncode)
  5. sha1(byte[] bytes)
  6. sha1(byte[] bytes)
  7. SHA1(byte[] bytes)
  8. SHA1(byte[] convertme)
  9. sha1(byte[] data)