首页 > 编程语言 > 详细

【Java】API SecureRandom 安全随机

时间:2020-10-02 23:04:36      阅读:66      评论:0      收藏:0      [点我收藏+]

之前学习的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 -> Suns Microsoft Crypto API provider

Process finished with exit code 0

获取随机数实例和Random类实例是一样的。

 

【Java】API SecureRandom 安全随机

原文:https://www.cnblogs.com/mindzone/p/13762806.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!