Java SHA1 sha1Hash(String text)

Here you can find the source of sha1Hash(String text)

Description

Compute the SHA-1 hash of a string, and return it has a string of hexadecimal numbers.

License

Open Source License

Parameter

Parameter Description
text Text to compute the SHA-1 hash of

Exception

Parameter Description
NoSuchAlgorithmException thrown if the SHA-1 algorithm cannot be found
UnsupportedEncodingException thrown if the encoding is not supported

Return

the hexadecimal representation of the hash

Declaration

public static String sha1Hash(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException 

Method Source Code


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

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

    /**/*from   ww  w  .j a va2 s  .c o  m*/
     * Compute the SHA-1 hash of a string, and return it has a string of hexadecimal numbers. The incoming text is assumed
     * to be UTF-8 encoded. An exception will be thrown if this encoding is not supported.
     *
     * @param text Text to compute the SHA-1 hash of
     * @return the hexadecimal representation of the hash
     * @throws NoSuchAlgorithmException thrown if the SHA-1 algorithm cannot be found
     * @throws UnsupportedEncodingException thrown if the encoding is not supported
     */
    public static String sha1Hash(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        return bytesToHex(computeHash(text, HASH_SHA1));
    }

    /**
     * Convert an array of arbitrary bytes into a String of hexadecimal number-pairs with each pair representing on byte
     * of the array.
     *
     * @param bytes the array to convert into hexadecimal string
     * @return the String containing the hexadecimal representation of the array
     */
    public static String bytesToHex(byte[] bytes) {
        final char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] hexChars = new char[bytes.length << 1];
        for (int i = 0; i < bytes.length; i++) {
            int value = bytes[i] & 0xFF;
            int baseIndex = i << 1;
            hexChars[baseIndex] = hexArray[value >>> 4];
            hexChars[baseIndex + 1] = hexArray[value & 0x0F];
        }
        return new String(hexChars);
    }

    private static byte[] computeHash(String text, String algorithm)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        md.update(text.getBytes("UTF-8"), 0, text.length());
        return md.digest();
    }
}

Related

  1. sha1Hash(String input)
  2. sha1Hash(String input)
  3. sha1hash(String key)
  4. sha1Hash(String s)
  5. sha1Hash(String source)
  6. sha1Hash(String tohash)
  7. sha1Hash(String toHash)
  8. sha1HashHex(String data)
  9. sha1HashInt(String text)