Java SHA1 sha1(String data)

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

Description

Hashes a String using the SHA-1 algorithm and returns the result as a String of hexadecimal numbers.

License

Apache License

Declaration

public static String sha1(String data) 

Method Source Code


//package com.java2s;
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./* w ww  .j  av  a 2  s  . c om*/
 */

import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

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

    /** 
     * Hashes a String using the SHA-1 algorithm and returns the result as a
     * String of hexadecimal numbers.
     */
    public static String sha1(String data) {
        return hash(data, SHA1);
    }

    /** 
     * Hashes a byte array using the SHA-1 algorithm and returns the result as 
     * a String of hexadecimal numbers.
     */
    public static String sha1(byte[] data) {
        return hash(data, SHA1);
    }

    /**
     * Hashes a String using the specified algorithm and returns the result as 
     * a String of hexadecimal numbers.
     * 
     * @param data the String to compute the hash of
     * @return the computed hash
     */
    public static String hash(String data, String algorithm) {
        return hash(data.getBytes(), algorithm);
    }

    /**
     * Hashes a byte array using the specified algorithm and returns the 
     * result as a String of hexadecimal numbers.
     * 
     * @param data the bytes to compute the hash of
     * @return the computed hash
     */
    public static String hash(byte[] data, String algorithm) {
        MessageDigest digest = createDigest(algorithm);
        digest.update(data);
        return toHex(digest.digest());
    }

    /**
     * Hashes data read from an InputStream using the specified algorithm 
     * and returns the result as a String of hexadecimal numbers.
     * 
     * @param in the stream to read the data from
     * @return the computed hash
     */
    public static String hash(InputStream in, String algorithm) throws IOException {

        MessageDigest digest = createDigest(algorithm);
        byte[] buffer = new byte[8192];
        int i = 0;
        while ((i = in.read(buffer)) > 0) {
            digest.update(buffer, 0, i);
        }
        in.close();
        return toHex(digest.digest());
    }

    /**
     * Creates a MessageDigest for the given algorithm. 
     * NoSuchAlgorithmExceptions are caught and re-thrown as RuntimeExceptions.
     */
    private static MessageDigest createDigest(String algorithm) {
        try {
            return MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Converts an array of bytes into a String representing each byte as an
     * unsigned hex number.
     * 
     * @param buffer array of bytes to convert
     * @return generated hex string
     */
    private static String toHex(byte[] buffer) {
        StringBuffer sb = new StringBuffer();
        String s = null;
        for (int i = 0; i < buffer.length; i++) {
            s = Integer.toHexString((int) buffer[i] & 0xff);
            if (s.length() < 2) {
                sb.append('0');
            }
            sb.append(s);
        }
        return sb.toString();
    }
}

Related

  1. sha1(final String todigest)
  2. SHA1(InputStream in)
  3. SHA1(MessageDigest messageDigest, String... texts)
  4. sha1(Object object)
  5. sha1(String data)
  6. sha1(String data)
  7. sha1(String data)
  8. SHA1(String decript)
  9. sha1(String input)