本文整理汇总了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");
}
}
示例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);
}
}
示例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;
}
示例4: getSalt
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* 获取加密算法中使用的盐值,解密中使用的盐值必须与加密中使用的相同才能完成操作.
* 盐长度必须为8字节
*
* @return byte[] 盐值
*/
public static byte[] getSalt() throws Exception {
//实例化安全随机数
SecureRandom random = new SecureRandom();
//产出盐
return random.generateSeed(8);
}
示例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");
}
}
}
示例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);
}
}
示例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);
}