當前位置: 首頁>>代碼示例>>Java>>正文


Java SecureRandom.generateSeed方法代碼示例

本文整理匯總了Java中java.security.SecureRandom.generateSeed方法的典型用法代碼示例。如果您正苦於以下問題:Java SecureRandom.generateSeed方法的具體用法?Java SecureRandom.generateSeed怎麽用?Java SecureRandom.generateSeed使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.security.SecureRandom的用法示例。


在下文中一共展示了SecureRandom.generateSeed方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: testGenerateSeed

import java.security.SecureRandom; //導入方法依賴的package包/類
@Test
public void testGenerateSeed()
    throws NoSuchProviderException, NoSuchAlgorithmException {

    byte[] valuesA = new byte[128];
    byte[] valuesB = new byte[128];

    SecureRandom rand = SecureRandom.getInstance("HashDRBG", "wolfJCE");

    valuesA = rand.generateSeed(valuesA.length);
    for (int i = 0; i < 10; i++) {
        valuesB = rand.generateSeed(valuesB.length);

        if(Arrays.equals(valuesA, valuesB))
            fail("SecureRandom generated two equal consecutive arrays");

        valuesA = Arrays.copyOf(valuesB, valuesB.length);
    }
}
 
開發者ID:wolfSSL,項目名稱:wolfcrypt-jni,代碼行數:20,代碼來源:WolfCryptRandomTest.java

示例3: forEachSeedBytes

import java.security.SecureRandom; //導入方法依賴的package包/類
/**
 * Generates fixed number of seed bytes through a SecureRandom instance
 * to verify it's seed generation status.
 * @param sr SecureRandom instance
 * @return The test success indicator
 */
private static boolean forEachSeedBytes(SecureRandom sr) {
    boolean success = true;
    sr.setSeed(1l);
    for (int seedByte : new int[]{Integer.MIN_VALUE, -1, 0, 1, 256, 1024,
        Short.MAX_VALUE, (int) pow(2, 20)}) {
        try {
            byte[] seed = sr.generateSeed(seedByte);
            if (seed.length != seedByte) {
                throw new RuntimeException("Not able to produce expected "
                        + "seed size.");
            }
        } catch (IllegalArgumentException e) {
            if (seedByte >= 0) {
                throw new RuntimeException("Unknown Exception occured.", e);
            }
            System.out.printf("%nPASS - Exception expected when required "
                    + "seed size requested is negative: %s", seedByte);
        }
    }
    return success;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:28,代碼來源:EnoughSeedTest.java

示例4: getSalt

import java.security.SecureRandom; //導入方法依賴的package包/類
/**
 * 獲取加密算法中使用的鹽值,解密中使用的鹽值必須與加密中使用的相同才能完成操作.
 * 鹽長度必須為8字節
 *
 * @return byte[] 鹽值
 */
public static byte[] getSalt() throws Exception {
    //實例化安全隨機數
    SecureRandom random = new SecureRandom();
    //產出鹽
    return random.generateSeed(8);
}
 
開發者ID:zhonglikui,項目名稱:cardinalsSample,代碼行數:13,代碼來源:EncryptByPbe.java

示例5: testSeed

import java.security.SecureRandom; //導入方法依賴的package包/類
private static void testSeed(String alg) throws Exception {
    System.out.println("Testing: " + alg);
    SecureRandom sr = SecureRandom.getInstance(alg);
    byte[] ba = sr.generateSeed(20);

    // We should get back a bunch of zeros from the file.
    for (byte b : ba) {
        if (b != 0) {
            throw new Exception("Byte != 0");
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:13,代碼來源:StrongSeedReader.java

示例6: main

import java.security.SecureRandom; //導入方法依賴的package包/類
public static void main(String... arguments) throws Exception {
    for (String mech : new String[]{"SHA1PRNG", "Hash_DRBG", "HMAC_DRBG",
        "CTR_DRBG"}) {

        SecureRandom prng = null;
        if (!mech.contains("_DRBG")) {
            prng = SecureRandom.getInstance(mech);
        } else {
            Security.setProperty("securerandom.drbg.config", mech);
            prng = SecureRandom.getInstance("DRBG");
        }
        prng.generateSeed(1);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:15,代碼來源:SeedGeneratorChoice.java

示例7: main

import java.security.SecureRandom; //導入方法依賴的package包/類
public static void main(String... arguments) throws Exception {
    byte[] bytes;
    SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
    bytes = prng.generateSeed(1);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:6,代碼來源:SeedGeneratorChoice.java


注:本文中的java.security.SecureRandom.generateSeed方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。