Example usage for java.security SecureRandom nextBytes

List of usage examples for java.security SecureRandom nextBytes

Introduction

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

Prototype

@Override
public void nextBytes(byte[] bytes) 

Source Link

Document

Generates a user-specified number of random bytes.

Usage

From source file:com.mb.framework.util.SecurityUtil.java

/**
 * //from   w ww.jav  a 2 s  . com
 * This method is used for generate slat
 * 
 * @param String
 * @return String
 * @throws Exception
 */
public static String generateSaltAESPBKDF2() {
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[20];
    random.nextBytes(bytes);
    return new String(bytes);
}

From source file:com.bconomy.autobit.Encryption.java

public static void makeRandomKey() {
    SecureRandom random;
    try {// www.j a v  a 2  s  . com
        random = SecureRandom.getInstanceStrong();
    } catch (NoSuchAlgorithmException ex) {
        random = new SecureRandom();
    }
    key = new byte[16];
    random.nextBytes(key);
}

From source file:org.waterforpeople.mapping.app.web.rest.UserRestService.java

static String createRandomKey() {
    SecureRandom secureRandom = new SecureRandom();
    byte bytes[] = new byte[32];
    secureRandom.nextBytes(bytes);
    return Base64.encodeBase64String(bytes).trim();
}

From source file:uk.ac.cam.cl.dtg.segue.auth.SegueLocalAuthenticator.java

/**
 * Helper method to generate a base64 encoded salt.
 * //  www.  ja  v a 2s  . c o  m
 * @return generate a base64 encoded secure salt.
 * @throws NoSuchAlgorithmException
 *             - problem locating the algorithm.
 */
private static String generateSalt() throws NoSuchAlgorithmException {
    SecureRandom sr = SecureRandom.getInstance(SALTING_ALGORITHM);

    byte[] salt = new byte[SALT_SIZE];
    sr.nextBytes(salt);

    return new String(Base64.encodeBase64(salt));
}

From source file:net.tawacentral.roger.secrets.OnlineAgentManager.java

/**
 * Generate a new response key//w w  w . j  ava  2s  .c  o  m
 * 
 * @return String response key
 */
public static String generateResponseKey() {
    SecureRandom random = new SecureRandom();
    byte[] keyBytes = new byte[RESPONSEKEY_LENGTH];
    random.nextBytes(keyBytes);
    return new String(keyBytes);
}

From source file:com.xpn.xwiki.objects.classes.PasswordClass.java

public static String randomSalt() {
    StringBuilder salt = new StringBuilder();
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[32];
    random.nextBytes(bytes);
    for (byte temp : bytes) {
        String s = Integer.toHexString(Byte.valueOf(temp));
        while (s.length() < 2) {
            s = "0" + s;
        }/*from  ww  w.  j  ava 2 s . c o  m*/
        s = s.substring(s.length() - 2);
        salt.append(s);
    }
    return salt.toString();
}

From source file:org.apache.rahas.impl.util.SAMLUtilsTest.java

private static byte[] generateEphemeralKey(int keySize) throws TrustException {
    try {/*from  ww  w.j av a2 s.c om*/
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        byte[] temp = new byte[keySize / 8];
        random.nextBytes(temp);
        return temp;
    } catch (Exception e) {
        throw new TrustException("errorCreatingSymmKey", e);
    }
}

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

public static final String generateSecret() {
    Base32 base32 = new Base32();
    SecureRandom random = null;
    final byte[] secret = new byte[secret_size_bits / 8];

    try {//from  w ww .ja  va 2 s  .  co m
        random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        return null;
    }

    random.nextBytes(secret);

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

From source file:net.jmhertlein.mcanalytics.api.auth.SSLUtil.java

/**
 * Generates a new 64-bit salt with the default CSPRNG
 *
 * @return a 64-bit cryptographic salt/* w  ww  . java 2 s .  c  o m*/
 */
public static byte[] newSalt() {
    SecureRandom gen = new SecureRandom();
    byte[] salt = new byte[8];
    gen.nextBytes(salt);
    return salt;
}

From source file:cz.muni.fi.airportservicelayer.services.StewardServiceImpl.java

private static String createHash(String password) {
    final int SALT_BYTE_SIZE = 24;
    final int HASH_BYTE_SIZE = 24;
    final int PBKDF2_ITERATIONS = 1000;
    // Generate a random salt
    SecureRandom random = new SecureRandom();
    byte[] salt = new byte[SALT_BYTE_SIZE];
    random.nextBytes(salt);
    // Hash the password
    byte[] hash = pbkdf2(password.toCharArray(), salt, PBKDF2_ITERATIONS, HASH_BYTE_SIZE);
    // format iterations:salt:hash
    return PBKDF2_ITERATIONS + ":" + toHex(salt) + ":" + toHex(hash);
}