Hristo Hristov
Hristo Hristov

Reputation: 4151

How to calculate SHA in parallel?

Calculation of SHA variants is simple, you create a message digest, update it with data and get the result. But, if we have many cores, how to do that in parallel? You can not calculate it on blocks and hash the results, because the overall result will not be correct. How to do this calculation in parallel, getting the same result?

The following is sequential java example:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class BenchmarkSha256 {

  public static void main(String... args) throws NoSuchAlgorithmException {
    int size = 1024 * 1024;
    byte[] bytes = new byte[size];
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    long startTime = System.nanoTime();
    for (int i = 0; i < 1024; i++)
      md.update(bytes, 0, size);
    long endTime = System.nanoTime();
    System.out.println(String.format("%1$064x", new java.math.BigInteger(1, md.digest())));
    System.out.println(String.format("%d ms", (endTime - startTime) / 1000000));
  }

}

Upvotes: 2

Views: 2225

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533492

To use multiple threads you need to be able to perform tasks in parallel.

AFAIK, you can only calculate a message digest processing each block in turn. As such you can only use one thread to calculate each digest value. However, if you can produce multiple values or merge them, you can do it is parallel.

Upvotes: 3

Related Questions