【Java】API SecureRandom 安全随机
之前学习的Random工具类只是一个伪随机数类。。。
@Test public void secureRandom() throws Exception { // 个不可预测的安全的随机数 // 无法指定种子,它使用RNG(random number generator)算法 // 实际上有多种不同的底层实现,有的使用安全随机种子加上伪随机数算法来产生安 // SecureRandom常用工具类构建全的随机数,有的使用真正的随机数生成器。实际使用的时候,可以优先获取高强度的安全随机数生成器,如果没有提供,再使用普通等级的安全随机数生成器 // 实例获取 SecureRandom secureRandom = new SecureRandom(); // jdk8 获取更安全的实例 SecureRandom instanceStrong = SecureRandom.getInstanceStrong(); String algorithm = secureRandom.getAlgorithm(); Provider provider = secureRandom.getProvider(); String providerInfo = provider.getInfo(); System.out.println("normalInstance algorithm -> " + algorithm + "\nprovider -> " + providerInfo); System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); algorithm = instanceStrong.getAlgorithm(); provider = instanceStrong.getProvider(); providerInfo = provider.getInfo(); System.out.println("strongInstance algorithm -> " + algorithm + "\nprovider -> " + providerInfo); }
打印结果:
normalInstance algorithm -> SHA1PRNG provider -> SUN (DSA key/parameter generation; DSA signing; SHA-1, MD5 digests; SecureRandom; X.509 certificates; JKS & DKS keystores; PKIX CertPathValidator; PKIX CertPathBuilder; LDAP, Collection CertStores, JavaPolicy Policy; JavaLoginConfig Configuration) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - strongInstance algorithm -> Windows-PRNG provider -> Sun's Microsoft Crypto API provider Process finished with exit code 0
获取随机数实例和Random类实例是一样的。