// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.zip.CRC32;

public class MD5vsCRC {
    private final static int L = 10000;
    private final static int REP = 250000;

    public static void main(String[] args) throws NoSuchAlgorithmException {
        Random rng = new Random();
        byte[] b = new byte[L];
        rng.nextBytes(b);
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < REP; i++) {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] v = md.digest(b);
        }
        long t2 = System.currentTimeMillis();
        System.out.println("MD5: " + (t2 - t1) / 1000.0);
        long t3 = System.currentTimeMillis();
        for(int i = 0; i < REP; i++) {
            CRC32 crc = new CRC32();
            crc.update(b);
            long v = crc.getValue();
        }
        long t4 = System.currentTimeMillis();
        System.out.println("CRC: " + (t4 - t3) / 1000.0);
    }
}
