Java Utililty Methods SHA1

List of utility methods to do SHA1

Description

The list of methods to do SHA1 are organized into topic(s).

Method

StringdigestSha1Hex(String source)
Hashes the source with SHA1 and returns the resulting hash as an hexadecimal string.
String sha1 = "";
try {
    MessageDigest crypt = MessageDigest.getInstance("SHA-1");
    crypt.reset();
    crypt.update(source.getBytes("UTF-8"));
    sha1 = byteToHex(crypt.digest());
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
...
StringdoSHA1(byte[] buf)
do SHA
return doSHA1(buf, 0, buf.length);
StringdoSHA1(byte[] buf, int off, int len)
do SHA
Formatter f = null;
try {
    MessageDigest md = MessageDigest.getInstance("SHA1");
    md.update(buf, off, len);
    byte[] fingerprintVector = md.digest();
    f = new Formatter();
    for (int i = 0; i < fingerprintVector.length; i++) {
        if (i == fingerprintVector.length - 1)
...
StringgenerateSHA1(String message)
generate SHA
return hashString(message, "SHA-1");
voidgenerateSHA1ChecksumFile(String filename)
generate SHA Checksum File
generateSHA1ChecksumFile(new File(filename).getAbsoluteFile());
StringgenerateSHA1Key(String input)
Generates a SHA1 hash from a string.
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();
...
byte[]generateSHA1RSASignature(RSAPrivateKey privKey, byte[] text)
Given the privateKey produce an RSA signature for the SHA1 hash of a given text.
try {
    Signature mySig = Signature.getInstance("SHA1withRSA");
    mySig.initSign(privKey);
    mySig.update(text);
    return mySig.sign();
} catch (Exception e) {
    System.err.println("Exception in generateSHA1RSASignature: " + e);
    return null;
...
StringgenerateSHA1String(String stringToEncode)
Generates the SHA-1 digest for a given String based on UTF-8.
return generateDigestString(stringToEncode, "SHA-1", "UTF-8", 40);
byte[]sha1(byte[] bytes)
returns a 20 byte sha1 hash
try {
    MessageDigest sha = MessageDigest.getInstance("SHA-1");
    byte[] result = sha.digest(bytes);
    return result;
} catch (Exception x) {
return null;
byte[]sha1(byte[] bytes)
Returns a 20-byte SHA-1 hash of a set of bytes.
return digest(bytes, "SHA-1");