Example usage for java.util Random nextBytes

List of usage examples for java.util Random nextBytes

Introduction

In this page you can find the example usage for java.util Random nextBytes.

Prototype

public void nextBytes(byte[] bytes) 

Source Link

Document

Generates random bytes and places them into a user-supplied byte array.

Usage

From source file:Main.java

public static byte[] getRandomBuffer(int size) {
    byte[] buffer = new byte[size];
    Random random = new Random();
    random.nextBytes(buffer);
    return buffer;
}

From source file:net.kazyx.wirespider.TestUtil.java

public static byte[] fixedLengthRandomByteArray(int length) {
    System.out.println("Create random byte array: " + length);
    byte[] ba = new byte[length];
    Random rnd = new Random(20L);
    rnd.nextBytes(ba);
    return ba;//from  w  w  w . ja v a2  s. c  o  m
}

From source file:Main.java

public static ByteArrayInputStream getRandomDataStream(int length) {
    final Random randGenerator = new Random();
    final byte[] buff = new byte[length];
    randGenerator.nextBytes(buff);
    return new ByteArrayInputStream(buff);
}

From source file:org.apache.james.mime4j.Base64InputStreamBench.java

private static byte[] initData(int size) {
    Random random = new Random(size);
    byte[] data = new byte[size];
    random.nextBytes(data);
    return data;//ww w .ja  v  a 2  s. c  o  m
}

From source file:com.hazelcast.simulator.utils.GeneratorUtils.java

public static byte[] generateByteArray(Random random, int length) {
    byte[] result = new byte[length];
    random.nextBytes(result);
    return result;
}

From source file:com.aspose.showcase.qrcodegen.web.api.util.StringEncryptor.java

public static String encrypt(String data, String password) throws Exception {

    Security.addProvider(new BouncyCastleProvider());
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");

    final Random r = new SecureRandom();
    byte[] salt = new byte[SALT_SIZE];
    r.nextBytes(salt);

    SecretKeyFactory fact = SecretKeyFactory.getInstance("PBEWITHMD5AND128BITAES-CBC-OPENSSL", "BC");

    cipher.init(Cipher.ENCRYPT_MODE,
            fact.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_SALE_SIZE)));

    byte[] encVal = cipher.doFinal(data.getBytes());

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    // writing encrypted data along with the salt in the format readable by
    // open ssl api
    bos.write("Salted__".getBytes());
    bos.write(salt);//from   w  w  w .j a  v a  2  s  . c  o m
    bos.write(encVal);
    String encryptedValue = new String(Base64.encode(bos.toByteArray()));
    bos.close();

    return encryptedValue;

}

From source file:org.apache.hadoop.dfs.AppendTestUtil.java

static byte[] randomBytes(long seed, int size) {
    LOG.info("seed=" + seed + ", size=" + size);
    final byte[] b = new byte[size];
    final Random rand = new Random(seed);
    rand.nextBytes(b);
    return b;/* ww w  .  jav a  2  s. c om*/
}

From source file:org.eclipse.hawkbit.artifact.repository.ArtifactFilesystemRepositoryTest.java

private static byte[] randomBytes() {
    final byte[] randomBytes = new byte[20];
    final Random ran = new Random();
    ran.nextBytes(randomBytes);
    return randomBytes;
}

From source file:com.izforge.izpack.test.util.TestHelper.java

/**
 * Helper to create a file of the specified size containing random data.
 *
 * @param file the file//from   w  w w  . j av  a 2  s.co m
 * @param size the file size
 * @return a new file
 * @throws IOException for any I/O error
 */
public static File createFile(File file, int size) throws IOException {
    byte[] data = new byte[size];
    Random random = new Random();
    random.nextBytes(data);
    FileOutputStream stream = new FileOutputStream(file);
    stream.write(data);
    return file;
}

From source file:pieShareAppITs.helper.ITFileUtils.java

public static File createFile(File file, long size) throws FileNotFoundException, IOException {
    int defaultSize = 1024;
    if (size < defaultSize) {
        defaultSize = (int) size;
    }//from w w w .j a  v  a 2 s.  c o  m

    long max = size / defaultSize;
    long delta = size - (max * defaultSize);
    Random random = new Random();
    byte[] data = new byte[defaultSize];

    for (int i = 0; i < max; i++) {
        random.nextBytes(data);
        FileUtils.writeByteArrayToFile(file, data, true);
    }

    data = new byte[(int) delta];
    random.nextBytes(data);
    FileUtils.writeByteArrayToFile(file, data, true);

    return file;
}