List of usage examples for java.util Random nextBytes
public void nextBytes(byte[] bytes)
From source file:org.apache.hadoop.fs.TestCopyFiles.java
private static void updateFiles(FileSystem fs, String topdir, MyFile[] files, int nupdate) throws IOException { assert nupdate <= NFILES; Path root = new Path(topdir); for (int idx = 0; idx < nupdate; ++idx) { Path fPath = new Path(root, files[idx].getName()); // overwrite file assertTrue(fPath.toString() + " does not exist", fs.exists(fPath)); FSDataOutputStream out = fs.create(fPath); files[idx].reset();/* w w w . ja v a 2 s .co m*/ byte[] toWrite = new byte[files[idx].getSize()]; Random rb = new Random(files[idx].getSeed()); rb.nextBytes(toWrite); out.write(toWrite); out.close(); } }
From source file:org.apache.hadoop.fs.TestCopyFiles.java
private static boolean checkFiles(FileSystem fs, String topdir, MyFile[] files, boolean existingOnly) throws IOException { Path root = new Path(topdir); for (int idx = 0; idx < files.length; idx++) { Path fPath = new Path(root, files[idx].getName()); try {/*w ww . j a v a2s . c o m*/ fs.getFileStatus(fPath); FSDataInputStream in = fs.open(fPath); byte[] toRead = new byte[files[idx].getSize()]; byte[] toCompare = new byte[files[idx].getSize()]; Random rb = new Random(files[idx].getSeed()); rb.nextBytes(toCompare); assertEquals("Cannnot read file.", toRead.length, in.read(toRead)); in.close(); for (int i = 0; i < toRead.length; i++) { if (toRead[i] != toCompare[i]) { return false; } } toRead = null; toCompare = null; } catch (FileNotFoundException fnfe) { if (!existingOnly) { throw fnfe; } } } return true; }
From source file:org.syncany.tests.unit.util.TestFileUtil.java
public static void changeRandomPartOfBinaryFile(File file) throws IOException { if (file != null && !file.exists()) { throw new IOException("File does not exist: " + file); }/*from ww w . ja v a 2 s . c o m*/ if (file.isDirectory()) { throw new IOException("Cannot change directory: " + file); } // Prepare: random bytes at random position Random randomEngine = new Random(); int fileSize = (int) file.length(); int maxChangeBytesLen = 20; int maxChangeBytesStartPos = (fileSize - maxChangeBytesLen - 1 >= 0) ? fileSize - maxChangeBytesLen - 1 : 0; int changeBytesStartPos = (maxChangeBytesStartPos > 0) ? randomEngine.nextInt(maxChangeBytesStartPos) : 0; int changeBytesLen = (fileSize - changeBytesStartPos < maxChangeBytesLen) ? fileSize - changeBytesStartPos - 1 : maxChangeBytesLen; byte[] changeBytes = new byte[changeBytesLen]; randomEngine.nextBytes(changeBytes); // Write to file RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); randomAccessFile.seek(changeBytesStartPos); randomAccessFile.write(changeBytes); randomAccessFile.close(); }
From source file:microsoft.exchange.webservices.data.core.ExchangeServiceBase.java
/** * Gets the session key.//w ww .j a va2s .co m * @return session key */ public static byte[] getSessionKey() { // this has to be computed only once. synchronized (ExchangeServiceBase.class) { if (ExchangeServiceBase.binarySecret == null) { Random randomNumberGenerator = new Random(); ExchangeServiceBase.binarySecret = new byte[256 / 8]; randomNumberGenerator.nextBytes(binarySecret); } return ExchangeServiceBase.binarySecret; } }
From source file:org.zenoss.zep.dao.impl.IndexMetadataDaoImplIT.java
@Test public void testIndexMetadataDao() throws ZepException { byte[] sha1 = new byte[20]; Random r = new Random(); r.nextBytes(sha1); IndexMetadata md = new IndexMetadata(); md.setIndexName("myindex"); md.setIndexVersion(5);/* w w w.j a v a 2s .com*/ md.setIndexVersionHash(sha1); md.setZepInstance(zepInstance.getId()); assertNull(indexMetadataDao.findIndexMetadata(md.getIndexName())); indexMetadataDao.updateIndexVersion(md.getIndexName(), md.getIndexVersion(), md.getIndexVersionHash()); IndexMetadata mdFromDb = indexMetadataDao.findIndexMetadata(md.getIndexName()); assertArrayEquals(md.getIndexVersionHash(), mdFromDb.getIndexVersionHash()); assertEquals(md.getIndexName(), mdFromDb.getIndexName()); assertEquals(md.getIndexVersion(), mdFromDb.getIndexVersion()); assertEquals(md.getZepInstance(), mdFromDb.getZepInstance()); }
From source file:com.microsoft.azure.keyvault.test.CertificateOperationsTest.java
/** * Verify a RSA key pair with a simple encrypt/decrypt test. * /* w w w . j a v a 2 s .co m*/ * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ private static void verifyRSAKeyPair(KeyPair keyPair) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { // Validate algorithm is RSA Assert.assertTrue(keyPair.getPublic().getAlgorithm().equals(ALGO_RSA)); Assert.assertTrue(keyPair.getPrivate().getAlgorithm().equals(ALGO_RSA)); // Generate an array of 10 random bytes byte[] plainData = new byte[10]; Random random = new Random(); random.nextBytes(plainData); // Encrypt using the public key Cipher encryptCipher = Cipher.getInstance(ALGO_RSA); encryptCipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic()); byte[] encryptedData = encryptCipher.doFinal(plainData); // Decrypt using the private key Cipher decryptCipher = Cipher.getInstance(ALGO_RSA); decryptCipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate()); byte[] decryptedData = decryptCipher.doFinal(encryptedData); // Validate plainData is equal to decryptedData Assert.assertArrayEquals(plainData, decryptedData); }
From source file:microsoft.exchange.webservices.data.ExchangeServiceBase.java
/** * Gets the session key.// w w w . jav a 2s .c o m */ protected static byte[] getSessionKey() { // this has to be computed only once. synchronized (ExchangeServiceBase.class) { if (ExchangeServiceBase.binarySecret == null) { Random randomNumberGenerator = new Random(); ExchangeServiceBase.binarySecret = new byte[256 / 8]; randomNumberGenerator.nextBytes(binarySecret); } return ExchangeServiceBase.binarySecret; } }
From source file:filters.SaltingPasswordFilter.java
@Override public String executePreProcessing(String request) { final Random r = new SecureRandom(); byte[] salt = new byte[32]; r.nextBytes(salt); byte[] encodedSalt = Base64.encodeBase64(salt); String saltString = new String(encodedSalt); request = request.concat(saltString); return request; }
From source file:org.apache.apex.malhar.lib.utils.IOUtilsTest.java
private void createDataFile(File file, int dataSize) throws IOException { byte[] val = new byte[dataSize]; Random random = new Random(); random.nextBytes(val); FileUtils.write(file, new String(val)); }
From source file:org.apache.hadoop.tools.TestIntegrationByChunk.java
private static void touchFile(String path, long totalFileSize, boolean preserveBlockSize, Options.ChecksumOpt checksumOpt, long seed) throws IOException { FileSystem fs;//from w w w .j av a 2 s. c o m DataOutputStream outputStream = null; try { fs = cluster.getFileSystem(); final Path qualifiedPath = new Path(path).makeQualified(fs.getUri(), fs.getWorkingDirectory()); final long blockSize = preserveBlockSize ? NON_DEFAULT_BLOCK_SIZE : fs.getDefaultBlockSize(qualifiedPath) * 2; FsPermission permission = FsPermission.getFileDefault().applyUMask(FsPermission.getUMask(fs.getConf())); outputStream = fs.create(qualifiedPath, permission, EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE), 0, (short) (fs.getDefaultReplication(qualifiedPath) * 2), blockSize, null, checksumOpt); try { if (totalFileSize > 0) { int bufferLen = DEFAULT_BUFFER_SIZE; byte[] toWrite = new byte[bufferLen]; Random rb = new Random(seed); long bytesToWrite = totalFileSize; while (bytesToWrite > 0) { rb.nextBytes(toWrite); int bytesToWriteNext = (bufferLen < bytesToWrite) ? bufferLen : (int) bytesToWrite; outputStream.write(toWrite, 0, bytesToWriteNext); bytesToWrite -= bytesToWriteNext; } } } finally { if (outputStream != null) { outputStream.close(); } } FileStatus fileStatus = fs.getFileStatus(qualifiedPath); System.out.println(fileStatus.getBlockSize()); System.out.println(fileStatus.getReplication()); } finally { IOUtils.cleanup(null, outputStream); } }