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 void main(String... a) {
    int n = 16;/*from  w ww  .ja  v a2  s.c  o  m*/

    Random r = new Random();
    byte[] b = new byte[n];
    r.nextBytes(b);
    BigInteger i = new BigInteger(b);

    System.out.println(i);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Random rand = new Random();

    byte[] bytes = new byte[5];
    rand.nextBytes(bytes);
}

From source file:Main.java

public static void main(String args[]) {

    Random randomno = new Random();

    // create byte array
    byte[] nbyte = new byte[30];

    // put the next byte in the array
    randomno.nextBytes(nbyte);

    // check the value of array   
    System.out.println("Value of byte array: " + Arrays.toString(nbyte));
}

From source file:org.helios.rindle.metric.MetricSerialization.java

public static void main(String[] args) {
    log("Test Metric Ser");
    byte[] okey = new byte[10];
    Random r = new Random(System.currentTimeMillis());
    r.nextBytes(okey);
    IMetricDefinition[] metrics = {/*from   ww  w . ja  v  a2s  . com*/

            new UnsafeMetricDefinition(77, "MeToo", okey),
            new UnsafeMetricDefinition(54, "FooBar", "FooBar".getBytes()) };

    //      SimpleModule module = new SimpleModule();
    //      module.addSerializer(IMetricDefinition.class, new MetricSerialization.UnsafeMetricDefinitionSerializer());
    //      module.addDeserializer(IMetricDefinition.class, new MetricSerialization.UnsafeMetricDefinitionDeserializer());
    //      MAP.registerModule(module);
    try {
        String s = JSON.MAP.writeValueAsString(metrics);
        log(s);
        //         String text = "[{\"id\":54,\"ts\":1397644799993,\"n\":\"FooBar\",\"o\":\"Um05dlFtRnk=\"},{\"id\":77,\"ts\":1397644799993,\"n\":\"MeToo\",\"o\":\"V1c5NWJ3PT0A\"}]";
        IMetricDefinition[] deser = JSON.MAP.readValue(s, IMetricDefinition[].class);
        log(Arrays.toString(deser));
        log("========================================");
        s = JSON.MAP.writeValueAsString(deser);
        log(s);
        log("========================================");
        for (int i = 0; i < deser.length; i++) {
            log("Metric 1:" + deser[i].equals(metrics[i]));
        }
    } catch (Exception x) {
        x.printStackTrace(System.err);
    }
    //      MAP.registerModule(mod);
}

From source file:HashCollider.java

/**i
 * @param args the command line arguments
 *///from  www.j a  va2s .c om
public static void main(String[] args) {
    Random random = new Random();
    byte[] nbytes1 = new byte[64];
    byte[] nbytes2 = new byte[64];
    float average = 0;
    // set number of bits for checking collision
    int number_of_bits = 4;
    random.nextBytes(nbytes1);
    random.nextBytes(nbytes2);

    String sha1 = DigestUtils.sha256Hex(nbytes1);
    String sha2 = DigestUtils.sha256Hex(nbytes2);

    String nsh1 = new BigInteger(sha1, 16).toString(2);
    String nsh2 = new BigInteger(sha2, 16).toString(2);
    int i = 0;
    for (int j = 0; j < 5; j++) {
        while (true) {
            if (nsh1.substring(0, number_of_bits - 1).equals(nsh2.substring(0, number_of_bits - 1))) {
                System.out.println(i);
                average += i;
                break;
            }
            i++;
            //            System.out.println("......"+i);
            random.nextBytes(nbytes1);
            random.nextBytes(nbytes2);
            sha1 = DigestUtils.sha256Hex(nbytes1);
            sha2 = DigestUtils.sha256Hex(nbytes2);
            nsh1 = new BigInteger(sha1, 16).toString(2);
            nsh2 = new BigInteger(sha2, 16).toString(2);
        }
        i = 0;
        //        System.out.println(nsh1+"\n"+nsh2);
        random.nextBytes(nbytes1);
        random.nextBytes(nbytes2);
        sha1 = DigestUtils.sha256Hex(nbytes1);
        sha2 = DigestUtils.sha256Hex(nbytes2);
        nsh1 = new BigInteger(sha1, 16).toString(2);
        nsh2 = new BigInteger(sha2, 16).toString(2);

    }
    //
    System.out.println("Average for  " + number_of_bits + " bits is  " + average / 5);

}

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

