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.gmu.uav.RadioSecurity.java

public static String computeSHA256(String password) {
    MessageDigest messagDigest = null;
    String saltedPassword = null;

    SecureRandom random = new SecureRandom();
    byte[] salt = new byte[SALT_BYTES];
    random.nextBytes(salt);

    saltedPassword = salt + password;/*from   w w w . jav a 2  s .  co m*/

    try {

        messagDigest = MessageDigest.getInstance("SHA-256");

    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();
    }

    messagDigest.update(saltedPassword.getBytes());

    byte finalHash[] = messagDigest.digest();

    return HASH_ITERATIONS + ":" + toHex(salt) + ":" + toHex(finalHash);
}

From source file:org.apache.zeppelin.python.PythonUtils.java

public static String createSecret(int secretBitLength) {
    SecureRandom rnd = new SecureRandom();
    byte[] secretBytes = new byte[secretBitLength / java.lang.Byte.SIZE];
    rnd.nextBytes(secretBytes);
    return Base64.encodeBase64String(secretBytes);
}

From source file:org.apache.openmeetings.util.crypt.SHA256Implementation.java

private static byte[] getSalt() throws NoSuchAlgorithmException {
    SecureRandom sr = SecureRandom.getInstance(SECURE_RND_ALG);
    byte[] salt = new byte[SALT_LENGTH];
    sr.nextBytes(salt);
    return salt;//from   w  ww  .  j a v  a  2  s  .c  o  m
}

From source file:org.casbah.provider.SSLeayEncoder.java

public static String encodeKey(RSAPrivateCrtKey key, String keypass)
        throws GeneralSecurityException, IOException {

    PKCS1EncodedKey pkcs1Key = new PKCS1EncodedKey(key);
    byte[] derData = pkcs1Key.getEncoded();
    byte[] salt = new byte[SALT_LENGTH];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);
    String pemData = encryptKey(derData, salt, keypass);
    StringBuffer buffer = new StringBuffer();
    buffer.append("-----BEGIN RSA PRIVATE KEY-----\n");
    buffer.append(PROC_TYPE + ": " + SUPPORTED_PROC_TYPE + "\n");
    buffer.append(DEK_INFO + ": " + SSLEAY_ENC_ALGORITHM + "," + Hex.encodeHexString(salt) + "\n\n");
    buffer.append(pemData);/*from  w  w w  . j av a 2 s  .c o  m*/
    buffer.append("-----END RSA PRIVATE KEY-----\n");
    return buffer.toString();
}

From source file:com.cisco.oss.foundation.directory.utils.ObfuscatUtil.java

/**
 * Generate a random salt./* ww w .ja  v  a2 s.  c  o  m*/
 *
 * @return
 *         the random salt.
 * @throws NoSuchAlgorithmException
 *         the NoSuchAlgorithmException.
 */
public static byte[] generateSalt() throws NoSuchAlgorithmException {
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
    byte[] salt = new byte[8];
    random.nextBytes(salt);
    return salt;
}

From source file:com.networknt.light.util.HashUtil.java

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

From source file:net.mc_cubed.msrp.MsrpUtil.java

/**
 * Generate a RFC 4975 Section 7.1 compliant transaction identifier
 * <p>/*  w  w  w  .ja va2 s.  c  o  m*/
 * To form a new request, the sender creates a transaction identifier and
 * uses this and the method name to create an MSRP request start line. The
 * transaction identifier MUST NOT collide with that of other transactions
 * that exist at the same time. Therefore, it MUST contain at least 64 bits
 * of randomness.
 */
public static String generateMsrpTransactionId() {
    SecureRandom secure = new SecureRandom();
    byte[] bytes = new byte[MSRP_TX_ID_LENGTH];
    secure.nextBytes(bytes);
    return Base64.encodeBase64String(bytes);
}

From source file:org.wso2.carbon.appmgt.rest.api.publisher.utils.RestApiPublisherUtils.java

public static String generateBinaryUUID() {
    SecureRandom secRandom = new SecureRandom();
    byte[] result = new byte[8];
    secRandom.nextBytes(result);
    String uuid = String.valueOf(Hex.encodeHex(result));
    return uuid;/*  www.  j  av  a  2s.  co m*/
}

From source file:net.mc_cubed.msrp.MsrpUtil.java

/**
 * Generate a RFC 4975 Section 14.1 compliant SessionId for use in an MSRP
 * URI//from ww w  .j  av  a2s . c  om
 * @return
 */
public static String generateMsrpUriSessionId() {
    SecureRandom secure = new SecureRandom();
    byte[] bytes = new byte[MSRP_URI_SESSION_LENGTH];
    secure.nextBytes(bytes);
    return Base64.encodeBase64String(bytes);
}

From source file:org.sakuli.services.cipher.AesCbcCipher.java

public static IvParameterSpec createIV(final int ivSizeBytes, final Optional<SecureRandom> rng) {
    final byte[] iv = new byte[ivSizeBytes];
    final SecureRandom theRNG = rng.orElse(new SecureRandom());
    theRNG.nextBytes(iv);
    return new IvParameterSpec(iv);
}