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:Main.java

private static byte[] getRawKey(byte[] seed) throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = null;//  w  w  w  .ja  va2s.co m
    if (android.os.Build.VERSION.SDK_INT >= 17) {
        sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    } else {
        sr = SecureRandom.getInstance("SHA1PRNG");
    }
    sr.setSeed(seed);
    kgen.init(128, sr); //256 bits or 128 bits,192bits  
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    return raw;
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kGen = KeyGenerator.getInstance("AES");
    SecureRandom sr;/* www .  java  2  s .c  o m*/
    if (android.os.Build.VERSION.SDK_INT >= 17) {
        sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    } else {
        sr = SecureRandom.getInstance("SHA1PRNG");
    }
    sr.setSeed(seed);
    kGen.init(128, sr);
    SecretKey sKey = kGen.generateKey();
    return sKey.getEncoded();
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    //  SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    sr.setSeed(seed);/*  w  ww.j  av  a 2 s .c  o m*/
    kgen.init(128, sr); // 192 and 256 bits may not be available
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    return raw;
}

From source file:MainClass.java

public KeyPair generateKeyPair(long seed) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA");
    SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN");
    rng.setSeed(seed);/* ww w .ja  v a  2 s  . c  om*/
    keyGenerator.initialize(1024, rng);

    return (keyGenerator.generateKeyPair());
}

From source file:org.apache.accumulo.core.crypto.CryptoUtils.java

private static SecureRandom newSecureRandom(String secureRNG, String secureRNGProvider) {
    SecureRandom secureRandom = null;
    try {//from  www  .  j a  v a  2s.c o m
        secureRandom = SecureRandom.getInstance(secureRNG, secureRNGProvider);

        // Immediately seed the generator
        byte[] throwAway = new byte[16];
        secureRandom.nextBytes(throwAway);
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        throw new CryptoException("Unable to generate secure random.", e);
    }
    return secureRandom;
}

From source file:MainClass.java

public static KeyPair generateKeyPair(long seed) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA");
    SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN");
    rng.setSeed(seed);/*www .  j  av  a  2  s.  co  m*/
    keyGenerator.initialize(1024, rng);

    return (keyGenerator.generateKeyPair());
}

From source file:de.dominikschadow.javasecurity.csrf.CSRFTokenHandler.java

private static String getToken() throws NoSuchAlgorithmException, NoSuchProviderException {
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "SUN");
    sr.nextBytes(new byte[20]);
    return String.valueOf(sr.nextLong());
}

From source file:nu.yona.server.crypto.CryptoUtil.java

public static SecureRandom getSecureRandomInstance() {
    try {/*from w ww.  j a  v a 2s  .  c o m*/
        return SecureRandom.getInstance("SHA1PRNG", "SUN");
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        throw CryptoException.gettingRandomInstance(e);
    }
}

From source file:pl.kotcrab.crypto.CryptoUtils.java

/** Constructs and returns secure random. Algorithm depends on operating system, on Windows SHA1PRNG from Sun will be used and on
 * Unix and Mac systems NativePRNG will be used.
 * @return SecureRandom instance */
public static SecureRandom getSecureRandom() {
    try {/*from   w w  w  .j  a va2  s .c o  m*/
        if (OSDetector.isUnix() || OSDetector.isMac())
            return SecureRandom.getInstance("NativePRNG", "SUN");
        else
            return SecureRandom.getInstance("SHA1PRNG", "SUN");
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.github.achatain.nopasswordauthentication.utils.TokenService.java

public String generate() {
    SecureRandom sr;//from   ww w . ja v a2  s. co  m

    try {
        sr = SecureRandom.getInstance("SHA1PRNG", "SUN");
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        LOG.log(Level.SEVERE, "Failed to initiate a secure random", e);
        throw new RuntimeException("Unable to generate an API token", e);
    }

    byte bytes[] = new byte[16];
    sr.nextBytes(bytes);

    return RandomStringUtils.random(64, 0, 0, true, true, null, sr);
}