Java SHA1 generateSHA1Key(String input)

Here you can find the source of generateSHA1Key(String input)

Description

Generates a SHA1 hash from a string.

License

Open Source License

Parameter

Parameter Description
input The string that is to be hashed.

Exception

Parameter Description
NoSuchAlgorithmException If the SHA1 hash algorithm is not available on the system.

Return

A SHA1 hash

Declaration

public static String generateSHA1Key(String input) throws NoSuchAlgorithmException 

Method Source Code

//package com.java2s;
// it under the terms of the GNU Lesser General Public License as published by

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

public class Main {
    /**//www  . j av  a2s . c o  m
     * Generates a SHA1 hash from a string.
     * @param input The string that is to be hashed.
     * @return A SHA1 hash
     * @throws NoSuchAlgorithmException If the SHA1 hash algorithm is not available on the system.
     */
    public static String generateSHA1Key(String input) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");

        String hash;

        byte[] bytes = digest.digest(input.getBytes());
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < bytes.length; i++) {

            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        hash = sb.toString();
        return hash;
    }
}

Related

  1. digestSha1Hex(String source)
  2. doSHA1(byte[] buf)
  3. doSHA1(byte[] buf, int off, int len)
  4. generateSHA1(String message)
  5. generateSHA1ChecksumFile(String filename)
  6. generateSHA1RSASignature(RSAPrivateKey privKey, byte[] text)
  7. generateSHA1String(String stringToEncode)
  8. sha1(byte[] bytes)
  9. sha1(byte[] bytes)