Example usage for java.security SecureRandom SecureRandom

List of usage examples for java.security SecureRandom SecureRandom

Introduction

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

Prototype

public SecureRandom() 

Source Link

Document

Constructs a secure random number generator (RNG) implementing the default random number algorithm.

Usage

From source file:Main.java

public static String randomBytesAsHexString(int byteCount) {
    SecureRandom rng = new SecureRandom();
    byte[] randomData = new byte[byteCount];

    rng.nextBytes(randomData);//  ww  w .  j a  v  a  2s . c o  m

    StringBuilder builder = new StringBuilder();

    for (byte byteValue : randomData) {
        builder.append(String.format("%02x", byteValue));
    }

    return builder.toString();
}

From source file:Main.java

public static byte[] generateSalt() {
    byte[] salt = new byte[8];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);/*from ww w . j av  a2  s . c o  m*/

    return salt;
}

From source file:Main.java

static byte[] generateSalt(int length) {
    byte[] salt = new byte[length];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);// w ww.j  av a  2 s .  c  o m
    return salt;
}

From source file:Main.java

static String passwordGenerator() {
    SecureRandom random = new SecureRandom();
    return new BigInteger(130, random).toString(32);
}

From source file:Main.java

static String getGeneratedUdid() {
    SecureRandom localSecureRandom = new SecureRandom();
    return new BigInteger(64, localSecureRandom).toString(16);
}

From source file:Main.java

public static String getTxRef() {
    String ints = "0123456789";
    SecureRandom rnd = new SecureRandom();
    StringBuilder sb = new StringBuilder("rave-checkout-");
    for (int i = 0; i < 10; i++) {
        sb.append(ints.charAt(rnd.nextInt(ints.length())));
    }/*from  w  w w . j  a  v a 2 s  .  c o  m*/
    return sb.toString();
}

From source file:Main.java

public static byte[] makeSalt() {
    SecureRandom random = new SecureRandom();
    byte[] salt = new byte[SALT_LENGTH / 8];
    random.nextBytes(salt);//  ww w .jav a2 s.com

    return salt;
}

From source file:Main.java

/**
 * Generates a new random app ID. Currently the App id consists of 8
 * hexadecimal digits generated based on the Android SecureRandom class.
 * //from w ww  . j a  v  a 2  s .c  om
 * @return
 */
@SuppressLint("TrulyRandom")
public static String generateAppId() {
    SecureRandom sr = new SecureRandom();
    byte[] random = new byte[4];
    sr.nextBytes(random);
    return String.format("%02x%02x%02x%02x", random[0], random[1], random[2], random[3]);
}

From source file:com.floreantpos.license.FiveStarPOSKeyGenerator.java

public static void createKeys(String publicKeyUri, String privateKeyUri)
        throws NoSuchAlgorithmException, IOException {

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024, new SecureRandom());
    KeyPair keyPair = keyGen.generateKeyPair();

    IOUtils.write(keyPair.getPublic().getEncoded(), new FileOutputStream(publicKeyUri));
    IOUtils.write(keyPair.getPrivate().getEncoded(), new FileOutputStream(privateKeyUri));

}

From source file:Main.java

/**
 * Generates a new symmetric key of a given type.
 * @param type Type of key to generate (algorithm).
 * @return Generated new SecretKey./*from w w  w.ja  v a 2  s  .  co  m*/
 * @throws NoSuchAlgorithmException
 */
public static SecretKey generateSymmetricKey(String type) throws NoSuchAlgorithmException {
    int keySize = 192;
    /* SecureRandom seeded automatically */
    SecureRandom secureRandom = new SecureRandom();
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    //128, 192, 256
    /* This is the correct way to initialize the keygenerator */
    keyGenerator.init(keySize, secureRandom);
    SecretKey key = keyGenerator.generateKey();
    return key;
}