public static void main(String[] args) throws Exception {
    int numRegions = Integer.parseInt(args[0]);
    long numRows = Long.parseLong(args[1]);

    HTableDescriptor htd = new HTableDescriptor(TABLENAME);
    htd.setMaxFileSize(10L * 1024 * 1024 * 1024);
    htd.setValue(HTableDescriptor.SPLIT_POLICY, ConstantSizeRegionSplitPolicy.class.getName());
    htd.addFamily(new HColumnDescriptor(FAMILY1));
    htd.addFamily(new HColumnDescriptor(FAMILY2));
    htd.addFamily(new HColumnDescriptor(FAMILY3));

    Configuration conf = HBaseConfiguration.create();
    Connection conn = ConnectionFactory.createConnection(conf);
    Admin admin = conn.getAdmin();//from w  w  w  . j  av a 2s. c  om
    if (admin.tableExists(TABLENAME)) {
        admin.disableTable(TABLENAME);
        admin.deleteTable(TABLENAME);
    }
    if (numRegions >= 3) {
        byte[] startKey = new byte[16];
        byte[] endKey = new byte[16];
        Arrays.fill(endKey, (byte) 0xFF);
        admin.createTable(htd, startKey, endKey, numRegions);
    } else {
        admin.createTable(htd);
    }
    admin.close();

    Table table = conn.getTable(TABLENAME);
    byte[] qf = Bytes.toBytes("qf");
    Random rand = new Random();
    byte[] value1 = new byte[16];
    byte[] value2 = new byte[256];
    byte[] value3 = new byte[4096];
    for (long i = 0; i < numRows; i++) {
        Put put = new Put(Hashing.md5().hashLong(i).asBytes());
        rand.setSeed(i);
        rand.nextBytes(value1);
        rand.nextBytes(value2);
        rand.nextBytes(value3);
        put.addColumn(FAMILY1, qf, value1);
        put.addColumn(FAMILY2, qf, value2);
        put.addColumn(FAMILY3, qf, value3);
        table.put(put);
        if (i % 10000 == 0) {
            LOG.info(i + " rows put");
        }
    }
    table.close();
    conn.close();
}

From source file:org.opendedup.collections.ShardedProgressiveFileBasedCSMap.java

public static void main(String[] args) throws Exception {
    ShardedProgressiveFileBasedCSMap b = new ShardedProgressiveFileBasedCSMap();
    b.init(1000000, "/opt/sdfs/hash", .001);
    long start = System.currentTimeMillis();
    Random rnd = new Random();
    byte[] hash = null;
    long val = -33;
    byte[] hash1 = null;
    long val1 = -33;
    Tiger16HashEngine eng = new Tiger16HashEngine();
    for (int i = 0; i < 60000; i++) {
        byte[] z = new byte[64];
        rnd.nextBytes(z);
        hash = eng.getHash(z);/* ww w. j  av  a  2s .  com*/
        val = rnd.nextLong();
        if (i == 1) {
            val1 = val;
            hash1 = hash;
        }
        if (val < 0)
            val = val * -1;
        ChunkData cm = new ChunkData(hash, val);
        InsertRecord k = b.put(cm);
        if (k.getInserted())
            System.out.println("Unable to add this " + k);

    }
    long end = System.currentTimeMillis();
    System.out.println("Took " + (end - start) / 1000 + " s " + val1);
    System.out.println("Took " + (System.currentTimeMillis() - end) / 1000 + " ms at pos " + b.get(hash1));
    b.claimRecords(SDFSEvent.gcInfoEvent("testing 123"));
    b.close();

}

From source file:Main.java

private static ByteBuffer buffer(int len) {
    byte[] b = new byte[len];
    Random r = new Random();
    r.nextBytes(b);
    return ByteBuffer.wrap(b);
}

From source file:MainClass.java

private static String encrypt(char[] password, String plaintext) throws Exception {

    byte[] salt = new byte[8];
    Random random = new Random();
    random.nextBytes(salt);

    PBEKeySpec keySpec = new PBEKeySpec(password);

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");

    SecretKey key = keyFactory.generateSecret(keySpec);

    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 1000);

    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

    byte[] ciphertext = cipher.doFinal(plaintext.getBytes());

    BASE64Encoder encoder = new BASE64Encoder();

    String saltString = encoder.encode(salt);
    String ciphertextString = encoder.encode(ciphertext);

    return saltString + ciphertextString;
}

From source file:com.pamarin.income.util.StringRandom.java

public static String random2048bit() {
    byte[] r = new byte[256]; //Means 2048 bit
    Random random = new Random();
    random.nextBytes(r);

    return Base64.encodeBase64String(r).replace("+", "_");
}