/** * Sets the number of random characters that will be appended to the * {@link #createDigest(String) digest}. When it is set to 0, nothing * will be appended to the digest. Otherwise, a random string will be * generated (containing only letters and digits) and appended to the * digest. * <p> * Random digest chars is a <b>unique</b> key per one VM! * This key is initialized only once. * This is useful to automatically expire any cache that browsers may have in * JS and CSS files, so that changes in those files will be downloaded by the * browser. */ public void setRandomDigestChars(final int randomDigestChars) { this.randomDigestChars = randomDigestChars; if (randomDigestChars == 0) { uniqueDigestKey = null; } else { uniqueDigestKey = new RandomString().randomAlphaNumeric(randomDigestChars); } }
/** * Creates random string that consist only of numbers. */ public String randomNumeric(int count) { return random(count, '0', '9'); }
/** * Creates random string of characters. */ public String randomAlpha(int count) { return randomRanges(count, ALPHA_RANGE); }
/** * Generates new CSRF token and puts it in the session. Returns generated token value. */ @SuppressWarnings({"unchecked"}) public static String prepareCsrfToken(final HttpSession session, final int timeToLive) { Set<Token> tokenSet = (Set<Token>) session.getAttribute(CSRF_TOKEN_SET); if (tokenSet == null) { tokenSet = new HashSet<>(); session.setAttribute(CSRF_TOKEN_SET, tokenSet); } String value; boolean unique; do { value = RandomString.get().randomAlphaNumeric(32); assureSize(tokenSet); unique = tokenSet.add(new Token(value, timeToLive)); } while (!unique); return value; }
@Test void testRandom() { int total = 100000; ArrayList<String> randomList = new ArrayList<>(); for (int i = 0; i < total; i++) { randomList.add(RandomString.get().random(20, 'a', 'z')); } SortedArrayList<String> sortedList = new SortedArrayList<>(randomList); Collections.sort(randomList); for (int i = 0; i < total; i++) { assertEquals(randomList.get(i), sortedList.get(i)); } }
@Test void testReadString_with_unknown_path() throws Exception { assertThrows(IOException.class, () -> { PathUtil.readString(new File(BASE_DIR, RandomString.get().randomAlpha(8)).toPath()); }); }
@Test void testNaturalComparatorContract() { NaturalOrderComparator<String> comparator = new NaturalOrderComparator<>(); int loop = 1000; while(loop-- > 0) { String s1 = RandomString.get().randomAscii(2); String s2 = RandomString.get().randomAscii(5); String s3 = RandomString.get().randomAscii(4); assertReflexivity(comparator, s1, s2); assertTransitivity(comparator, s1, s2, s3); } }
@Test void testNaturalComparatorContract2() { NaturalOrderComparator<String> comparator = new NaturalOrderComparator<>(); int loop = 1000; while(loop-- > 0) { String s1 = RandomString.get().randomNumeric(2); String s2 = RandomString.get().randomNumeric(5); String s3 = RandomString.get().randomNumeric(4); assertReflexivity(comparator, s1, s2); assertTransitivity(comparator, s1, s2, s3); } }
@Test void testRepeatableSequence() { RandomString randomString = new RandomString(123); String rnd = randomString.randomAlpha(2); RandomString randomString2 = new RandomString(123); String rnd2 = randomString2.randomAlpha(2); assertEquals(rnd, rnd2); }
@Test void testRandomBase64() { long iter = 10000; RandomString randomString = new RandomString(); while (iter-->0) { String base64 = randomString.randomBase64(10); try { Base64.decodeToString(base64); } catch (Exception e) { fail(e.getMessage()); } } } }
@Test void testRandomAppends() { StringBuilder sb = new StringBuilder(10); FastCharBuffer fcb = new FastCharBuffer(10); Random rnd = new Random(); int loop = 100; while (loop-- > 0) { String s = RandomString.get().randomAlphaNumeric(rnd.nextInt(20)); sb.append(s); fcb.append(s); } assertEquals(sb.toString(), fcb.toString()); }
@Test void testString() { JsonParsers.forEachParser(jsonParser -> { assertEquals("123", jsonParser.parse("\"" + "123" + "\"")); assertEquals("12\n3", jsonParser.parse("\"" + "12\\n3" + "\"")); String big = RandomString.get().randomAlpha(510); String jbig = big + "\\n"; String rbig = big + "\n"; assertEquals(512, jbig.length()); assertEquals(rbig, jsonParser.parse("\"" + jbig + "\"")); jbig += "x"; rbig += "x"; assertEquals(rbig, jsonParser.parse("\"" + jbig + "\"")); jbig = "12" + jbig; rbig = "12" + rbig; assertEquals(rbig, jsonParser.parse("\"" + jbig + "\"")); }); }
@Test void testLoop() { long reps = 10000; while (reps-- > 0) { String s = RandomString.get().randomAscii(MathUtil.randomInt(1, 1024)); byte[] encrypted = threefish.encryptString(s); String s2 = threefish.decryptString(encrypted); assertEquals(s, s2); } } }
@Test void testBCryptRandom() { for (int rounds = 0; rounds < 1000; rounds++) { String text = RandomString.get().randomAlphaNumeric(10); String hash = BCrypt.hashpw(text, BCrypt.gensalt(4)); assertTrue(BCrypt.checkpw(text, hash)); } }
/** * Creates random string that contains only Base64 characters. */ public String randomBase64(int count) { return random(count, Base64.CHARS); }
/** * Sets the number of random characters that will be appended to the * {@link #createDigest(String) digest}. When it is set to 0, nothing * will be appended to the digest. Otherwise, a random string will be * generated (containing only letters and digits) and appended to the * digest. * <p> * Random digest chars is a <b>unique</b> key per one VM! * This key is initialized only once. * This is useful to automatically expire any cache that browsers may have in * JS and CSS files, so that changes in those files will be downloaded by the * browser. */ public void setRandomDigestChars(final int randomDigestChars) { this.randomDigestChars = randomDigestChars; if (randomDigestChars == 0) { uniqueDigestKey = null; } else { uniqueDigestKey = new RandomString().randomAlphaNumeric(randomDigestChars); } }
/** * Creates random string of characters and digits. */ public String randomAlphaNumeric(int count) { return randomRanges(count, ALPHA_NUMERIC_RANGE); }
String boundary = StringUtil.repeat('-', 10) + RandomString.get().randomAlphaNumeric(10);
/** * Creates random string whose length is the number of characters specified. * Characters are chosen from the set of characters whose * ASCII value is between <code>32</code> and <code>126</code> (inclusive). */ public String randomAscii(int count) { return random(count, (char) 32, (char) 126); }
/** * Creates random string of characters. */ public String randomAlpha(final int count) { return randomRanges(count, ALPHA_RANGE); }