List of usage examples for java.security SecureRandom nextInt
public int nextInt(int bound)
From source file:com.ntsync.android.sync.client.ClientKeyHelper.java
private static byte[] createPwdCheck(SecretKey skey) throws InvalidKeyException, UnsupportedEncodingException { byte[] iv = new byte[IV_LENGTH]; SecureRandom random = new SecureRandom(); random.nextBytes(iv);//from ww w.ja v a 2 s . c om AEADBlockCipher ecipher = CryptoHelper.getCipher(); byte[] checkData; try { ecipher.init(true, new AEADParameters(new KeyParameter(skey.getEncoded()), CryptoHelper.MAC_SIZE, iv)); // create random integer with checksum (UPC-Format : 12 digits) String testValue = String.format("%011d", random.nextInt(Integer.MAX_VALUE)) + "0"; int res1 = calcUpcChecksum(testValue); testValue = testValue.substring(0, UPC_NR_LEN) + res1; byte[] pwdCheck = CryptoHelper.cipherData(ecipher, testValue.getBytes(SyncDataHelper.DEFAULT_CHARSET_NAME)); checkData = new byte[iv.length + pwdCheck.length]; System.arraycopy(iv, 0, checkData, 0, iv.length); System.arraycopy(pwdCheck, 0, checkData, iv.length, pwdCheck.length); } catch (DataLengthException e) { throw new InvalidKeyException(e.getMessage(), e); } catch (IllegalStateException e) { throw new InvalidKeyException(e.getMessage(), e); } catch (InvalidCipherTextException e) { throw new InvalidKeyException(e.getMessage(), e); } return checkData; }
From source file:com.owncloud.android.utils.EncryptionUtils.java
public static ArrayList<String> getRandomWords(int count, Context context) throws IOException { InputStream ins = context.getResources().openRawResource( context.getResources().getIdentifier("encryption_key_words", "raw", context.getPackageName())); InputStreamReader inputStreamReader = new InputStreamReader(ins); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); ArrayList<String> lines = new ArrayList<>(); String line;/*from w ww .j a va 2 s . co m*/ while ((line = bufferedReader.readLine()) != null) { lines.add(line); } SecureRandom random = new SecureRandom(); ArrayList<String> outputLines = new ArrayList<>(); for (int i = 0; i < count; i++) { int randomLine = random.nextInt(lines.size()); outputLines.add(lines.get(randomLine)); } return outputLines; }
From source file:egovframework.asadal.asapro.com.cmm.util.AsaproEgovStringUtil.java
License:asdf
/** * ? A? Z?? ? ?? ? ? ? ?? ? ?? //from w w w .j av a2 s .co m * * @param startChr * - ? * @param endChr * - ? * @return ?? * @exception MyException * @see */ public static String getRandomStr(char startChr, char endChr) { int randomInt; String randomStr = null; // ? ? ? ? . int startInt = Integer.valueOf(startChr); int endInt = Integer.valueOf(endChr); // ?? ? ? if (startInt > endInt) { throw new IllegalArgumentException("Start String: " + startChr + " End String: " + endChr); } try { // ? ? ? SecureRandom rnd = new SecureRandom(); do { // ? ? ? ? ? ? ?. randomInt = rnd.nextInt(endInt + 1); } while (randomInt < startInt); // ? ? 'A'(65) ? ? ?. // ? ? ? ? randomStr = (char) randomInt + ""; } catch (Exception e) { log.error(e.getMessage()); } // ?? return randomStr; }
From source file:net.sf.gazpachoquest.util.impl.RandomTokenGeneratorImpl.java
@Override public String generate(int length) { StringBuilder token = new StringBuilder(); SecureRandom random = new SecureRandom(); for (int i = 0; i < length; i++) { int pos = random.nextInt(ALPHABET_LENGTH); token.append(ALPHABET[pos]);//from w ww. j a v a 2 s . co m } return token.toString(); }
From source file:org.wso2.carbon.user.core.util.UserCoreUtil.java
/** * This method generates a random password that adhere to most of the password policies defined * by various LDAPs such as AD, ApacheDS 2.0 etc * * @param username/* w ww. j a v a2 s .c o m*/ * @param length * @return password * @throws UserStoreException */ public static String getPolicyFriendlyRandomPassword(String username, int length) throws UserStoreException { if (length < 8 || length > 50) { length = 12; } // Avoiding admin, administrator, root, wso2, carbon to be a password char[] chars = { 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'N', 'P', 'Q', 'U', 'V', 'W', 'X', 'Y', 'Z', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'n', 'p', 'q', 'u', 'v', 'w', 'x', 'y', 'z', '~', '!', '@', '#', '$', '%', '^', '&', '*', '_', '-', '+', '=', }; char[] invalidChars = username.toCharArray(); StringBuffer passwordFeed = new StringBuffer(); // now we are going filter characters in the username for (char invalidCha : invalidChars) { for (char cha : chars) { if (cha != invalidCha) passwordFeed.append(cha); } } // the password generation String passwordChars = passwordFeed.toString(); char[] password = new char[length]; String randomNum = null; try { // the secure random SecureRandom prng = SecureRandom.getInstance("SHA1PRNG"); for (int i = 0; i < length; i++) { password[i] = passwordChars.charAt(prng.nextInt(passwordFeed.length())); } randomNum = new Integer(prng.nextInt()).toString(); } catch (NoSuchAlgorithmException e) { String errorMessage = "Error while creating the random password for user : " + username; if (log.isDebugEnabled()) { log.debug(errorMessage, e); } throw new UserStoreException(errorMessage, e); } return new String(password).concat(randomNum); }
From source file:org.apache.cordova.CordovaBridge.java
/** Called by cordova.js to initialize the bridge. */ int generateBridgeSecret() { SecureRandom randGen = new SecureRandom(); expectedBridgeSecret = randGen.nextInt(Integer.MAX_VALUE); return expectedBridgeSecret; }
From source file:register.register_servlet.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from www.j a va2 s . c o m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ public String generateToken(int len) { String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; SecureRandom rnd = new SecureRandom(); StringBuilder sb = new StringBuilder(len); for (int i = 0; i < len; i++) sb.append(AB.charAt(rnd.nextInt(AB.length()))); return sb.toString(); }
From source file:org.wso2.carbon.identity.provider.openid.OpenIDServerAssociationStore.java
/** * Here we instantiate a DAO to access the identity database. * * @param dbConnection/*from w w w . j av a2 s. co m*/ * @param privateAssociations if this association store stores private associations */ public OpenIDServerAssociationStore(String associationsType) { try { SecureRandom secureRandom = SecureRandom.getInstance(SHA_1_PRNG); storeId = secureRandom.nextInt(9999); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("SHA1PRNG algorithm could not be found."); } timestamp = Long.toString(new Date().getTime()); counter = 0; cache = OpenIDAssociationCache.getCacheInstance(); // get singleton dao dao = OpenIDAssociationDAO.getInstance(associationsType); }
From source file:login.login_servlet.java
public String generateToken(int len) { String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; SecureRandom rnd = new SecureRandom(); StringBuilder sb = new StringBuilder(len); for (int i = 0; i < len; i++) sb.append(AB.charAt(rnd.nextInt(AB.length()))); return sb.toString(); }
From source file:egovframework.oe1.utl.fcc.service.EgovStringUtil.java
License:asdf
/** * ? A? Z?? ? ?? ? ? ? ??//from w ww . jav a 2 s . c o m * ? ?? * @param startChr * - ? * @param endChr * - ? * @return ?? * @exception MyException * @see */ public static String getRandomStr(char startChr, char endChr) { int randomInt; String randomStr = null; // ? ? ? ? . int startInt = Integer.valueOf(startChr); int endInt = Integer.valueOf(endChr); // ?? ? ? if (startInt > endInt) { throw new IllegalArgumentException("Start String: " + startChr + " End String: " + endChr); } try { // ? ? ? SecureRandom rnd = new SecureRandom(); do { // ? ? ? ? ? ? ?. randomInt = rnd.nextInt(endInt + 1); } while (randomInt < startInt); // ? ? // 'A'(65) // // ? ? // ?. // ? ? ? ? randomStr = (char) randomInt + ""; } catch (Exception e) { log.debug(e.getMessage()); } // ?? return randomStr; }