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:org.ejbca.core.protocol.cmp.CmpMessageHelper.java

/** Creates a 16 bytes random sender nonce
 * /*ww w  .  ja v  a  2 s  .c  o  m*/
 * @return byte array of length 16
 */
public static byte[] createSenderNonce() {
    // Sendernonce is a random number
    byte[] senderNonce = new byte[16];
    Random randomSource;
    randomSource = new Random();
    randomSource.nextBytes(senderNonce);
    return senderNonce;
}

From source file:net.sf.keystore_explorer.crypto.privatekey.Pkcs8Util.java

private static byte[] generateSalt() {
    // Generate random 8-bit salt
    Random random = new Random();
    random.setSeed(Calendar.getInstance().getTimeInMillis());

    byte[] salt = new byte[8];

    random.nextBytes(salt);

    return salt;//ww w  .  j av a  2 s  .c  om
}

From source file:com.netscape.cms.servlet.test.DRMTest.java

private static byte[] genIV(int blockSize) throws Exception {
    // generate an IV
    byte[] iv = new byte[blockSize];

    Random rnd = new Random();
    rnd.nextBytes(iv);

    return iv;//  w ww .j  av  a 2  s  .  com
}

From source file:org.apache.hadoop.yarn.util.TestFSDownload.java

static LocalResource createJarFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis)
        throws IOException, URISyntaxException {
    byte[] bytes = new byte[len];
    r.nextBytes(bytes);

    File archiveFile = new File(p.toUri().getPath() + ".jar");
    archiveFile.createNewFile();//from  w  ww.j a  va 2 s .  c om
    JarOutputStream out = new JarOutputStream(new FileOutputStream(archiveFile));
    out.putNextEntry(new JarEntry(p.getName()));
    out.write(bytes);
    out.closeEntry();
    out.close();

    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(URL.fromPath(new Path(p.toString() + ".jar")));
    ret.setSize(len);
    ret.setType(LocalResourceType.ARCHIVE);
    ret.setVisibility(vis);
    ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".jar")).getModificationTime());
    return ret;
}

From source file:org.apache.hadoop.yarn.util.TestFSDownload.java

static LocalResource createZipFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis)
        throws IOException, URISyntaxException {
    byte[] bytes = new byte[len];
    r.nextBytes(bytes);

    File archiveFile = new File(p.toUri().getPath() + ".ZIP");
    archiveFile.createNewFile();/*from   w w w .  j a v a2s  .com*/
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(archiveFile));
    out.putNextEntry(new ZipEntry(p.getName()));
    out.write(bytes);
    out.closeEntry();
    out.close();

    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(URL.fromPath(new Path(p.toString() + ".ZIP")));
    ret.setSize(len);
    ret.setType(LocalResourceType.ARCHIVE);
    ret.setVisibility(vis);
    ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".ZIP")).getModificationTime());
    return ret;
}

From source file:org.apache.hadoop.yarn.util.TestFSDownload.java

static LocalResource createTarFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis)
        throws IOException, URISyntaxException {
    byte[] bytes = new byte[len];
    r.nextBytes(bytes);

    File archiveFile = new File(p.toUri().getPath() + ".tar");
    archiveFile.createNewFile();//from  w  ww  .  ja va2  s  .  co  m
    TarArchiveOutputStream out = new TarArchiveOutputStream(new FileOutputStream(archiveFile));
    TarArchiveEntry entry = new TarArchiveEntry(p.getName());
    entry.setSize(bytes.length);
    out.putArchiveEntry(entry);
    out.write(bytes);
    out.closeArchiveEntry();
    out.close();

    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(URL.fromPath(new Path(p.toString() + ".tar")));
    ret.setSize(len);
    ret.setType(LocalResourceType.ARCHIVE);
    ret.setVisibility(vis);
    ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".tar")).getModificationTime());
    return ret;
}

From source file:org.apache.hadoop.yarn.server.nodemanager.containermanager.localizer.TestContainerLocalizer.java

@SuppressWarnings({ "rawtypes", "unchecked" })
static DataInputBuffer createFakeCredentials(Random r, int nTok) throws IOException {
    Credentials creds = new Credentials();
    byte[] password = new byte[20];
    Text kind = new Text();
    Text service = new Text();
    Text alias = new Text();
    for (int i = 0; i < nTok; ++i) {
        byte[] identifier = ("idef" + i).getBytes();
        r.nextBytes(password);
        kind.set("kind" + i);
        service.set("service" + i);
        alias.set("token" + i);
        Token token = new Token(identifier, password, kind, service);
        creds.addToken(alias, token);/*from w w  w  .  java 2  s  .  co m*/
    }
    DataOutputBuffer buf = new DataOutputBuffer();
    creds.writeTokenStorageToStream(buf);
    DataInputBuffer ret = new DataInputBuffer();
    ret.reset(buf.getData(), 0, buf.getLength());
    return ret;
}

From source file:org.apache.hadoop.yarn.util.TestFSDownload.java

static LocalResource createTgzFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis)
        throws IOException, URISyntaxException {
    byte[] bytes = new byte[len];
    r.nextBytes(bytes);

    File gzipFile = new File(p.toUri().getPath() + ".tar.gz");
    gzipFile.createNewFile();/*ww  w  .  j  a  va 2 s.  co  m*/
    TarArchiveOutputStream out = new TarArchiveOutputStream(
            new GZIPOutputStream(new FileOutputStream(gzipFile)));
    TarArchiveEntry entry = new TarArchiveEntry(p.getName());
    entry.setSize(bytes.length);
    out.putArchiveEntry(entry);
    out.write(bytes);
    out.closeArchiveEntry();
    out.close();

    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(URL.fromPath(new Path(p.toString() + ".tar.gz")));
    ret.setSize(len);
    ret.setType(LocalResourceType.ARCHIVE);
    ret.setVisibility(vis);
    ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".tar.gz")).getModificationTime());
    return ret;
}

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

static void writeFile(FSDataOutputStream stm, int size) throws IOException {
    byte[] buffer = new byte[fileSize];
    Random rand = new Random(seed);
    rand.nextBytes(buffer);
    stm.write(buffer, 0, size);/*from  w  ww  . j ava  2s. c  om*/
}

From source file:org.apache.hadoop.hbase.regionserver.HFileReadWriteTest.java

private static byte[] createRandomQualifier(Random rand) {
    byte[] q = new byte[10 + rand.nextInt(30)];
    rand.nextBytes(q);
    return q;/*from  ww w .  j ava  2  s  .c o m*/
}