Created
September 19, 2018 13:13
-
-
Save jac18281828/df3d9d4f05a527704e99c76b5ba6ad7b to your computer and use it in GitHub Desktop.
Simple Java DSA Signer Performance Check
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 com.covemarkets; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import java.security.InvalidKeyException; | |
import java.security.KeyPair; | |
import java.security.KeyPairGenerator; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.NoSuchProviderException; | |
import java.security.SecureRandom; | |
import java.security.Signature; | |
import java.security.SignatureException; | |
import java.util.Random; | |
public final class DSASigner { | |
private static int KEY_SIZE = 1024; | |
private static int BLOCK_SIZE = 2048; | |
private static int NBLOCK = 20000; | |
private static final Logger log = LogManager.getLogger(DSASigner.class); | |
private final KeyPair keyPair; | |
private final Random r; | |
private Signature dsa; | |
private long nops = 0L; | |
private long signtime = 0L; | |
private long verifytime = 0L; | |
private long totaltime = 0L; | |
private DSASigner() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException { | |
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); | |
SecureRandom random = SecureRandom.getInstance("NativePRNG"); | |
keyGen.initialize(KEY_SIZE, random); | |
keyPair = keyGen.generateKeyPair(); | |
dsa = Signature.getInstance("SHA256withDSA", "SUN"); | |
r = new Random(random.nextInt()); | |
} | |
private void run(final int n) throws InvalidKeyException, SignatureException { | |
final byte[] datablock = new byte[BLOCK_SIZE]; | |
for(int i=0; i<n; i++) { | |
generateRandom(datablock); | |
final long startSign = System.nanoTime(); | |
dsa.initSign(keyPair.getPrivate()); | |
dsa.update(datablock); | |
final byte[] signature = dsa.sign(); | |
signtime += System.nanoTime() - startSign; | |
final long startVerf = System.nanoTime(); | |
dsa.initVerify(keyPair.getPublic()); | |
dsa.update(datablock); | |
if(!dsa.verify(signature)) throw new RuntimeException("Invalid test"); | |
final long endTime = System.nanoTime(); | |
verifytime += endTime - startVerf; | |
totaltime += endTime - startSign; | |
nops++; | |
} | |
log.info("Sign Rate: {}, Verf Rate: {}, Overall Rate: {}", | |
nops*1e9/signtime, | |
nops*1e9/verifytime, | |
nops*1e9/totaltime); | |
log.info("mean sign (us): {}, mean verf (us): {}", | |
signtime/1e3/nops, | |
verifytime/1e3/nops); | |
} | |
private void generateRandom(byte[] datablock) { | |
r.nextBytes(datablock); | |
} | |
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException { | |
log.info("Warming up."); | |
new DSASigner().run(10000); | |
new DSASigner().run(10000); | |
new DSASigner().run(10000); | |
log.info("Running test."); | |
new DSASigner().run(NBLOCK); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment