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.shadows.liquiblq.common.utils.HashManager.java

public static String GenerateStringSalt() {
    SecureRandom RANDOM = new SecureRandom();
    byte[] salt = new byte[16];
    RANDOM.nextBytes(salt);
    return Base64.encodeBase64String(salt);
}

From source file:Main.java

private static byte[] randomBytes(int length) throws Exception {
    SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
    byte[] b = new byte[length];
    random.nextBytes(b);
    return b;//from  w  w w .  j av a2  s . c o m
}

From source file:Main.java

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

    rng.nextBytes(randomData);

    StringBuilder builder = new StringBuilder();

    for (byte byteValue : randomData) {
        builder.append(String.format("%02x", byteValue));
    }//from   w ww  . ja  v a 2s .  co m

    return builder.toString();
}

From source file:de.hybris.platform.b2b.punchout.services.impl.AsymmetricManager.java

/**
 * Generates a random salt.//from  ww  w  .j  av  a 2 s  .  c  o  m
 * 
 * @return The salt.
 */
public static String getSalt() {
    final byte[] salt = new byte[SALT_LEN];
    final SecureRandom rnd = new SecureRandom();
    rnd.nextBytes(salt);
    return Hex.encodeHexString(salt);
}

From source file:Main.java

private static byte[] generateSalt() throws NoSuchAlgorithmException {
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
    byte[] salt = new byte[8];
    random.nextBytes(salt);

    return salt;/*from  w  ww .j ava2 s  .c o  m*/
}

From source file:Main.java

private static byte[] generateSalt() throws NoSuchAlgorithmException {
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
    byte[] salt = new byte[16];
    random.nextBytes(salt);

    return salt;/* ww w .jav  a 2 s . c o m*/
}

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:io.hops.hopsworks.common.dao.user.security.ua.SecurityUtils.java

/**
 * Generate the secrete key for mobile devices.
 *
 * @return//  w  ww.  ja  va 2  s . co m
 */
public static String calculateSecretKey() throws NoSuchAlgorithmException {
    byte[] secretKey = new byte[10];
    SecureRandom sha1Prng = SecureRandom.getInstance("SHA1PRNG");
    sha1Prng.nextBytes(secretKey);
    Base32 codec = new Base32();
    byte[] encodedKey = codec.encode(secretKey);
    return new String(encodedKey);
}

From source file:com.bytecode.util.Crypto.java

private static byte[] encrypt(String keystring, String message, int bits) throws Exception {
    byte[] encValue = null;
    SecureRandom random = new SecureRandom();
    byte[] nonceBytes = new byte[8];
    random.nextBytes(nonceBytes);
    IvParameterSpec nonce = new IvParameterSpec(Arrays.copyOf(nonceBytes, 16));

    Key key = generateKey(keystring, bits);
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key, nonce);
    byte[] ciphertextWithoutNonce = c.doFinal(message.getBytes("UTF-8"));
    encValue = Arrays.copyOf(nonceBytes, nonceBytes.length + ciphertextWithoutNonce.length);
    for (int i = 0; i < ciphertextWithoutNonce.length; i++) {
        encValue[i + 8] = ciphertextWithoutNonce[i];
    }/*  w  ww .  j  av  a2 s  .  c  o m*/

    return encValue;
}

From source file:com.zimbra.cs.service.mail.SendVerificationCode.java

static String generateVerificationCode() {
    ZimbraLog.misc.debug("Generating verification code");
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[3];
    random.nextBytes(bytes);
    return new String(Hex.encodeHex(bytes));
}