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.platform.common.utils.security.NonceUtils.java

/**
 * SecureRandom?, Hex?./*from ww w.  ja  va  2s  . co m*/
 * 
 * @param length ,?.
 */
public static String randomHexString(int length) {
    SecureRandom nonceGenerator = new SecureRandom();
    byte[] nonce = new byte[length / 2];
    nonceGenerator.nextBytes(nonce);
    return Hex.encodeHexString(nonce);
}

From source file:com.sonicle.webtop.core.util.IdentifierUtils.java

/**
 * @deprecated use com.sonicle.commons.IdentifierUtils.getCRSFToken instead
 * @return//  w  w w.jav a 2s. c  o  m
 */
@Deprecated
public static synchronized String getCRSFToken() {
    try {
        byte[] buffer = new byte[80 / 8];
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        sr.nextBytes(buffer);
        byte[] secretKey = Arrays.copyOf(buffer, 80 / 8);
        byte[] encodedKey = new Base32().encode(secretKey);
        return new String(encodedKey).toLowerCase();
    } catch (NoSuchAlgorithmException ex) {
        return null;
    }
}

From source file:com.sonicle.webtop.core.util.IdentifierUtils.java

/**
 * @deprecated use com.sonicle.commons.IdentifierUtils.generateSecretKey instead
 * @return// w w w  .j a va 2 s .c  o m
 */
@Deprecated
public static synchronized String generateSecretKey() {
    try {
        byte[] buffer = new byte[80 / 8];
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        sr.nextBytes(buffer);
        byte[] secretKey = Arrays.copyOf(buffer, 80 / 8);
        byte[] encodedKey = new Base32().encode(secretKey);
        return new String(encodedKey).toLowerCase();
    } catch (NoSuchAlgorithmException ex) {
        return null;
    }
}

From source file:bridgempp.PermissionsManager.java

public static String generateKey(int permissions, boolean useOnce) {
    SecureRandom random = new SecureRandom();
    byte[] byteKey = new byte[32];
    random.nextBytes(byteKey);
    String key = Base64.getEncoder().encodeToString(byteKey);
    accessKeys.put(key, new AccessKey(key, permissions, useOnce));
    return key;//w ww .java 2  s . c  om
}

From source file:com.networknt.utility.HashUtil.java

private static String getSalt() throws NoSuchAlgorithmException {
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    byte[] salt = new byte[16];
    sr.nextBytes(salt);
    return Arrays.toString(salt);
}

From source file:org.apache.cloudstack.server.auth.PBKDF2UserAuthenticator.java

public static byte[] makeSalt() throws NoSuchAlgorithmException {
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    byte[] salt = new byte[s_saltlen];
    sr.nextBytes(salt);
    return salt;/* w  w w.j  a  va2s.  com*/
}

From source file:org.appcelerator.util.SecurityUtil.java

/**
 * generate a cryptographically secure random seed based on using
 * the SHA1PRNG secure random number. it uses a double seeded
 * secure generation using 2 1024-bit random generators and then
 * hashing with a SHA-256 hash./* w  w  w . j a  va  2 s . c o  m*/
 *
 * @return seed
 * @throws Exception upon error
 */
public static String generateSecureRandomSeed() throws Exception {
    SecureRandom r2 = SecureRandom.getInstance("SHA1PRNG");
    byte buf[] = new byte[1024 / 8];
    byte buf2[] = new byte[1024 / 8];
    random.nextBytes(buf2);
    r2.nextBytes(buf);
    byte buf3[] = digest512.digest((Util.toHexString(buf) + Util.toHexString(buf2)).getBytes());
    return Util.toHexString(buf3);
}

From source file:org.linagora.linshare.core.utils.SymmetricEnciphermentPBEwithAES.java

/**
 * To create the bytes for the initialization vector IV, we should use java.security.SecureRandom to generate
 * a byte array equivalent to the block size for the cipher we are using. Most block ciphers have a block size of 64 bits
 *(8 bytes). AES has a variable block size, either 128, 192, or 256 bits, but is typically set to 128-bit (16 bytes).
 *//*  w w  w  .ja  v  a  2 s. co m*/
private static byte[] generateSalt() {
    byte[] salt = new byte[SALT_NUMBER_BITES];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);
    return salt;
}

From source file:com.cl.roadshow.crypto.AESCtr.java

/**
 * Private encryption method./*from w  w w .ja  va 2 s  . co m*/
 * 
 * @param keystring
 * @param message
 * @param bits
 * @return bytearray containing encrypted message
 * @throws Exception
 */
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];
    }

    return encValue;
}

From source file:my.adam.smo.ServerCorrectnessTest.java

public static byte[] getMegaBytes(int amount) {
    SecureRandom sr = new SecureRandom();
    byte[] out = new byte[1024 * amount];
    sr.nextBytes(out);
    return out;//from w ww.j  av  a2  s.  co m
}