List of usage examples for java.util Random nextBytes
public void nextBytes(byte[] bytes)
From source file:org.owasp.benchmark.testcode.BenchmarkTest00931.java
void getNextNumber(java.util.Random generator, byte[] barray) { generator.nextBytes(barray); }
From source file:de.burlov.bouncycastle.io.test.EncryptOutputStreamTest.java
@Test public void testEncryptDecrypt() throws IOException { BlockCipher cipher = new SerpentEngine(); Random rnd = new Random(); byte[] key = new byte[256 / 8]; rnd.nextBytes(key); byte[] iv = new byte[cipher.getBlockSize()]; rnd.nextBytes(iv);/*from w w w.ja v a2s. co m*/ byte[] data = new byte[1230000]; new Random().nextBytes(data); ByteArrayOutputStream bout = new ByteArrayOutputStream(); CryptOutputStream eout = new CryptOutputStream(bout, cipher, key); eout.write(data); eout.close(); byte[] eData = bout.toByteArray(); // eData[1000] = (byte) (eData[1000] & 0x88); ByteArrayInputStream bin = new ByteArrayInputStream(eData); CryptInputStream din = new CryptInputStream(bin, cipher, key); bout = new ByteArrayOutputStream(); IOUtils.copy(din, bout); eData = bout.toByteArray(); Assert.assertTrue(Arrays.areEqual(data, eData)); }
From source file:org.commonjava.util.partyline.ConcurrentFirstReaderTest.java
/** * GIVEN:/*w ww . jav a 2 s . c o m*/ * <ol> * <li>File exists on disk</li> * <li>File is not opened for reading or writing</li> * <li>The underlying filesystem is slow to respond to lock requests on FileChannel</li> * </ol> * <br/> * WHEN: * <ol> * <li>Two reader threads attempt to open the file at the same time</li> * </ol> * <br/> * THEN: * <ol> * <li>One reader thread should "win" and be the first to open the file</li> * <li>The other reader thread should "join" that first thread's open file</li> * <li>No OverlappingFileLockException should be thrown</li> * </ol> * @throws Exception */ /*@formatter:off*/ @BMRules(rules = { // setup a rendezvous to control thread execution @BMRule(name = "init rendezvous", targetClass = "FileTree", targetMethod = "<init>", targetLocation = "ENTRY", action = "createRendezvous(\"begin\", 2);" + "debug(\"<<<init rendezvous for begin.\")"), // sync up to reduce chances of missing the race condition for the opLock.lock() control. @BMRule(name = "sync FileTree.setOrJoin", targetClass = "FileTree", targetMethod = "setOrJoinFile", targetLocation = "ENTRY", action = "debug(\"Rendezvous read operations.\"); " + "rendezvous(\"begin\"); " + "debug(\"Continue read operations.\");"), // When we try to init a new JoinableFile for INPUT, simulate an IOException from somewhere deeper in the stack. @BMRule(name = "new JoinableFile lock delay", targetClass = "JoinableFile", targetMethod = "<init>", targetLocation = "ENTRY", action = "debug(\"Delaying JoinableFile.init. Lock is: \" + $5); " + "Thread.sleep(500);" + "debug( \"Resuming lock operation.\" );") }) /*@formatter:on*/ @BMUnitConfig(debug = true) @Test public void run() throws Exception { byte[] bytes = new byte[1024 * 1024 * 2]; // let's get some serious data going. Random rand = new Random(); rand.nextBytes(bytes); final ExecutorService execs = Executors.newFixedThreadPool(2); final File f = temp.newFile("child.bin"); FileUtils.writeByteArrayToFile(f, bytes); final CountDownLatch latch = new CountDownLatch(2); final JoinableFileManager manager = new JoinableFileManager(); manager.startReporting(5000, 5000); for (int i = 0; i < 2; i++) { final int k = i; execs.execute(reader(k, manager, f, latch, true)); } latch.await(); }
From source file:org.sakaiproject.content.impl.serialize.impl.test.ByteStorageConversionCheck.java
@Test public void testRandomConversion() { byte[] bin = new byte[102400]; char[] cin = new char[102400]; byte[] bout = new byte[102400]; Random r = new Random(); r.nextBytes(bin); ByteStorageConversion.toChar(bin, 0, cin, 0, bin.length); ByteStorageConversion.toByte(cin, 0, bout, 0, cin.length); for (int i = 0; i < bin.length; i++) { if (bin[i] != bout[i]) { Assert.assertEquals(/*w ww. j a va2 s . c om*/ "Internal Byte conversion failed at " + bin[i] + "=>" + (int) cin[i] + "=>" + bout[i], bin[i], bout[i]); } } log.info("Internal Byte (random set) conversion test Passed Ok"); }
From source file:com.intel.cryptostream.CryptoCodecTest.java
@Before public void setUp() throws IOException { Random random = new SecureRandom(); random.nextBytes(key); random.nextBytes(iv);/*from w w w. j a v a 2 s. co m*/ }
From source file:org.osaf.cosmo.io.BufferedContentTest.java
public void testBufferedContent() throws Exception { Random random = new Random(); // 100K test// ww w .j av a 2s .com byte[] bytes = new byte[1024 * 100]; random.nextBytes(bytes); BufferedContent content = new BufferedContent(new ByteArrayInputStream(bytes)); Assert.assertTrue(content.getLength() == (1024 * 100)); // verify streams are the same Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bytes), content.getInputStream())); // verify we can re-consume the same stream Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bytes), content.getInputStream())); // should fit into memory Assert.assertTrue(content.getInputStream() instanceof ByteArrayInputStream); // should be buffered into file content = new BufferedContent(new ByteArrayInputStream(bytes), 1024 * 50); Assert.assertTrue(content.getLength() == (1024 * 100)); Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bytes), content.getInputStream())); Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bytes), content.getInputStream())); // should be in a file Assert.assertTrue(content.getInputStream() instanceof FileInputStream); }
From source file:com.muk.ext.security.impl.DefaultNonceService.java
@Override public byte[] generatePsudoRandomNonce() throws NoSuchAlgorithmException { byte[] nonce = new byte[16]; Random rand; rand = SecureRandom.getInstance("SHA1PRNG"); rand.nextBytes(nonce); return nonce; }
From source file:org.apache.hadoop.hdfs.server.namenode.TestFileDeleteWhitelist.java
private void createFile(FileSystem fileSys, Path name) throws IOException { FSDataOutputStream stm = fileSys.create(name, true, fileSys.getConf().getInt("io.file.buffer.size", 4096), (short) 1, (long) blockSize); byte[] buffer = new byte[1024]; Random rand = new Random(seed); rand.nextBytes(buffer); stm.write(buffer);/*from w ww .j a v a2 s . c om*/ stm.close(); }
From source file:org.apache.nifi.file.FileUtils.java
/** * Randomly generates a sequence of bytes and overwrites the contents of the * file a number of times. The file is then deleted. * * @param file File to be overwritten a number of times and, ultimately, * deleted/*from w w w . j a v a2 s. c om*/ * @param passes Number of times file should be overwritten * @throws IOException if something makes shredding or deleting a problem */ public static void shredFile(final File file, final int passes) throws IOException { final Random generator = new Random(); final long fileLength = file.length(); final int byteArraySize = (int) Math.min(fileLength, 1048576); // 1MB final byte[] b = new byte[byteArraySize]; final long numOfRandomWrites = (fileLength / b.length) + 1; final FileOutputStream fos = new FileOutputStream(file); try { // Over write file contents (passes) times final FileChannel channel = fos.getChannel(); for (int i = 0; i < passes; i++) { generator.nextBytes(b); for (int j = 0; j <= numOfRandomWrites; j++) { fos.write(b); } fos.flush(); channel.position(0); } // Write out "0" for each byte in the file Arrays.fill(b, (byte) 0); for (int j = 0; j < numOfRandomWrites; j++) { fos.write(b); } fos.flush(); fos.close(); // Try to delete the file a few times if (!FileUtils.deleteFile(file, null, 5)) { throw new IOException("Failed to delete file after shredding"); } } finally { FileUtils.closeQuietly(fos); } }
From source file:org.echocat.marquardt.common.ValidatorUnitTest.java
private void whenManipulatingLast2PayloadBytes() { final byte[] bytes = new byte[2]; final Random random = new Random(); random.nextBytes(bytes); final int length = _signedPayload.length; _signedPayload[length - 2] = bytes[0]; _signedPayload[length - 1] = bytes[1]; }