当前位置: 首页>>代码示例>>Java>>正文


Java SecureRandom.reseed方法代码示例

本文整理汇总了Java中java.security.SecureRandom.reseed方法的典型用法代码示例。如果您正苦于以下问题:Java SecureRandom.reseed方法的具体用法?Java SecureRandom.reseed怎么用?Java SecureRandom.reseed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.security.SecureRandom的用法示例。


在下文中一共展示了SecureRandom.reseed方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import java.security.SecureRandom; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    SecureRandom sr;
    boolean pass = true;
    for (String mech : new String[]{"Hash_DRBG", "HMAC_DRBG", "CTR_DRBG"}) {
        try {
            System.out.println("Testing " + mech + "...");
            Security.setProperty("securerandom.drbg.config", mech);

            // Check auto reseed works
            sr = SecureRandom.getInstance("DRBG");
            sr.nextInt();
            sr = SecureRandom.getInstance("DRBG");
            sr.reseed();
            sr = SecureRandom.getInstance("DRBG");
            sr.generateSeed(10);
        } catch (Exception e) {
            pass = false;
            e.printStackTrace(System.out);
        }
    }
    if (!pass) {
        throw new RuntimeException("At least one test case failed");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:AutoReseed.java

示例2: test

import java.security.SecureRandom; //导入方法依赖的package包/类
public static void test(SecureRandom sr, int tnum, int rnum)
        throws Exception {

    System.out.println(sr);
    System.out.println(sr.getAlgorithm() + " " + sr.getProvider().getName());

    System.out.println(new Date());
    boolean reseed = sr.getParameters() != null;
    Thread[] threads = new Thread[tnum];
    AtomicBoolean failed = new AtomicBoolean(false);
    Thread.UncaughtExceptionHandler h = (t, e) -> {
        failed.set(true);
        e.printStackTrace();
    };
    for (int i = 0; i < threads.length; i++) {
        threads[i] = new Thread() {
            @Override
            public void run() {
                for (int j = 0; j < rnum; j++) {
                    sr.nextBytes(new byte[j%100+100]);
                    sr.setSeed((long)j);
                    if (reseed) {
                        sr.reseed();
                    }
                }
            }
        };
        threads[i].setUncaughtExceptionHandler(h);
        threads[i].start();
    }
    for (int i = 0; i < threads.length; i++) {
        threads[i].join();
    }
    System.out.println(new Date());
    System.out.println();
    if (failed.get()) {
        throw new RuntimeException("Failed");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:40,代码来源:NoSync.java

示例3: main

import java.security.SecureRandom; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {

        byte[] result = new byte[10];
        MyES es = new MyES();

        // Set es as the default entropy source, overriding SeedGenerator.
        setDefaultSeeder(es);

        // Nothing happened yet
        es.checkUsage(0);

        SecureRandom sr;
        sr = SecureRandom.getInstance("DRBG");

        // No entropy reading if only getInstance
        es.checkUsage(0);

        // Entropy is read at 1st nextBytes of the 1st DRBG
        sr.nextInt();
        es.checkUsage(1);

        for (String mech : new String[]{"Hash_DRBG", "HMAC_DRBG", "CTR_DRBG"}) {
            System.out.println("Testing " + mech + "...");

            // DRBG with pr_false will never read entropy again no matter
            // if nextBytes or reseed is called.

            Security.setProperty("securerandom.drbg.config", mech);
            sr = SecureRandom.getInstance("DRBG");
            sr.nextInt();
            sr.reseed();
            es.checkUsage(0);

            // DRBG with pr_true always read from default entropy, and
            // its nextBytes always reseed itself

            Security.setProperty("securerandom.drbg.config",
                    mech + ",pr_and_reseed");
            sr = SecureRandom.getInstance("DRBG");

            sr.nextInt();
            es.checkUsage(2); // one instantiate, one reseed
            sr.nextInt();
            es.checkUsage(1); // one reseed in nextBytes
            sr.reseed();
            es.checkUsage(1); // one reseed
            sr.nextBytes(result, DrbgParameters.nextBytes(-1, false, null));
            es.checkUsage(0); // pr_false for this call
            sr.nextBytes(result, DrbgParameters.nextBytes(-1, true, null));
            es.checkUsage(1); // pr_true for this call
            sr.reseed(DrbgParameters.reseed(true, null));
            es.checkUsage(1); // reseed from es
            sr.reseed(DrbgParameters.reseed(false, null));
            es.checkUsage(0); // reseed from AbstractDrbg.SeederHolder.seeder
        }
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:57,代码来源:CommonSeeder.java


注:本文中的java.security.SecureRandom.reseed方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。