Cugu's blog

IT security & forensics

RSSTwitterGithub

sha1sum benchmark of Go, Python and Java

I want to start an IO heavy programming project. To choose an appropriate programming language I ran some benchmarks to calculate a sha1 hash sum for a given input file and print the hex value. All code is listed below.

I ran the code on my mid 2011 MacBook Air and my Windows computer. Both are equipped with a SSD drive. As an input I used a random 1 GiB file. The following table lists the runtime of the code.

macOS
2 Core 1,7 GHz
4 GB RAM
Windows
4 Core 3,2 GHz
8 GB RAM
Go 1.8 5.3 s 2.0 s
Java 8 121 10.7 s 5.0 s
Python 3.6.0 5.3 s 2.0 s

These results are not really scientifically sound but show a much better performance for Go and Python over Java.

You can send me feedback on this post via Twitter.

Code

Go sha1sum

    package main

    import (
        "bufio"
        "crypto/sha1"
        "fmt"
        "io"
        "os"
    )

    func main() {
        f, _ := os.Open(os.Args[1])
        reader := bufio.NewReader(f)
        sha1 := sha1.New()
        io.Copy(sha1, reader)
        fmt.Printf("%x\n", sha1.Sum(nil))
    }

Python sha1sum

    import sys
    import hashlib

    sha1 = hashlib.sha1()

    with open(sys.argv[1], 'rb') as f:
        while True:
            data = f.read(32 * 1024)
            if not data:
                break
            sha1.update(data)

    print(sha1.hexdigest())

Java sha1sum

    import java.security.MessageDigest;
    import java.io.InputStream;
    import java.security.DigestInputStream;
    import java.nio.file.Paths;
    import java.nio.file.Files;

    public class Hash {
     public static void main(String[] args) {
      try {
       MessageDigest digest = MessageDigest.getInstance("SHA-1");
       InputStream fis = Files.newInputStream(Paths.get(args[0]));

       int n = 0;
       byte[] buffer = new byte[32 * 1024];
       while (n != -1) {
        n = fis.read(buffer);
        if (n > 0) {
         digest.update(buffer, 0, n);
        }
       }

       byte test[] = digest.digest();
       for (int j = 0; j < test.length; j++) {
        System.out.format("%02X", test[j]);
       }
       System.out.println();

      } catch (Exception ex) {
       System.out.println(ex);
      }
     }
    }