List of usage examples for java.security SecureRandom nextInt
public int nextInt(int bound)
From source file:org.owasp.benchmark.score.BenchmarkScore.java
public static void main(String[] args) { if (args == null || args.length < 2) { System.out.println(usageNotice); System.exit(-1);//from w ww . j a v a2s .c om } if (args.length > 2) { focus = args[2].replace(' ', '_'); } if (args.length > 3) { if ("anonymous".equalsIgnoreCase(args[3])) { anonymousMode = true; } else if ("show_ave_only".equalsIgnoreCase(args[3])) { showAveOnlyMode = true; } else { System.out.println(usageNotice); System.exit(-1); } } // Prepare the scorecard results directory for the newly generated scorecards // Step 1: Create the dir if it doesn't exist, or delete everything in it if it does File scoreCardDir = new File(scoreCardDirName); try { if (!scoreCardDir.exists()) { Files.createDirectories(Paths.get(scoreCardDirName)); } else { System.out.println( "Deleting previously generated scorecard files in: " + scoreCardDir.getAbsolutePath()); FileUtils.cleanDirectory(scoreCardDir); } // Step 2: Now copy the entire /content directory, that either didn't exist, or was just deleted with everything else File dest1 = new File(scoreCardDirName + File.separator + "content"); FileUtils.copyDirectory(new File(pathToScorecardResources + "content"), dest1); } catch (IOException e) { System.out.println("Error dealing with scorecard directory: '" + scoreCardDir.getAbsolutePath() + "' for some reason!"); e.printStackTrace(); } // Step 3: Copy over the Homepage and Guide templates try { Files.copy(Paths.get(pathToScorecardResources + HOMEFILENAME), Paths.get(scoreCardDirName + "/" + HOMEFILENAME), StandardCopyOption.REPLACE_EXISTING); Files.copy(Paths.get(pathToScorecardResources + GUIDEFILENAME), Paths.get(scoreCardDirName + "/" + GUIDEFILENAME), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { System.out.println("Problem copying home and guide files"); e.printStackTrace(); } // Step 4: Read the expected results so we know what each tool 'should do' try { if ("mixed".equalsIgnoreCase(args[0])) { mixedMode = true; // Tells anyone that cares that we aren't processing a single version of Benchmark results File f = new File(args[1]); if (!f.exists()) { System.out.println("Error! - results directory: '" + f.getAbsolutePath() + "' doesn't exist."); System.exit(-1); } if (!f.isDirectory()) { System.out.println("Error! - results parameter is a file: '" + f.getAbsolutePath() + "' but must be a directory when processing results in 'mixed' mode."); System.exit(-1); } // Go through each file in the root directory. // -- 1st find each directory. And then within each of those directories: // -- 1st find the expected results file in that directory // -- and then each of the actual results files in that directory for (File rootDirFile : f.listFiles()) { if (rootDirFile.isDirectory()) { // Process this directory TestResults expectedResults = null; String expectedResultsFilename = null; // Step 4a: Find and process the expected results file so we know what each tool in this directory 'should do' for (File resultsDirFile : rootDirFile.listFiles()) { if (resultsDirFile.getName().startsWith("expectedresults-")) { if (expectedResults != null) { System.out.println("Found 2nd expected results file " + resultsDirFile.getAbsolutePath() + " in same directory. Can only have 1 in each results directory"); System.exit(-1); } // read in the expected results for this directory of results expectedResults = readExpectedResults(resultsDirFile); if (expectedResults == null) { System.out.println("Couldn't read expected results file: " + resultsDirFile.getAbsolutePath()); System.exit(-1); } // end if expectedResultsFilename = resultsDirFile.getName(); if (benchmarkVersion == null) { benchmarkVersion = expectedResults.getBenchmarkVersion(); } else benchmarkVersion += "," + expectedResults.getBenchmarkVersion(); System.out.println( "\nFound expected results file: " + resultsDirFile.getAbsolutePath()); } // end if } // end for loop going through each file looking for expected results file // Make sure we found an expected results file, before processing the results if (expectedResults == null) { System.out.println("Couldn't find expected results file in results directory: " + rootDirFile.getAbsolutePath()); System.out.println( "Expected results file has to be a .csv file that starts with: 'expectedresults-'"); System.exit(-1); } // Step 5a: Go through each result file and generate a scorecard for that tool. if (!anonymousMode) { for (File actual : rootDirFile.listFiles()) { // Don't confuse the expected results file as an actual results file if its in the same directory if (!actual.isDirectory() && !expectedResultsFilename.equals(actual.getName())) process(actual, expectedResults, toolResults); } } else { // To handle anonymous mode, we are going to randomly grab files out of this directory // and process them. By doing it this way, multiple runs should randomly order the commercial // tools each time. List<File> files = new ArrayList(); for (File file : rootDirFile.listFiles()) { files.add(file); } SecureRandom generator = SecureRandom.getInstance("SHA1PRNG"); while (files.size() > 0) { // Get a random, positive integer int fileToGet = Math.abs(generator.nextInt(files.size())); File actual = files.remove(fileToGet); // Don't confuse the expected results file as an actual results file if its in the same directory if (!actual.isDirectory() && !expectedResultsFilename.equals(actual.getName())) process(actual, expectedResults, toolResults); } } } // end if a directory } // end for loop through all files in the directory // process the results the normal way with a single results directory } else { // Step 4b: Read the expected results so we know what each tool 'should do' File expected = new File(args[0]); TestResults expectedResults = readExpectedResults(expected); if (expectedResults == null) { System.out.println("Couldn't read expected results file: " + expected); System.exit(-1); } else { System.out.println("Read expected results from file: " + expected.getAbsolutePath()); int totalResults = expectedResults.totalResults(); if (totalResults != 0) { System.out.println(totalResults + " results found."); benchmarkVersion = expectedResults.getBenchmarkVersion(); } else { System.out.println("Error! - zero expected results found in results file."); System.exit(-1); } } // Step 5b: Go through each result file and generate a scorecard for that tool. File f = new File(args[1]); if (!f.exists()) { System.out.println("Error! - results file: '" + f.getAbsolutePath() + "' doesn't exist."); System.exit(-1); } if (f.isDirectory()) { if (!anonymousMode) { for (File actual : f.listFiles()) { // Don't confuse the expected results file as an actual results file if its in the same directory if (!actual.isDirectory() && !expected.getName().equals(actual.getName())) process(actual, expectedResults, toolResults); } } else { // To handle anonymous mode, we are going to randomly grab files out of this directory // and process them. By doing it this way, multiple runs should randomly order the commercial // tools each time. List<File> files = new ArrayList(); for (File file : f.listFiles()) { files.add(file); } SecureRandom generator = SecureRandom.getInstance("SHA1PRNG"); while (files.size() > 0) { int randomNum = generator.nextInt(); // FIXME: Get Absolute Value better if (randomNum < 0) randomNum *= -1; int fileToGet = randomNum % files.size(); File actual = files.remove(fileToGet); // Don't confuse the expected results file as an actual results file if its in the same directory if (!actual.isDirectory() && !expected.getName().equals(actual.getName())) process(actual, expectedResults, toolResults); } } } else { // This will process a single results file, if that is what the 2nd parameter points to. // This has never been used. process(f, expectedResults, toolResults); } } // end else System.out.println("Tool scorecards computed."); } catch (Exception e) { System.out.println("Error during processing: " + e.getMessage()); e.printStackTrace(); } // Step 6: Now generate scorecards for each type of vulnerability across all the tools // First, we have to figure out the list of vulnerabilities // A set is used here to eliminate duplicate categories across all the results Set<String> catSet = new TreeSet<String>(); for (Report toolReport : toolResults) { catSet.addAll(toolReport.getOverallResults().getCategories()); } // Then we generate each vulnerability scorecard BenchmarkScore.generateVulnerabilityScorecards(toolResults, catSet); System.out.println("Vulnerability scorecards computed."); // Step 7: Update all the menus for all the generated pages to reflect the tools and vulnerability categories updateMenus(toolResults, catSet); // Step 8: Generate the overall comparison chart for all the tools in this test ScatterHome.generateComparisonChart(toolResults, focus); // Step 9: Generate the results table across all the tools in this test String table = generateOverallStatsTable(toolResults); File f = Paths.get(scoreCardDirName + "/" + HOMEFILENAME).toFile(); try { String html = new String(Files.readAllBytes(f.toPath())); html = html.replace("${table}", table); Files.write(f.toPath(), html.getBytes()); } catch (IOException e) { System.out.println("Error updating results table in: " + f.getName()); e.printStackTrace(); } System.out.println("Benchmark scorecards complete."); System.exit(0); }
From source file:Main.java
public static String getTxRef() { String ints = "0123456789"; SecureRandom rnd = new SecureRandom(); StringBuilder sb = new StringBuilder("rave-checkout-"); for (int i = 0; i < 10; i++) { sb.append(ints.charAt(rnd.nextInt(ints.length()))); }/*ww w .ja v a2 s. c om*/ return sb.toString(); }
From source file:nu.yona.server.crypto.CryptoUtil.java
public static String getRandomDigits(int length) { SecureRandom random = CryptoUtil.getSecureRandomInstance(); return StringUtils.leftPad(Integer.toString(random.nextInt((int) Math.pow(10, length))), length, '0'); }
From source file:Main.java
private static String randomWord(int lenght, SecureRandom random) { StringBuilder builder = new StringBuilder(lenght); for (int i = 0; i < lenght; ++i) { if (i % 2 == 0) { builder.append(consonants[random.nextInt(consonants.length)]); } else {/* w w w.ja v a 2 s . c o m*/ builder.append(vowels[random.nextInt(vowels.length)]); } } return builder.toString(); }
From source file:com.billing.ng.crypto.util.HashUtils.java
/** * Generates a string of random alphanumeric characters of the given length. The generated salt can be * used to add entropy to a hashing algorithm or token generator, thus making the result harder to crack. * * @param length length of salt string to generate * @return salt string/*from ww w.j a v a2 s . co m*/ */ public static String generateHashSalt(int length) { SecureRandom random = new SecureRandom(); StringBuffer salt = new StringBuffer(length); for (int i = 0; i < length; i++) salt.append(chars[random.nextInt(chars.length)]); return salt.toString(); }
From source file:edu.stanford.mobisocial.dungbeetle.model.Feed.java
public static int colorFor(String name) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {/* w w w . j a v a 2s. co m*/ bos.write(name.getBytes()); } catch (IOException e) { } SecureRandom r = new SecureRandom(bos.toByteArray()); float hsv[] = new float[] { baseHues[r.nextInt(baseHues.length)], r.nextFloat(), r.nextFloat() }; hsv[0] = hsv[0] + 20 * r.nextFloat() - 10; hsv[1] = hsv[1] * 0.2f + 0.8f; hsv[2] = hsv[2] * 0.2f + 0.8f; return Color.HSVToColor(hsv); }
From source file:Main.java
/** * Generate a random string (used for database encryption) * * @return a random string of 64 characters *//*from w ww .ja va 2 s . c o m*/ public static String generateRandomString() { SecureRandom generator = new SecureRandom(); StringBuilder randomStringBuilder = new StringBuilder(); char tempChar; int numbChar = 0; while (numbChar < SEED_LENGTH) { tempChar = (char) (generator.nextInt(SEED_SPACE) + 32); boolean num = tempChar >= 48 && tempChar < 58;//0-9 boolean cap = tempChar >= 65 && tempChar < 91;//A-Z boolean lower = tempChar >= 65 && tempChar < 91;//a-z if (num || cap || lower) { randomStringBuilder.append(tempChar); numbChar++; } } return randomStringBuilder.toString(); }
From source file:org.glite.slcs.caclient.impl.CMPRequest.java
private static int makeRandomInt(int digits) { SecureRandom random = new SecureRandom(); return random.nextInt(digits); }
From source file:realestate.utils.StringUtils.java
/** * random ra mot chuoi ngau nhien//from w w w . j a va 2 s . c om * * @param length * chieu dai cua chuoi se generate * @return string */ public static String randomString(int length) { SecureRandom rnd = new SecureRandom(); StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { sb.append(ValueConstants.ALPHABET.charAt(rnd.nextInt(ValueConstants.ALPHABET.length()))); } return sb.toString(); }
From source file:com.pingidentity.adapters.idp.mobileid.restservice.MssRequestHandlerRest.java
/** * Creates a random transaction id beginning with 'pf' * //from w w w . ja va2 s .c o m * @param digits * number of digits without 'pf' * @return the generated transaction id */ public static String createTransId(int digits) { final String VALUES = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; SecureRandom rand = new SecureRandom(); rand.setSeed(System.currentTimeMillis()); StringBuffer randBuffer = new StringBuffer("pf"); for (int i = 0; i < digits; i++) { randBuffer.append(VALUES.charAt(rand.nextInt(VALUES.length()))); } return randBuffer.toString(); }