List of usage examples for java.security SecureRandom nextInt
public int nextInt(int bound)
From source file:net.firejack.platform.core.utils.SecurityHelper.java
/** * generate random sequence// w w w . jav a 2 s. c o m * * @param length lengh of sequence to generate * @return Generated random sequence */ public static String generateRandomSequence(int length) { String password = ""; try { SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); for (int i = 0; i < length; i++) { char ch = 0; int j = random.nextInt(3); switch (j) { case 0: ch = (char) (97 + random.nextInt(26)); break; case 1: ch = (char) (65 + random.nextInt(26)); break; case 2: ch = (char) (48 + random.nextInt(10)); break; } password += ch; } } catch (NoSuchAlgorithmException e) { LOGGER.info("Couldn't create new instance of SecureRandom class", e); throw new RuntimeException(e); } return password; }
From source file:org.wso2.carbon.identity.authenticator.smsotp.OneTimePassword.java
/** * * @param size digit size of the number/*from www.ja v a2 s .c o m*/ * @return token */ public static String getRandomNumber(int size) { StringBuilder generatedToken = new StringBuilder(); try { SecureRandom number = SecureRandom.getInstance(SMSOTPConstants.ALGORITHM_NAME); // Generate 20 integers 0..20 for (int i = 0; i < size; i++) { generatedToken.append(number.nextInt(9)); } } catch (NoSuchAlgorithmException e) { log.error("Unable to find the Algorithm", e); } return generatedToken.toString(); }
From source file:org.wso2.carbon.identity.authenticator.emailotp.OneTimePassword.java
public static String getRandomNumber(int size) { StringBuilder generatedToken = new StringBuilder(); try {/*from w w w .ja v a 2s . c om*/ SecureRandom number = SecureRandom.getInstance(EmailOTPAuthenticatorConstants.ALGORITHM_NAME); // Generate 20 integers 0..20 for (int i = 0; i < size; i++) { generatedToken.append(number.nextInt(9)); } } catch (NoSuchAlgorithmException e) { log.error("Unable to find the Algorithm", e); } return generatedToken.toString(); }
From source file:RandomUtil.java
/** * Generates a secure random word with the given length. * /*from w w w . j av a 2s . c om*/ * @param len Amount of random characters to generate * @param alphabet Alphabet to generate from. * @return random Word containing letters and numbers. */ public static String createWord(int len, char[] alphabet) { SecureRandom random = createSecureRandom(); if (alphabet == null) { alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890".toCharArray(); } StringBuffer out = new StringBuffer(len); for (int i = 0; i < len; i++) { out.append(alphabet[random.nextInt(alphabet.length)]); } return out.toString(); }
From source file:jp.go.nict.langrid.management.web.utility.StringUtil.java
/** * // www .j a v a 2 s. com * */ public static String randomPassword(int size) { SecureRandom random = new SecureRandom(); char[] pass = new char[size]; for (int k = 0; k < pass.length; k++) { switch (random.nextInt(3)) { case 0: // 'a' - 'z' pass[k] = (char) (97 + random.nextInt(26)); break; case 1: // 'A' - 'Z' pass[k] = (char) (65 + random.nextInt(26)); break; case 2: // '0' - '9' pass[k] = (char) (48 + random.nextInt(10)); break; default: pass[k] = 'a'; } } return new String(pass); }
From source file:org.eclipse.om2m.core.controller.Controller.java
/** * Generates an random ID based on SecureRandom library * @param prefix - prefix of the resource ID * @param postfix - postfix of the resource ID * @return generated resource ID//from w w w .j ava 2 s . co m */ public static String generateId(String prefix, String postfix) { SecureRandom secureRandom = new SecureRandom(); return prefix + String.valueOf(secureRandom.nextInt(999999999)) + postfix; }
From source file:org.jahia.params.valves.CookieAuthValveImpl.java
public static String generateRandomString(int length) { SecureRandom randomGen = new SecureRandom(); StringBuilder result = new StringBuilder(); int count = 0; while (count < length) { int randomSel = randomGen.nextInt(3); int randomInt = randomGen.nextInt(26); char randomChar = '0'; switch (randomSel) { case 0:// w ww . j a va2 s. c o m randomChar = (char) (((int) 'A') + randomInt); break; case 1: randomChar = (char) (((int) 'a') + randomInt); break; case 2: randomChar = (char) (((int) '0') + (randomInt % 10)); break; } result.append(randomChar); count++; } return result.toString(); }
From source file:burrito.util.StringUtils.java
/** * Genererar en random strng//from w w w . ja v a2 s .c om * @param length * @return */ public static String generateRandomString(int length) { try { SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); char[] chars = new char[length]; for (int i = 0; i < length; i++) { chars[i] = randomStringAlphabet[random.nextInt(randomStringAlphabet.length)]; } return String.valueOf(chars); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
From source file:io.stallion.utils.GeneralUtils.java
public static String secureRandomNumeric(int length) { SecureRandom test = new SecureRandom(); int result = test.nextInt(((Double) Math.pow(10, length)).intValue()); String resultStr = result + ""; if (resultStr.length() != length) for (int x = resultStr.length(); x < length; x++) resultStr = "0" + resultStr; return resultStr; }
From source file:com.zimbra.common.util.RandomPassword.java
public static String generate(int minLength, int maxLength, String alphabet) { SecureRandom random = new SecureRandom(); // Calculate the desired length of the password int length;/*from w w w . ja v a 2s . c o m*/ if (minLength > maxLength) { throw new IllegalArgumentException("minLength=" + minLength + " > maxLength=" + maxLength); } else if (minLength < maxLength) { length = minLength + random.nextInt(1 + maxLength - minLength); } else { length = maxLength; } int alphabetLength = alphabet.length(); int limit = byteLimit(alphabetLength); StringBuffer password = new StringBuffer(length); byte[] randomByte = new byte[1]; while (password.length() < length) { random.nextBytes(randomByte); int i = randomByte[0] + 128; if (i < limit) { password.append(alphabet.charAt(i % alphabetLength)); } } return password.toString(); }