List of usage examples for java.security SecureRandom nextBytes
@Override public void nextBytes(byte[] bytes)
From source file:de.inpiraten.jdemocrator.TAN.generator.TANGenerator.java
public void generateMasterTANs() throws IOException { //Read seed from System.in System.out.println("You will be asked to specify a random seed. Please enter a number of random"); System.out.println("characters to your liking. This will help to seed a secure random number"); System.out.println("generator which will generate the master TANs for every vote and derive the"); System.out.println("TANs. The longer your seed the stronger your master TANs."); System.out.print("\nEnter the seed for the TAN generator.\n> "); String seed = commandLineInput.readLine(); SecureRandom random = new SecureRandom( ArrayUtils.addAll(seed.getBytes(), ("" + System.currentTimeMillis()).getBytes())); //Get length of master TANs int masterTANlength = -1; while (masterTANlength < 6) { System.out.print("\nPlease enter the length of the master TANs in bytes. (Standard: 18)\n>"); int input; try {//from w w w.ja v a 2 s. c om input = inputInteger(); if (input > 5) { masterTANlength = input; } else if (input > 0) { System.out .println("Lengths shorter than 6 byte are too insecure. Please chose another length."); } else if (input == 0) { masterTANlength = 18; //standard option } else throw new NumberFormatException("Length of master TAN must be positive"); } catch (NumberFormatException e) { System.out.println("Invalid input. Please try again."); } } this.masterTAN = new String[this.event.numberOfElections]; byte[] rawTAN = new byte[masterTANlength]; for (int i = 0; i < this.masterTAN.length; i++) { random.nextBytes(rawTAN); this.masterTAN[i] = Base64.encodeBase64String(rawTAN); } }
From source file:com.qut.middleware.deployer.logic.RegisterESOELogic.java
private String generatePassphrase() { SecureRandom random; String passphrase;/* w ww .j ava2 s .co m*/ byte[] buf; try { /* Attempt to get the specified RNG instance */ random = SecureRandom.getInstance("SHA1PRNG"); } catch (NoSuchAlgorithmException nsae) { this.logger.error("NoSuchAlgorithmException when trying to create SecureRandom instance " //$NON-NLS-1$ + nsae.getLocalizedMessage()); this.logger.debug(nsae.getLocalizedMessage(), nsae); random = new SecureRandom(); } buf = new byte[Constants.PASSPHRASE_LENGTH]; random.nextBytes(buf); passphrase = new String(Hex.encodeHex(buf)); return passphrase; }
From source file:com.badlogic.gdx.pay.android.ouya.PurchaseManagerAndroidOUYA.java
public void requestPurchase(final Product product) throws GeneralSecurityException, UnsupportedEncodingException, JSONException { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); // This is an ID that allows you to associate a successful purchase with // it's original request. The server does nothing with this string except // pass it back to you, so it only needs to be unique within this instance // of your app to allow you to pair responses with requests. String uniqueId = Long.toHexString(sr.nextLong()); JSONObject purchaseRequest = new JSONObject(); purchaseRequest.put("uuid", uniqueId); purchaseRequest.put("identifier", product.getIdentifier()); // purchaseRequest.put("testing", "true"); // !!!! This value is only needed for testing, not setting it results in a live purchase String purchaseRequestJson = purchaseRequest.toString(); byte[] keyBytes = new byte[16]; sr.nextBytes(keyBytes); SecretKey key = new SecretKeySpec(keyBytes, "AES"); byte[] ivBytes = new byte[16]; sr.nextBytes(ivBytes);//ww w.jav a 2s . c o m IvParameterSpec iv = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, key, iv); byte[] payload = cipher.doFinal(purchaseRequestJson.getBytes("UTF-8")); cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, ouyaPublicKey); byte[] encryptedKey = cipher.doFinal(keyBytes); purchasable = new Purchasable(product.getIdentifier(), Base64.encodeToString(encryptedKey, Base64.NO_WRAP), Base64.encodeToString(ivBytes, Base64.NO_WRAP), Base64.encodeToString(payload, Base64.NO_WRAP)); synchronized (ouyaOutstandingPurchaseRequests) { ouyaOutstandingPurchaseRequests.put(uniqueId, product); } }
From source file:com.guillaumesoft.escapehellprison.PurchaseActivity.java
public void requestPurchase(final Product product) throws GeneralSecurityException, UnsupportedEncodingException, JSONException { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); // This is an ID that allows you to associate a successful purchase with // it's original request. The server does nothing with this string except // pass it back to you, so it only needs to be unique within this instance // of your app to allow you to pair responses with requests. String uniqueId = Long.toHexString(sr.nextLong()); JSONObject purchaseRequest = new JSONObject(); purchaseRequest.put("uuid", uniqueId); purchaseRequest.put("identifier", product.getIdentifier()); purchaseRequest.put("testing", "true"); // This value is only needed for testing, not setting it results in a live purchase String purchaseRequestJson = purchaseRequest.toString(); byte[] keyBytes = new byte[16]; sr.nextBytes(keyBytes); SecretKey key = new SecretKeySpec(keyBytes, "AES"); byte[] ivBytes = new byte[16]; sr.nextBytes(ivBytes);/*w w w . j a va2s. co m*/ IvParameterSpec iv = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, key, iv); byte[] payload = cipher.doFinal(purchaseRequestJson.getBytes("UTF-8")); cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, mPublicKey); byte[] encryptedKey = cipher.doFinal(keyBytes); Purchasable purchasable = new Purchasable(product.getIdentifier()); synchronized (mOutstandingPurchaseRequests) { mOutstandingPurchaseRequests.put(uniqueId, product); } mOuyaFacade.requestPurchase(this, purchasable, new PurchaseListener(product)); }
From source file:tv.ouya.sample.IapSampleActivity.java
public void requestPurchase(final Product product) throws GeneralSecurityException, UnsupportedEncodingException, JSONException { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); // This is an ID that allows you to associate a successful purchase with // it's original request. The server does nothing with this string except // pass it back to you, so it only needs to be unique within this instance // of your app to allow you to pair responses with requests. String uniqueId = Long.toHexString(sr.nextLong()); JSONObject purchaseRequest = new JSONObject(); purchaseRequest.put("uuid", uniqueId); purchaseRequest.put("identifier", product.getIdentifier()); purchaseRequest.put("testing", "true"); // This value is only needed for testing, not setting it results in a live purchase String purchaseRequestJson = purchaseRequest.toString(); byte[] keyBytes = new byte[16]; sr.nextBytes(keyBytes); SecretKey key = new SecretKeySpec(keyBytes, "AES"); byte[] ivBytes = new byte[16]; sr.nextBytes(ivBytes);/*from www . j a va 2s.c o m*/ IvParameterSpec iv = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, key, iv); byte[] payload = cipher.doFinal(purchaseRequestJson.getBytes("UTF-8")); cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, mPublicKey); byte[] encryptedKey = cipher.doFinal(keyBytes); Purchasable purchasable = new Purchasable(product.getIdentifier(), Base64.encodeToString(encryptedKey, Base64.NO_WRAP), Base64.encodeToString(ivBytes, Base64.NO_WRAP), Base64.encodeToString(payload, Base64.NO_WRAP)); synchronized (mOutstandingPurchaseRequests) { mOutstandingPurchaseRequests.put(uniqueId, product); } ouyaFacade.requestPurchase(purchasable, new PurchaseListener(product)); }
From source file:tv.ouya.sdk.TestOuyaFacade.java
public void requestPurchase(final Product product) throws GeneralSecurityException, UnsupportedEncodingException, JSONException { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); // This is an ID that allows you to associate a successful purchase with // it's original request. The server does nothing with this string except // pass it back to you, so it only needs to be unique within this instance // of your app to allow you to pair responses with requests. String uniqueId = Long.toHexString(sr.nextLong()); JSONObject purchaseRequest = new JSONObject(); purchaseRequest.put("uuid", uniqueId); purchaseRequest.put("identifier", product.getIdentifier()); purchaseRequest.put("testing", "true"); // This value is only needed for testing, not setting it results in a live purchase String purchaseRequestJson = purchaseRequest.toString(); byte[] keyBytes = new byte[16]; sr.nextBytes(keyBytes); SecretKey key = new SecretKeySpec(keyBytes, "AES"); byte[] ivBytes = new byte[16]; sr.nextBytes(ivBytes);/*from w ww.j av a 2 s .c o m*/ IvParameterSpec iv = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, key, iv); byte[] payload = cipher.doFinal(purchaseRequestJson.getBytes("UTF-8")); cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, mPublicKey); byte[] encryptedKey = cipher.doFinal(keyBytes); Purchasable purchasable = new Purchasable(product.getIdentifier(), Base64.encodeToString(encryptedKey, Base64.NO_WRAP), Base64.encodeToString(ivBytes, Base64.NO_WRAP), Base64.encodeToString(payload, Base64.NO_WRAP)); synchronized (mOutstandingPurchaseRequests) { mOutstandingPurchaseRequests.put(uniqueId, product); } //custom-iap-code Log.i("TestOuyaFacade", "TestOuyaFacade.requestPurchase(" + product.getIdentifier() + ")"); UnityPlayer.UnitySendMessage("OuyaGameObject", "DebugLog", "TestOuyaFacade.requestPurchase(" + product.getIdentifier() + ")"); ouyaFacade.requestPurchase(purchasable, new PurchaseListener(product)); }
From source file:com.netscape.cmsutil.crypto.CryptoUtil.java
/** * Generates a nonce_iv for padding./*from ww w . j av a 2 s . co m*/ * * @throws GeneralSecurityException */ public static byte[] getNonceData(int size) throws GeneralSecurityException { byte[] iv = new byte[size]; SecureRandom rnd = CryptoUtil.getRandomNumberGenerator(); rnd.nextBytes(iv); return iv; }
From source file:org.apache.hadoop.hdfs.TestBalancer.java
private void generateFileSystemLoad(long numBlocks, short replication) { String destfile = "hdfs:///user/hadoopqa/";// + BALANCER_TEMP_DIR + "/LOADGEN.DAT"; SecureRandom randgen = new SecureRandom(); ByteArrayOutputStream dat = null; ByteArrayInputStream in = null; final int CHUNK = 4096; final Configuration testConf = new Configuration(hadoopConf); try {/* w ww . jav a 2 s . c o m*/ testConf.setInt("dfs.replication", replication); for (int i = 0; i < numBlocks; i++) { FileSystem fs = FileSystem.get(URI.create(destfile), testConf); OutputStream out = fs.create(new Path(destfile), replication, new ProgressReporter()); dat = new ByteArrayOutputStream(DFS_BLOCK_SIZE); for (int z = 0; z < DFS_BLOCK_SIZE; z += CHUNK) { byte[] bytes = new byte[CHUNK]; randgen.nextBytes(bytes); dat.write(bytes, 0, CHUNK); } in = new ByteArrayInputStream(dat.toByteArray()); IOUtils.copyBytes(in, out, CHUNK, true); LOG.info("wrote block " + (i + 1) + " of " + numBlocks); } } catch (IOException ioExc) { LOG.warn("f/s loadgen failed!", ioExc); } finally { try { dat.close(); } catch (Exception e) { } try { in.close(); } catch (Exception e) { } } }
From source file:tv.ouya.sdk.UnityOuyaFacade.java
public void requestPurchase(final Product product) throws GeneralSecurityException, UnsupportedEncodingException, JSONException { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); // This is an ID that allows you to associate a successful purchase with // it's original request. The server does nothing with this string except // pass it back to you, so it only needs to be unique within this instance // of your app to allow you to pair responses with requests. String uniqueId = Long.toHexString(sr.nextLong()); JSONObject purchaseRequest = new JSONObject(); purchaseRequest.put("uuid", uniqueId); purchaseRequest.put("identifier", product.getIdentifier()); purchaseRequest.put("testing", "true"); // This value is only needed for testing, not setting it results in a live purchase String purchaseRequestJson = purchaseRequest.toString(); byte[] keyBytes = new byte[16]; sr.nextBytes(keyBytes); SecretKey key = new SecretKeySpec(keyBytes, "AES"); byte[] ivBytes = new byte[16]; sr.nextBytes(ivBytes);/*from ww w . ja v a 2 s . c o m*/ IvParameterSpec iv = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, key, iv); byte[] payload = cipher.doFinal(purchaseRequestJson.getBytes("UTF-8")); cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, mPublicKey); byte[] encryptedKey = cipher.doFinal(keyBytes); Purchasable purchasable = new Purchasable(product.getIdentifier(), Base64.encodeToString(encryptedKey, Base64.NO_WRAP), Base64.encodeToString(ivBytes, Base64.NO_WRAP), Base64.encodeToString(payload, Base64.NO_WRAP)); synchronized (mOutstandingPurchaseRequests) { mOutstandingPurchaseRequests.put(uniqueId, product); } //custom-iap-code Log.i(LOG_TAG, "UnityOuyaFacade.requestPurchase(" + product.getIdentifier() + ")"); UnityPlayer.UnitySendMessage("OuyaGameObject", "DebugLog", "UnityOuyaFacade.requestPurchase(" + product.getIdentifier() + ")"); ouyaFacade.requestPurchase(purchasable, new PurchaseListener(product)); }
From source file:com.servoy.j2db.util.Utils.java
/** * Hashes the given string with the PKCS/PBKDF2 algoritme see http://en.wikipedia.org/wiki/PBKDF2 for more information * * @param textString The string to hash/*from w ww. j a v a2 s . co m*/ * @param iterations Number of hash iterations to be done (should be higher then 1000) * @return the hash of the string */ @SuppressWarnings("nls") public static String calculatePBKDF2(String textString, int iterations) { try { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); byte[] salt = new byte[8]; sr.nextBytes(salt); PBKDF2Parameters p = new PBKDF2Parameters("HmacSHA1", "ISO-8859-1", salt, iterations); PBKDF2Engine e = new PBKDF2Engine(p); p.setDerivedKey(e.deriveKey(textString)); return new PBKDF2HexFormatter().toString(p); } catch (NoSuchAlgorithmException e) { Debug.error("No SHA1 algorime found under the name SHA1PRNG", e); } return null; }