Created
December 4, 2014 05:32
-
-
Save ecki/a4d598c4231a0165d0a1 to your computer and use it in GitHub Desktop.
JMH Test Crypt Hash Performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.eckenfels.test.jmh; | |
import java.nio.ByteBuffer; | |
import java.security.InvalidKeyException; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.concurrent.TimeUnit; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.Mac; | |
import javax.crypto.SecretKey; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Measurement; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.OutputTimeUnit; | |
import org.openjdk.jmh.annotations.Param; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.Warmup; | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.MILLISECONDS) | |
@Warmup(iterations = 1, time = 3, timeUnit = TimeUnit.SECONDS) | |
@Measurement(iterations = 3, time = 6, timeUnit = TimeUnit.SECONDS) | |
@Fork(1) | |
@State(Scope.Thread) | |
public class HashBenchmark | |
{ | |
@Param(value = {"1", "1024", "1048576"}) | |
int bufsize; | |
public ByteBuffer buf; | |
private MessageDigest sha1; | |
private MessageDigest sha256; | |
private MessageDigest sha512; | |
private MessageDigest md5; | |
private SecretKey key; | |
@Setup | |
public void init() | |
throws NoSuchAlgorithmException | |
{ | |
buf = ByteBuffer.allocate(bufsize); | |
sha1 = MessageDigest.getInstance("SHA-1"); | |
sha256 = MessageDigest.getInstance("SHA-256"); | |
sha512 = MessageDigest.getInstance("SHA-512"); | |
md5 = MessageDigest.getInstance("MD5"); | |
KeyGenerator keyGen = KeyGenerator.getInstance("HmacSHA512"); | |
key = keyGen.generateKey(); | |
//System.out.println("key " + key.getFormat()); | |
} | |
@Benchmark | |
public byte[] testMD5() | |
throws NoSuchAlgorithmException | |
{ | |
byte[] arr = buf.array(); | |
MessageDigest digest = MessageDigest.getInstance("MD5"); | |
return digest.digest(arr); | |
} | |
@Benchmark | |
public byte[] testSHA1() | |
throws NoSuchAlgorithmException | |
{ | |
byte[] arr = buf.array(); | |
MessageDigest digest = MessageDigest.getInstance("SHA-1"); | |
return digest.digest(arr); | |
} | |
@Benchmark | |
public byte[] testSHA256() | |
throws NoSuchAlgorithmException | |
{ | |
byte[] arr = buf.array(); | |
MessageDigest digest = MessageDigest.getInstance("SHA-256"); | |
return digest.digest(arr); | |
} | |
@Benchmark | |
public byte[] testSHA512() | |
throws NoSuchAlgorithmException | |
{ | |
byte[] arr = buf.array(); | |
MessageDigest digest = MessageDigest.getInstance("SHA-512"); | |
return digest.digest(arr); | |
} | |
@Benchmark | |
public byte[] testMD5Reuse() | |
{ | |
byte[] arr = buf.array(); | |
MessageDigest digest = md5; | |
return digest.digest(arr); | |
} | |
@Benchmark | |
public byte[] testSHA1Reuse() | |
{ | |
byte[] arr = buf.array(); | |
MessageDigest digest = sha1; | |
return digest.digest(arr); | |
} | |
@Benchmark | |
public byte[] testSHA256Reuse() | |
{ | |
byte[] arr = buf.array(); | |
MessageDigest digest = sha256; | |
return digest.digest(arr); | |
} | |
@Benchmark | |
public byte[] testSHA512Reuse() | |
{ | |
byte[] arr = buf.array(); | |
MessageDigest digest = sha512; | |
return digest.digest(arr); | |
} | |
@Benchmark | |
public byte[] testHmacSHA1() | |
throws NoSuchAlgorithmException, InvalidKeyException | |
{ | |
byte[] arr = buf.array(); | |
Mac mac = Mac.getInstance("HmacSHA1"); | |
mac.init(key); | |
return mac.doFinal(arr); | |
} | |
@Benchmark | |
public byte[] testHmacSHA256() | |
throws NoSuchAlgorithmException, InvalidKeyException | |
{ | |
byte[] arr = buf.array(); | |
Mac mac = Mac.getInstance("HmacSHA256"); | |
mac.init(key); | |
return mac.doFinal(arr); | |
} | |
@Benchmark | |
public byte[] testHmacSHA512() | |
throws NoSuchAlgorithmException, InvalidKeyException | |
{ | |
byte[] arr = buf.array(); | |
Mac mac = Mac.getInstance("HmacSHA512"); | |
mac.init(key); | |
return mac.doFinal(arr); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment