Example usage for java.security SecureRandom getInstance

List of usage examples for java.security SecureRandom getInstance

Introduction

In this page you can find the example usage for java.security SecureRandom getInstance.

Prototype

public static SecureRandom getInstance(String algorithm, SecureRandomParameters params)
        throws NoSuchAlgorithmException 

Source Link

Document

Returns a SecureRandom object that implements the specified Random Number Generator (RNG) algorithm and supports the specified SecureRandomParameters request.

Usage

From source file:com.zxlim.totp.TOTP.java

public static final String generateSecret() {
    Base32 base32 = new Base32();
    SecureRandom random = null;//w w  w  . ja v a 2 s  . c  om
    final byte[] secret = new byte[secret_size_bits / 8];

    try {
        random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        return null;
    }

    random.nextBytes(secret);

    return new String(base32.encode(secret));
}

From source file:com.sun.syndication.aqueduct.prevayler.uuid.UUIDGen.java

/**
 * Constructor.  Initializes random generator, attempting
 * first to use SecureRandom, then failing over to Random.
 *//*from   ww w  . j a v a 2  s  .  co m*/
public UUIDGen() {
    try {
        random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    } catch (Exception e) {
        random = new Random();
    }
}

From source file:com.intel.cryptostream.JceAesCtrCryptoCodec.java

public JceAesCtrCryptoCodec() {
    provider = CryptoStreamUtils.getJCEProvider();
    final String secureRandomAlg = CryptoStreamUtils.getSecureRandomAlg();
    try {//from w w w. j  a va 2  s . co  m
        random = (provider != null) ? SecureRandom.getInstance(secureRandomAlg, provider)
                : SecureRandom.getInstance(secureRandomAlg);
    } catch (GeneralSecurityException e) {
        LOG.warn(e.getMessage());
        random = new SecureRandom();
    }
}

From source file:org.aon.esolutions.appconfig.client.util.RSAEncryptUtil.java

/**
 * Generate key which contains a pair of privae and public key using 1024 bytes
 * @return key pair/*w  w w.ja v a2s  .c  om*/
 * @throws NoSuchAlgorithmException
 */
public static KeyPair generateKey(String keyPhrase) throws GeneralSecurityException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);

    SecureRandom randomAlg = SecureRandom.getInstance("SHA1PRNG", "SUN");
    randomAlg.setSeed(keyPhrase.getBytes());

    keyGen.initialize(1024, randomAlg);
    KeyPair key = keyGen.generateKeyPair();
    return key;
}

From source file:com.intel.chimera.codec.JceAesCtrCryptoCodec.java

public JceAesCtrCryptoCodec(Properties props) {
    provider = Utils.getJCEProvider(props);
    final String secureRandomAlg = Utils.getSecureRandomAlg(props);
    try {// w w w .ja va 2 s . c o  m
        random = (provider != null) ? SecureRandom.getInstance(secureRandomAlg, provider)
                : SecureRandom.getInstance(secureRandomAlg);
    } catch (GeneralSecurityException e) {
        LOG.warn(e.getMessage());
        random = new SecureRandom();
    }
}

From source file:com.streamsets.lib.security.util.DataSignature.java

public KeyPair generateKeyPair() throws GeneralSecurityException {
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA");
    SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN");
    rng.setSeed(System.currentTimeMillis());
    keyGenerator.initialize(1024, rng);/*from www  . j  a  va 2s.c o m*/
    return keyGenerator.generateKeyPair();
}

From source file:components.security.Password.java

/**
 * Constructor.//from w  w w .j  a v a2  s.c  o m
 */
private Password() {
    try {
        this.random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.panet.imeta.core.util.UUID4Util.java

/**
 * Constructor.  Initializes random generator, attempting
 * first to use SecureRandom, then failing over to Random.
 *///from   w  ww  . j a  v a 2s.  c o  m
public UUID4Util() {
    try {
        random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    } catch (Exception e) {
        random = new Random();
    }
}

From source file:co.cask.cdap.security.tools.KeyStores.java

/**
 * Create a Java key store with a stored self-signed certificate.
 * @return Java keystore which has a self signed X.509 certificate
 *//*from w ww.  jav a  2s .c  o m*/
public static KeyStore generatedCertKeyStore(SConfiguration sConf, String password) {
    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KEY_PAIR_ALGORITHM);
        SecureRandom random = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM, SECURE_RANDOM_PROVIDER);
        keyGen.initialize(KEY_SIZE, random);
        // generate a key pair
        KeyPair pair = keyGen.generateKeyPair();
        int validity = sConf.getInt(Constants.Security.SSL.CERT_VALIDITY, VALIDITY);

        X509Certificate cert = getCertificate(DISTINGUISHED_NAME, pair, validity, SIGNATURE_ALGORITHM);

        KeyStore keyStore = KeyStore.getInstance(SSL_KEYSTORE_TYPE);
        keyStore.load(null, password.toCharArray());
        keyStore.setKeyEntry(CERT_ALIAS, pair.getPrivate(), password.toCharArray(),
                new java.security.cert.Certificate[] { cert });
        return keyStore;
    } catch (Exception e) {
        throw new RuntimeException(
                "SSL is enabled but a key store file could not be created. A keystore is required "
                        + "for SSL to be used.",
                e);
    }
}

From source file:mastermind.RandomGenerator.java

private RandomGenerator() {
    isValid = true;// www .j av  a2s .  c  o m
    try {
        secureRandom = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM, SECURE_RANDOM_PROVIDER);
    } catch (NoSuchAlgorithmException nsle) {
        isValid = false;
    } catch (NoSuchProviderException nspe) {
        isValid = false;
    }
    random = new Random(System.currentTimeMillis());
    seedTimer = new Timer(true); // Daemon-Thread
}