这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions core-java-modules/core-java-security-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,57 @@
<artifactId>bcprov-jdk18on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator-annprocess.version}</version>
</dependency>
</dependencies>

<properties>
<bouncycastle.version>1.76</bouncycastle.version>
<commons-codec.version>1.16.0</commons-codec.version>
<jmh-core.version>1.37</jmh-core.version>
<jmh-generator-annprocess.version>1.37</jmh-generator-annprocess.version>
</properties>

<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>build-classpath</id>
<goals>
<goal>build-classpath</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputProperty>depClasspath</outputProperty>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>BenchmarkRunner</mainClass>
<systemProperties>
<systemProperty>
<key>java.class.path</key>
<value>
${project.build.outputDirectory}${path.separator}${depClasspath}</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}