mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 00:10:25 +08:00
147 lines
6.1 KiB
Java
147 lines
6.1 KiB
Java
package minsecurity.crypto;
|
|
|
|
import minsecurity.identity.IdentityBenchmark;
|
|
import org.openjdk.jmh.annotations.*;
|
|
import org.openjdk.jmh.runner.Runner;
|
|
import org.openjdk.jmh.runner.RunnerException;
|
|
import org.openjdk.jmh.runner.options.Options;
|
|
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
|
|
|
import javax.crypto.BadPaddingException;
|
|
import javax.crypto.IllegalBlockSizeException;
|
|
import javax.crypto.NoSuchPaddingException;
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchProviderException;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import static minsecurity.crypto.TestSM4.SRC_DATA;
|
|
import static minsecurity.crypto.TestSM4.SRC_DATA_16B;
|
|
|
|
@BenchmarkMode(Mode.AverageTime)
|
|
@State(Scope.Thread)
|
|
@Fork(4)
|
|
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
|
@Warmup(iterations = 3)
|
|
@Measurement(iterations = 5)
|
|
public class SM4Benchmark {
|
|
private static byte[] key;
|
|
private static byte[] iv;
|
|
private static byte[] payLoad, payLoad1K, payLoad8K;
|
|
private static byte[] cipherText;
|
|
private static byte[] cipherText2;
|
|
private static byte[] cipherText3;
|
|
private static byte[] cipherText4;
|
|
private static byte[] cipherText5;
|
|
private static byte[] cipherText6;
|
|
|
|
|
|
private static byte[] createPayload(int mode){
|
|
byte[] res = null;
|
|
switch (mode){
|
|
case 0:
|
|
return new byte[]{1};
|
|
case 1:
|
|
res = new byte[1024];
|
|
for (int i = 0; i < 1024 ; i++) {
|
|
res[i] = (byte) (i % 128);
|
|
}
|
|
return res;
|
|
case 8:
|
|
res = new byte[8192];
|
|
for (int i = 0; i < 8192 ; i++) {
|
|
res[i] = (byte) (i % 128);
|
|
}
|
|
return res;
|
|
default:
|
|
return res;
|
|
}
|
|
}
|
|
|
|
static {
|
|
try {
|
|
key = SM4.generateKey();
|
|
iv = SM4.generateKey();
|
|
payLoad = createPayload(0);
|
|
payLoad1K = createPayload(1);
|
|
payLoad8K = createPayload(8);
|
|
cipherText = SM4.encrypt_ECB_Padding(key, payLoad);
|
|
cipherText2 = SM4.encrypt_CBC_Padding(key, iv, payLoad);
|
|
cipherText3 = SM4.encrypt_ECB_Padding(key, payLoad1K);
|
|
cipherText4 = SM4.encrypt_CBC_Padding(key, iv, payLoad1K);
|
|
cipherText5 = SM4.encrypt_ECB_Padding(key, payLoad8K);
|
|
cipherText6 = SM4.encrypt_CBC_Padding(key, iv, payLoad8K);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Benchmark
|
|
public void testCBCEncrypt() throws NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
|
|
SM4.encrypt_CBC_Padding(key, iv, payLoad);
|
|
}
|
|
|
|
@Benchmark
|
|
public void testCBCDecrypt() throws BadPaddingException, NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException {
|
|
SM4.decrypt_CBC_Padding(key, iv, cipherText2);
|
|
}
|
|
|
|
@Benchmark
|
|
public void testECBEncrypt() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
|
|
SM4.encrypt_ECB_Padding(key, payLoad);
|
|
}
|
|
|
|
@Benchmark
|
|
public void testECBDecrypt() throws BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException {
|
|
SM4.decrypt_ECB_Padding(key, cipherText);
|
|
}
|
|
|
|
@Benchmark
|
|
public void testCBCEncrypt1K() throws NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
|
|
SM4.encrypt_CBC_Padding(key, iv, payLoad1K);
|
|
}
|
|
|
|
@Benchmark
|
|
public void testCBCDecrypt1K() throws BadPaddingException, NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException {
|
|
SM4.decrypt_CBC_Padding(key, iv, cipherText4);
|
|
}
|
|
|
|
@Benchmark
|
|
public void testECBEncrypt1K() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
|
|
SM4.encrypt_ECB_Padding(key, payLoad1K);
|
|
}
|
|
|
|
@Benchmark
|
|
public void testECBDecrypt1K() throws BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException {
|
|
SM4.decrypt_ECB_Padding(key, cipherText3);
|
|
}
|
|
|
|
@Benchmark
|
|
public void testCBCEncrypt8K() throws NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
|
|
SM4.encrypt_CBC_Padding(key, iv, payLoad8K);
|
|
}
|
|
|
|
@Benchmark
|
|
public void testCBCDecrypt8K() throws BadPaddingException, NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException {
|
|
SM4.decrypt_CBC_Padding(key, iv, cipherText6);
|
|
}
|
|
|
|
@Benchmark
|
|
public void testECBEncrypt8K() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
|
|
SM4.encrypt_ECB_Padding(key, payLoad8K);
|
|
}
|
|
|
|
@Benchmark
|
|
public void testECBDecrypt8K() throws BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException {
|
|
SM4.decrypt_ECB_Padding(key, cipherText5);
|
|
}
|
|
|
|
public static void main(String[] args) throws RunnerException {
|
|
Options opt = new OptionsBuilder()
|
|
.include(SM4Benchmark.class.getSimpleName())
|
|
.build();
|
|
new Runner(opt).run();
|
|
}
|
|
}
|