How to compute the SHA-1 hash-code of a stream

Cryptographic Hash algorithms (Wiki), such as SHA-1 (Wiki), are known for digesting inputs of any size into a fixed length, nondeterministic, output. Irrespective of the input size, the output is always of a fixed length, 160 bits (or 20 bytes) in case of SHA-1. Different hash algorithms produce different lengths.

When processing large inputs, such as large files, it is convenient to work with streams, rather than loading all input to memory first. The following example illustrates how to compute the hash of a stream.

package com.javacreed.examples.crypto;

import java.util.Formatter;

public class Example {
  public static void main(final String[] args) throws Exception {

    final URL link = new URL("");
    try (DigestInputStream in = new DigestInputStream(new BufferedInputStream(link.openStream()),
        MessageDigest.getInstance("SHA-1"))) {
      // Read the stream and do nothing with it
      while ( != -1) {}

      // Get the digest and finialise the computation
      final MessageDigest md = in.getMessageDigest();
      final byte[] digest = md.digest();

      // Format as HEX
      try (Formatter formatter = new Formatter()) {
        for (final byte b : digest) {
          formatter.format("%02x", b);

        final String sha1 = formatter.toString();

This example applies to any other input/output stream, such as files and other cryptographic hash algorithms. The above code can be downloaded from