diff --git a/core-java-modules/core-java-security-2/pom.xml b/core-java-modules/core-java-security-2/pom.xml index 7ae084a0f975..9ea0b6ef77cd 100644 --- a/core-java-modules/core-java-security-2/pom.xml +++ b/core-java-modules/core-java-security-2/pom.xml @@ -24,11 +24,57 @@ bcprov-jdk18on ${bouncycastle.version} + + org.openjdk.jmh + jmh-core + ${jmh-core.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh-generator-annprocess.version} + 1.76 1.16.0 + 1.37 + 1.37 + + + + maven-dependency-plugin + + + build-classpath + + build-classpath + + + runtime + depClasspath + + + + + + org.codehaus.mojo + exec-maven-plugin + + BenchmarkRunner + + + java.class.path + + ${project.build.outputDirectory}${path.separator}${depClasspath} + + + + + + + \ No newline at end of file diff --git a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/securerandomtester/SecureRandomAvailableAlgorithms.java b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/securerandomtester/SecureRandomAvailableAlgorithms.java new file mode 100644 index 000000000000..72b39437994e --- /dev/null +++ b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/securerandomtester/SecureRandomAvailableAlgorithms.java @@ -0,0 +1,23 @@ +package com.baeldung.securerandomtester; + +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +public class SecureRandomAvailableAlgorithms { + + static String[] algorithmNames = { "NativePRNG", "NativePRNGBlocking", "NativePRNGNonBlocking", "PKCS11", "SHA1PRNG", "Windows-PRNG" }; + + public static void main(String[] args) { + for (int i = 0; i < algorithmNames.length; i++) { + String name = algorithmNames[i]; + Boolean isAvailable = true; + try { + SecureRandom random = SecureRandom.getInstance(name); + } catch (NoSuchAlgorithmException e) { + isAvailable = false; + } + + System.out.println("Algorithm " + name + (isAvailable ? " is" : " isn't") + " available"); + } + } +} diff --git a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/securerandomtester/SecureRandomPerformanceTest.java b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/securerandomtester/SecureRandomPerformanceTest.java new file mode 100644 index 000000000000..2842849ca821 --- /dev/null +++ b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/securerandomtester/SecureRandomPerformanceTest.java @@ -0,0 +1,51 @@ +package com.baeldung.securerandomtester; + +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.util.concurrent.TimeUnit; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Thread) +public class SecureRandomPerformanceTest { + + SecureRandom randomNativePRNGBlocking; + SecureRandom randomNativePRNGNonBlocking; + + final int NBYTES = 256; + final int NSAMPLES = 20_000; + + @Setup(Level.Trial) + public void setup() throws NoSuchAlgorithmException { + randomNativePRNGBlocking = SecureRandom.getInstance("NativePRNGBlocking"); + randomNativePRNGNonBlocking = SecureRandom.getInstance("NativePRNGNonBlocking"); + } + + @Benchmark + public void measureTimePRNGBlocking() { + byte[] randomBytes = new byte[NBYTES]; + for (int i = 0; i < NSAMPLES; i++) { + randomNativePRNGBlocking.nextBytes(randomBytes); + } + } + + @Benchmark + public void measureTimePRNGNonBlocking() { + byte[] randomBytes = new byte[NBYTES]; + for (int i = 0; i < NSAMPLES; i++) { + randomNativePRNGNonBlocking.nextBytes(randomBytes); + } + } + + public static void main(String[] args) throws Exception { + org.openjdk.jmh.Main.main(args); + } +}