Skip to content

Instantly share code, notes, and snippets.

@patrikbeno
Last active August 13, 2018 09:47
Show Gist options
  • Save patrikbeno/b7bf35c773b15e7ad07456a5abb36cd2 to your computer and use it in GitHub Desktop.
Save patrikbeno/b7bf35c773b15e7ad07456a5abb36cd2 to your computer and use it in GitHub Desktop.
WindowsPRNG
package test;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Optional;
public interface WindowsPRNG {
static void init() {
String provider = "SunMSCAPI"; // original provider
String type = "SecureRandom"; // service type
String alg = "Windows-PRNG"; // algorithm
String name = String.format("%s.%s", provider, type); // our provider name
if (Security.getProvider(name) != null) return; // already registered
Optional.ofNullable(Security.getProvider(provider)) // only on Windows
.ifPresent(p -> Optional.ofNullable(p.getService(type, alg)) // should exist but who knows?
.ifPresent(svc -> Security.insertProviderAt(
// insert our provider with single SecureRandom service
new Provider(name, p.getVersionStr(), null) {{
setProperty(String.format("%s.%s", type, alg), svc.getClassName());
}}, 1)));
}
static void main(String[] args) {
long started = System.currentTimeMillis();
WindowsPRNG.init();
new SecureRandom().generateSeed(20);
System.out.println(System.currentTimeMillis() - started);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment