本文整理汇总了Java中java.security.SecureRandom.getInstance方法的典型用法代码示例。如果您正苦于以下问题:Java SecureRandom.getInstance方法的具体用法?Java SecureRandom.getInstance怎么用?Java SecureRandom.getInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.SecureRandom
的用法示例。
在下文中一共展示了SecureRandom.getInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: buildArtifact
import java.security.SecureRandom; //导入方法依赖的package包/类
/** {@inheritDoc} */
public SAML1ArtifactType0001 buildArtifact(
SAMLMessageContext<RequestAbstractType, Response, NameIdentifier> requestContext, Assertion assertion) {
try {
MessageDigest sha1Digester = MessageDigest.getInstance("SHA-1");
byte[] source = sha1Digester.digest(requestContext.getLocalEntityId().getBytes());
SecureRandom handleGenerator = SecureRandom.getInstance("SHA1PRNG");
byte[] assertionHandle = new byte[20];
handleGenerator.nextBytes(assertionHandle);
return new SAML1ArtifactType0001(source, assertionHandle);
} catch (NoSuchAlgorithmException e) {
log.error("JVM does not support required cryptography algorithms.", e);
throw new InternalError("JVM does not support required cryptography algorithms: SHA-1 and/or SHA1PRNG.");
}
}
示例3: getCipher
import java.security.SecureRandom; //导入方法依赖的package包/类
public static Cipher getCipher(int mode, byte[] secret) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException {
// generate an encryption/decryption key from random data seeded with
// our secret (i.e. password)
SecureRandom secureRandom = SecureRandom.getInstance(RANDOM_ALGORITHM);
secureRandom.setSeed(secret);
KeyGenerator keyGenerator = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM);
keyGenerator.init(KEY_SIZE, secureRandom);
Key key = new SecretKeySpec(keyGenerator.generateKey().getEncoded(), ENCRYPTION_ALGORITHM);
// get a cipher based on the specified encryption algorithm
Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
// tell the cipher if it will be used for encryption or decryption
// (i.e. cipher mode) and give it our key
cipher.init(mode, key);
return cipher;
}
示例4: getSecretKey
import java.security.SecureRandom; //导入方法依赖的package包/类
public static SecretKeySpec getSecretKey(String secretKey) throws Exception
{
KeyGenerator kgen = KeyGenerator.getInstance(ENCRYPT_TYPE);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(secretKey.getBytes());
kgen.init(AES_DEFAULT_KEY_SIZE, random);
SecretKey sk = kgen.generateKey();
byte[] enCodeFormat = sk.getEncoded();
return new SecretKeySpec(enCodeFormat, ENCRYPT_TYPE);
}
示例5: generateKey
import java.security.SecureRandom; //导入方法依赖的package包/类
public static byte[] generateKey( byte[] seed ) throws Exception
{
KeyGenerator keyGenerator = KeyGenerator.getInstance( CIPHER_ALGORITHM );
SecureRandom secureRandom = SecureRandom.getInstance( RANDOM_GENERATOR_ALGORITHM );
secureRandom.setSeed( seed );
keyGenerator.init( RANDOM_KEY_SIZE, secureRandom );
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
}
示例6: createRngInternal
import java.security.SecureRandom; //导入方法依赖的package包/类
private static RandomWrapper createRngInternal() {
try {
return new RandomWrapper(SecureRandom.getInstance("SHA1PRNG"));
} catch (final NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
示例7: main
import java.security.SecureRandom; //导入方法依赖的package包/类
public static void main(String[] args) {
System.setProperty("java.security.egd", "file:/dev/urandom");
boolean success = true;
for (String mech : new String[]{
"SHA1PRNG", "Hash_DRBG", "HMAC_DRBG", "CTR_DRBG"}) {
System.out.printf("%nTest for SecureRandom algorithm: '%s'", mech);
try {
SecureRandom sr = null;
if (!mech.contains("_DRBG")) {
sr = SecureRandom.getInstance(mech);
} else {
Security.setProperty(DRBG_CONFIG, mech);
sr = SecureRandom.getInstance("DRBG");
}
success &= forEachSeedBytes(sr);
System.out.printf("%nCompleted test for SecureRandom "
+ "mechanism: '%s'", mech);
} catch (Exception e) {
success &= false;
e.printStackTrace(System.out);
} finally {
Security.setProperty(DRBG_CONFIG, DRBG_CONFIG_VALUE);
}
}
if (!success) {
throw new RuntimeException("At least one test failed.");
}
}
示例8: testWolfSignInteropVerify
import java.security.SecureRandom; //导入方法依赖的package包/类
@Test
public void testWolfSignInteropVerify()
throws NoSuchProviderException, NoSuchAlgorithmException,
SignatureException, InvalidKeyException,
InvalidAlgorithmParameterException {
String toSign = "Hello World";
byte[] toSignBuf = toSign.getBytes();
byte[] signature;
for (int i = 0; i < wolfJCEAlgos.length; i++) {
Signature signer =
Signature.getInstance(wolfJCEAlgos[i], "wolfJCE");
Signature verifier =
Signature.getInstance(wolfJCEAlgos[i]);
assertNotNull(signer);
assertNotNull(verifier);
Provider prov = verifier.getProvider();
if (prov.equals("wolfJCE")) {
/* bail out, there isn't another implementation to interop
* against by default */
return;
}
SecureRandom rand =
SecureRandom.getInstance("HashDRBG", "wolfJCE");
assertNotNull(rand);
/* generate key pair */
KeyPair pair = generateKeyPair(wolfJCEAlgos[i], rand);
assertNotNull(pair);
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
/* generate signature */
signer.initSign(priv);
signer.update(toSignBuf, 0, toSignBuf.length);
signature = signer.sign();
/* verify signature */
verifier.initVerify(pub);
verifier.update(toSignBuf, 0, toSignBuf.length);
boolean verified = verifier.verify(signature);
if (verified != true) {
fail("Signature verification failed when generating with " +
"wolfJCE and verifying with system default JCE " +
"provider");
}
}
}
示例9: generateRandomBytes
import java.security.SecureRandom; //导入方法依赖的package包/类
/** {@inheritDoc} */
@Override
public byte[] generateRandomBytes(final int numBytes) throws IOException {
final SecureRandom sr;
try {
sr = SecureRandom.getInstance("SHA1PRNG"); //$NON-NLS-1$
}
catch (final NoSuchAlgorithmException e) {
throw new IOException("Algoritmo de generacion de aleatorios no valido: " + e, e); //$NON-NLS-1$
}
final byte[] randomBytes = new byte[numBytes];
sr.nextBytes(randomBytes);
return randomBytes;
}
示例10: getSecureRandom
import java.security.SecureRandom; //导入方法依赖的package包/类
private static SecureRandom getSecureRandom() {
try {
return SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
}
示例11: generateSalt
import java.security.SecureRandom; //导入方法依赖的package包/类
public String generateSalt() throws NoSuchAlgorithmException {
// VERY important to use SecureRandom instead of just Random
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
// Generate a 8 byte (64 bit) salt as recommended by RSA PKCS5
byte[] salt = new byte[8];
random.nextBytes(salt);
return Base64.getEncoder().encodeToString(salt);
}
示例12: encrypt
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* 加密
*
* @param content �?要加密的内容
* @return
*/
public static String encrypt(String content) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance(AES);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(KEY.getBytes());
kgen.init(AES_LENGTH, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);
Cipher cipher = Cipher.getInstance(AES);// 创建密码�?
byte[] byteContent = content.getBytes(ENCODING_UTF8);
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始�?
byte[] result = cipher.doFinal(byteContent);
return new BASE64Encoder().encode(result); // 加密
}
示例13: installLinuxPRNGSecureRandom
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* Installs a Linux PRNG-backed {@code SecureRandom} implementation as the
* default. Does nothing if the implementation is already the default or if
* there is not need to install the implementation.
*
* @throws SecurityException if the fix is needed but could not be applied.
*/
private static void installLinuxPRNGSecureRandom()
throws SecurityException {
if (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2) {
// No need to apply the fix
return;
}
// Install a Linux PRNG-based SecureRandom implementation as the
// default, if not yet installed.
Provider[] secureRandomProviders =
Security.getProviders("SecureRandom.SHA1PRNG");
if ((secureRandomProviders == null)
|| (secureRandomProviders.length < 1)
|| (!LinuxPRNGSecureRandomProvider.class.equals(
secureRandomProviders[0].getClass()))) {
Security.insertProviderAt(new LinuxPRNGSecureRandomProvider(), 1);
}
// Assert that new SecureRandom() and
// SecureRandom.getInstance("SHA1PRNG") return a SecureRandom backed
// by the Linux PRNG-based SecureRandom implementation.
SecureRandom rng1 = new SecureRandom();
if (!LinuxPRNGSecureRandomProvider.class.equals(
rng1.getProvider().getClass())) {
throw new SecurityException(
"new SecureRandom() backed by wrong Provider: "
+ rng1.getProvider().getClass());
}
SecureRandom rng2;
try {
rng2 = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
throw new SecurityException("SHA1PRNG not available", e);
}
if (!LinuxPRNGSecureRandomProvider.class.equals(
rng2.getProvider().getClass())) {
throw new SecurityException(
"SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong"
+ " Provider: " + rng2.getProvider().getClass());
}
}
示例14: generateNonce
import java.security.SecureRandom; //导入方法依赖的package包/类
static String generateNonce() throws NoSuchAlgorithmException, UnsupportedEncodingException {
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(System.currentTimeMillis());
byte[] nonceBytes = new byte[16];
random.nextBytes(nonceBytes);
String nonce = new String(Base64.getEncoder().encode(nonceBytes), "UTF-8");
return nonce;
}
示例15: installLinuxPRNGSecureRandom
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* Installs a Linux PRNG-backed {@code SecureRandom} implementation as
* the default. Does nothing if the implementation is already the
* default or if there is not need to install the implementation.
*
* @throws SecurityException if the fix is needed but could not be
* applied.
*/
private static void installLinuxPRNGSecureRandom() throws SecurityException {
if (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2) {
// No need to apply the fix
return;
}
// Install a Linux PRNG-based SecureRandom implementation as the
// default, if not yet installed.
Provider[] secureRandomProviders = Security.getProviders("SecureRandom.SHA1PRNG");
// Insert and check the provider atomically.
// The official Android Java libraries use synchronized methods for
// insertProviderAt, etc., so synchronizing on the class should
// make things more stable, and prevent race conditions with other
// versions of this code.
synchronized (java.security.Security.class) {
if ((secureRandomProviders == null)
|| (secureRandomProviders.length < 1)
|| (!secureRandomProviders[0].getClass().getSimpleName().equals("LinuxPRNGSecureRandomProvider"))) {
Security.insertProviderAt(new LinuxPRNGSecureRandomProvider(), 1);
}
// Assert that new SecureRandom() and
// SecureRandom.getInstance("SHA1PRNG") return a SecureRandom backed
// by the Linux PRNG-based SecureRandom implementation.
SecureRandom rng1 = new SecureRandom();
if (!rng1.getProvider().getClass().getSimpleName().equals("LinuxPRNGSecureRandomProvider")) {
if (ALLOW_BROKEN_PRNG) {
Log.w(PrngFixes.class.getSimpleName(),
"new SecureRandom() backed by wrong Provider: " + rng1.getProvider().getClass());
return;
} else {
throw new SecurityException("new SecureRandom() backed by wrong Provider: "
+ rng1.getProvider().getClass());
}
}
SecureRandom rng2 = null;
try {
rng2 = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
if (ALLOW_BROKEN_PRNG) {
Log.w(PrngFixes.class.getSimpleName(), "SHA1PRNG not available", e);
return;
} else {
new SecurityException("SHA1PRNG not available", e);
}
}
if (!rng2.getProvider().getClass().getSimpleName().equals("LinuxPRNGSecureRandomProvider")) {
if (ALLOW_BROKEN_PRNG) {
Log.w(PrngFixes.class.getSimpleName(),
"SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong" + " Provider: "
+ rng2.getProvider().getClass());
return;
} else {
throw new SecurityException(
"SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong" + " Provider: "
+ rng2.getProvider().getClass());
}
}
}
}