Example usage for org.bouncycastle.crypto.prng DigestRandomGenerator nextBytes

List of usage examples for org.bouncycastle.crypto.prng DigestRandomGenerator nextBytes

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.prng DigestRandomGenerator nextBytes.

Prototype

public void nextBytes(byte[] bytes) 

Source Link

Usage

From source file:hu.akarnokd.utils.crypto.CryptoUtils.java

License:Apache License

/**
 * Generates salt with the given length.
 * @param size the number of bytes//ww w  .j ava  2s .c o  m
 * @return the salt bytes
 */
@NonNull
public static byte[] generateSalt(int size) {
    Digest digest = null;

    switch (String.format(DEFAULT_SALT_ALG, DEFAULT_PBE_KEY_BITS)) {
    case "SHA224PRNG":
        digest = new SHA224Digest();
        break;
    case "SHA256PRNG":
        digest = new SHA256Digest();
        break;
    case "SHA384PRNG":
        digest = new SHA384Digest();
        break;
    case "SHA512PRNG":
        digest = new SHA512Digest();
        break;
    default:
        digest = new SHA1Digest();
    }

    DigestRandomGenerator drg = new DigestRandomGenerator(digest);
    drg.addSeedMaterial(System.currentTimeMillis());
    byte[] r = new byte[size];
    drg.nextBytes(r);
    return r;
}