How to Compute Hash Code of Streams

3 years ago by in Crypto, How To, IO Tagged: , , ,

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.

Albert Attard

Albert Attard is a Java passionate and technical lead at a research group. You can find him on . Over the past years Albert worked on various Java projects including traditional server/client applications, modular applications, large data handling applications and concurrent data manipulation applications to name a few. He has a BSc degree from the University of London (Homepage) and an MSc Information Security with the same university. His MSc thesis (Book) received the 2012 award (Website).

Leave a Comment

Time limit is exhausted. Please reload the CAPTCHA.