Java SHA1 sha1HashInt(String text)

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

Description

Compute the SHA-1 hash of a string, and return a truncated int-value of the hash.

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

a truncated int-value of the hash.

Declaration

public static int sha1HashInt(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 w  w  w  . j a va  2 s  .  co  m
     * Compute the SHA-1 hash of a string, and return a truncated int-value of the hash. 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 a truncated int-value 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 int sha1HashInt(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        return truncateHashToInt(computeHash(text, HASH_SHA1));
    }

    /**
     * Computes a truncated code from the given hash-value and returns it as int-variable.
     *
     * @param hash the hash-string
     * @return truncated code as int
     */
    public static int truncateHashToInt(String hash) {
        return truncateHashToInt(hexToBytes(hash));
    }

    /**
     * Computes a truncated code from the given hash-value and returns it as int-variable.
     *
     * @param bytes hash-value as a byte-array
     * @return truncated code as int
     */
    public static int truncateHashToInt(byte[] bytes) {
        int offset = bytes[bytes.length - 1] & 0x0f;
        return (bytes[offset] & (0x7f << 24)) | (bytes[offset + 1] & (0xff << 16))
                | (bytes[offset + 2] & (0xff << 8)) | (bytes[offset + 3] & 0xff);
    }

    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();
    }

    /**
     * Convert a string consisting of hex-numbers into an array of bytes, which contains the binary representation of
     * those hexadecimal-numbers (takes pairs of numbers, so make sure the number of characters is even).
     *
     * @param hex String of pairs of hexadecimal numbers
     * @return byte-array with the binary representation of the hex-string
     */
    public static byte[] hexToBytes(String hex) {
        byte[] bytes = new byte[hex.length() >> 1];
        for (int i = 0; i < bytes.length; i++) {
            int baseIndex = i << 1;
            // in order to be able to parse the full range of 0x00 to 0xFF, we need a Short or Integer
            // to do the parsing, as Byte will throw an exception for values above or equal to 0x80.
            bytes[i] = (byte) Integer.parseInt(hex.substring(baseIndex, baseIndex + 2), 16);
        }
        return bytes;
    }
}

Related

  1. sha1Hash(String source)
  2. sha1Hash(String text)
  3. sha1Hash(String tohash)
  4. sha1Hash(String toHash)
  5. sha1HashHex(String data)
  6. sha1Hex(byte[] bytes)
  7. sha1Hex(String data)
  8. sha1hex(String source)
  9. sha1Java(String password)