List of usage examples for java.util Random nextBytes
public void nextBytes(byte[] bytes)
From source file:com.cprassoc.solr.auth.security.Sha256AuthenticationProvider.java
static void putUser(String user, String pwd, Map credentials) { if (user == null || pwd == null) return;/*from www . j a v a 2 s .c o m*/ final Random r = new SecureRandom(); byte[] salt = new byte[32]; r.nextBytes(salt); String saltBase64 = Base64.encodeBase64String(salt); String val = sha256(pwd, saltBase64) + " " + saltBase64; credentials.put(user, val); }
From source file:org.apache.hadoop.hdfs.TestListFilesInFileContext.java
private static void writeFile(FileContext fc, Path name, int fileSize) throws IOException { // Create and write a file that contains three blocks of data FSDataOutputStream stm = fc.create(name, EnumSet.of(CreateFlag.CREATE), Options.CreateOpts.createParent()); byte[] buffer = new byte[fileSize]; Random rand = new Random(seed); rand.nextBytes(buffer); stm.write(buffer);/*from ww w. j ava2s.c o m*/ stm.close(); }
From source file:org.apache.hadoop.hdfs.TestPipelines.java
static byte[] writeData(final FSDataOutputStream out, final int length) throws IOException { int bytesToWrite = length; byte[] ret = new byte[bytesToWrite]; byte[] toWrite = new byte[1024]; int written = 0; Random rb = new Random(rand.nextLong()); while (bytesToWrite > 0) { rb.nextBytes(toWrite); int bytesToWriteNext = (1024 < bytesToWrite) ? 1024 : bytesToWrite; out.write(toWrite, 0, bytesToWriteNext); System.arraycopy(toWrite, 0, ret, (ret.length - bytesToWrite), bytesToWriteNext); written += bytesToWriteNext;/*from w ww. j a va2 s. c o m*/ if (LOG.isDebugEnabled()) { LOG.debug("Written: " + bytesToWriteNext + "; Total: " + written); } bytesToWrite -= bytesToWriteNext; } return ret; }
From source file:de.zib.chordsharp.Main.java
private static void minibench() { try {//from www.j a v a 2 s . c om int DATA_SIZE = 1000; byte[] data = new byte[DATA_SIZE]; Random r = new Random(); r.nextBytes(data); int size = 100; long time = System.currentTimeMillis(); for (int i = 0; i < size; i++) { Transaction transaction = new Transaction(); transaction.start(); // transaction.write("" + i, new String(data)); transaction.write(new OtpErlangString("" + i), new OtpErlangBinary(data)); transaction.commit(); } System.out.println("time: " + (System.currentTimeMillis() - time)); System.out.println("speed: " + ((size * 1000) / (System.currentTimeMillis() - time))); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.apache.jackrabbit.oak.plugins.blob.SharedDataStoreUtilsTest.java
static InputStream randomStream(int seed, int size) { Random r = new Random(seed); byte[] data = new byte[size]; r.nextBytes(data); return new ByteArrayInputStream(data); }
From source file:com.liferay.portal.security.pwd.PwdEncryptor.java
public static byte[] _getSaltFromSSHA(String sshaString) throws PwdEncryptorException { byte[] saltBytes = new byte[8]; //if (Validator.isNull(sshaString)) { if (StringUtils.isEmpty(sshaString)) { // Generate random salt Random random = new SecureRandom(); random.nextBytes(saltBytes); } else {/*w w w . ja v a 2s. co m*/ // Extract salt from encrypted password try { byte[] digestPlusSalt = ByteArray.fromBase64String(sshaString); //byte[] digestPlusSalt = Base64.decode(sshaString); byte[] digestBytes = new byte[digestPlusSalt.length - 8]; System.arraycopy(digestPlusSalt, 0, digestBytes, 0, digestBytes.length); System.arraycopy(digestPlusSalt, digestBytes.length, saltBytes, 0, saltBytes.length); } catch (Exception e) { throw new PwdEncryptorException( "Unable to extract salt from encrypted password: " + e.getMessage()); } } return saltBytes; }
From source file:org.apache.hadoop.fs.FileSystemTestHelper.java
static String writeFile(FileSystem fileSys, Path name, int fileSize) throws IOException { final long seed = 0xDEADBEEFL; // Create and write a file that contains three blocks of data FSDataOutputStream stm = fileSys.create(name); byte[] buffer = new byte[fileSize]; Random rand = new Random(seed); rand.nextBytes(buffer); stm.write(buffer);//www . ja v a 2 s . c o m stm.close(); return new String(buffer); }
From source file:labr_client.Public.encryption.java
public static String encode(String dec) throws GeneralSecurityException { // Generate 128 bits of random data for use as the IV. It is important to use a different IV for // each encrypted block of text, to ensure that the same string encrypted by two different people // does not give the same encrypted text string - that leads to obvious attack possibilities. Note // however that the IV does not need to be kept secret; it is a little bit like a 'salt' for a // password, which improves security even when the salt is stored in plaintext in a database or // prefixed to the encrypted file. byte[] ivData = new byte[AES_NIVBITS / 8]; //Hoe groot is deze array -> 128/8 = 16 Random r = new Random(); // Note: no seed here, ie these values are truly random r.nextBytes(ivData); //// try { //// System.out.println(new String(ivData, "UTF-8")); // for UTF-8 encoding //// } catch (UnsupportedEncodingException ex) { //// Logger.getLogger(encryption.class.getName()).log(Level.SEVERE, null, ex); //// } // ivData[0] = Byte.valueOf("100"); // ivData[1] = Byte.valueOf("1"); // ivData[2] = Byte.valueOf("15"); // ivData[3] = Byte.valueOf("50"); // ivData[4] = Byte.valueOf("70"); // ivData[5] = Byte.valueOf("80"); // ivData[6] = Byte.valueOf("5"); // ivData[7] = Byte.valueOf("45"); // ivData[8] = Byte.valueOf("100"); // ivData[9] = Byte.valueOf("1"); // ivData[10] = Byte.valueOf("15"); // ivData[11] = Byte.valueOf("50"); // ivData[12] = Byte.valueOf("70"); // ivData[13] = Byte.valueOf("80"); // ivData[14] = Byte.valueOf("5"); // ivData[15] = Byte.valueOf("45"); // Select encryption algorithm and padding : AES with CBC and PCKS#7 //byte[] ivData = new sun.misc.BASE64Decoder().decodeBuffer(salt); BlockCipherPadding padding = new PKCS7Padding(); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding); // Encrypt the input string using key + iv KeyParameter keyParam = getAesKey(); CipherParameters params = new ParametersWithIV(keyParam, ivData); cipher.reset();//from w ww . j a v a 2 s . co m cipher.init(true, params); // first param = encode/decode byte[] bytesDec = dec.getBytes(UTF8); // data to decode byte[] bytesEnc; try { int buflen = cipher.getOutputSize(bytesDec.length); bytesEnc = new byte[buflen]; int nBytesEnc = cipher.processBytes(bytesDec, 0, bytesDec.length, bytesEnc, 0); nBytesEnc += cipher.doFinal(bytesEnc, nBytesEnc); if (nBytesEnc != bytesEnc.length) { throw new IllegalStateException("Unexpected behaviour : getOutputSize value incorrect"); } } catch (InvalidCipherTextException | RuntimeException e) { throw new GeneralSecurityException("encryption failed"); } // Return a base-64-encoded string containing IV + encrypted input string byte[] bytesAll = new byte[ivData.length + bytesEnc.length]; arraycopy(ivData, 0, bytesAll, 0, ivData.length); arraycopy(bytesEnc, 0, bytesAll, ivData.length, bytesEnc.length); out.println(new String(encodeBase64(bytesAll), UTF8)); return new String(encodeBase64(bytesAll), UTF8); }
From source file:hd3gtv.mydmam.auth.AuthenticationBackend.java
/** * @return 12 first chars of Base64(SHA-264(random(1024b))) *//*from w ww .j a v a 2s .c om*/ public static String passwordGenerator() throws NoSuchAlgorithmException, NoSuchProviderException { MessageDigest md = MessageDigest.getInstance("SHA-256"); Random r = new Random(); byte[] fill = new byte[1024]; r.nextBytes(fill); byte[] key = md.digest(fill); return new String(Base64.encodeBase64String(key)).substring(0, 12); }
From source file:org.apache.hadoop.fs.nfs.tools.Nfs3Console.java
private static void measureReadBandwidth() throws Exception { final int minBufferSize = 65536; final long fileSizes[] = { /*1024L * 1024L, 1024L * 1024L *1024L,*/ 4096L * 1024L * 1024L/*, 16384L * 1024L *1024L*/ }; final byte buffer[] = new byte[minBufferSize]; final Random random = new Random(); // Initialize the buffer random.nextBytes(buffer); // Make the files for (int i = 0; i < fileSizes.length; ++i) { String filename = "testfile-" + fileSizes[i] + ".dat"; FSDataOutputStream output = nfs.create(new Path(filename), true); long startTime = System.currentTimeMillis(), stopTime; // Copy byte-by-byte for small files if (fileSizes[i] < minBufferSize) { for (long j = 0; j < fileSizes[i]; ++j) { output.write(0);// w w w . j a v a2s. c om } output.close(); stopTime = System.currentTimeMillis(); LOG.info("FILE WRITE of " + filename + " took " + (stopTime - startTime) + " ms"); } // Copy one buffer at a time for large file else { long totalBytesWritten = 0L; for (long j = 0; j < fileSizes[i]; j += buffer.length) { output.write(buffer); totalBytesWritten += buffer.length; } output.close(); stopTime = System.currentTimeMillis(); LOG.info("FILE WRITE of " + filename + " took " + (stopTime - startTime) + " ms"); LOG.info("FILE WRITE of " + filename + " transferred at " + calculateTransferRateMB(totalBytesWritten, (stopTime - startTime)) + " MB/s"); } } // Read the files and measure time for (int i = 0; i < fileSizes.length; ++i) { String filename = "testfile-" + fileSizes[i] + ".dat"; FSDataInputStream input = nfs.open(new Path(filename)); long startTime = System.currentTimeMillis(), stopTime; // Copy byte-by-byte for small files if (fileSizes[i] < minBufferSize) { byte b; while ((b = (byte) input.read()) != -1) { assert (b == 0); } input.close(); stopTime = System.currentTimeMillis(); LOG.info("FILE READ of " + filename + " took " + (stopTime - startTime) + " ms"); } // Copy one buffer at a time for large file else { byte inBuf[] = new byte[minBufferSize]; long totalBytesRead = 0, bytesRead; while ((bytesRead = input.read(inBuf)) != -1) { for (int j = 0; j < buffer.length; ++j) { assert (inBuf[j] == buffer[j]); } totalBytesRead += bytesRead; } input.close(); stopTime = System.currentTimeMillis(); LOG.info("FILE READ of " + filename + " took " + (stopTime - startTime) + " ms"); LOG.info("FILE READ of " + filename + " transferred at " + calculateTransferRateMB(totalBytesRead, (stopTime - startTime)) + " MB/s"); } } }