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

Stringsha1(byte[] input)
Generate SHA-1 as hex string (all lower-case).
MessageDigest md = null;
try {
    md = MessageDigest.getInstance("SHA1");
} catch (NoSuchAlgorithmException e) {
    throw new RuntimeException(e);
md.update(input);
byte[] digest = md.digest();
...
byte[]SHA1(byte[] input)
SHA
return sha1.digest(input);
byte[]sha1(byte[] message)
sha
MessageDigest md = getSha1Digest();
return md.digest(message);
byte[]SHA1(byte[] src)
SHA
try {
    MessageDigest crypt = MessageDigest.getInstance("SHA-1");
    ;
    crypt.reset();
    crypt.update(src);
    return crypt.digest();
} catch (NoSuchAlgorithmException e) {
return new byte[0];
byte[]SHA1(ByteBuffer buf, int offset, int size)
SHA
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
for (int i = offset; i < offset + size; i++) {
    sha1.update(buf.get(i));
return sha1.digest();
Stringsha1(File f)
sha
return toHexString(sha1hash(f));
Stringsha1(File file)
sha
try {
    MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
    try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) {
        byte[] buffer = new byte[8192];
        int read = 0;
        while ((read = stream.read(buffer)) != -1) {
            messageDigest.update(buffer, 0, read);
    try (Formatter formatter = new Formatter()) {
        for (byte b : messageDigest.digest()) {
            formatter.format("%02x", b);
        return formatter.toString();
} catch (Exception ex) {
return null;
Stringsha1(File file)
Returns the SHA1 hash of a File
if (file == null) {
    return null;
return sha1(file.toPath());
Stringsha1(File sourceFile)
Computes the hash of a file or directory.
byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("SHA-1");
updateDigest(complete, sourceFile, buffer);
byte[] bytes = complete.digest();
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
    sb.append(String.format("%02x", b));
return sb.toString();
byte[]sha1(final byte[] bytes)
Gets the given byte array's SHA-1 checksum, or null if unavailable.
return digest("SHA-1", bytes);