RandomDataGenerator implements RandomData, Serializable { public String nextSecureHexString(int len) throws NotStrictlyPositiveException { if (len <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.LENGTH, len); } final RandomGenerator secRan = getSecRan(); MessageDigest alg = null; try { alg = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException ex) { throw new MathInternalError(ex); } alg.reset(); int numIter = (len / 40) + 1; StringBuilder outBuffer = new StringBuilder(); for (int iter = 1; iter < numIter + 1; iter++) { byte[] randomBytes = new byte[40]; secRan.nextBytes(randomBytes); alg.update(randomBytes); byte[] hash = alg.digest(); for (int i = 0; i < hash.length; i++) { Integer c = Integer.valueOf(hash[i]); String hex = Integer.toHexString(c.intValue() + 128); if (hex.length() == 1) { hex = "0" + hex; } outBuffer.append(hex); } } return outBuffer.toString().substring(0, len); } RandomDataGenerator();  RandomDataGenerator(RandomGenerator rand); String nextHexString(int len); int nextInt(final int lower, final int upper); long nextLong(final long lower, final long upper); String nextSecureHexString(int len); int nextSecureInt(final int lower, final int upper); long nextSecureLong(final long lower, final long upper); long nextPoisson(double mean); double nextGaussian(double mu, double sigma); double nextExponential(double mean); double nextGamma(double shape, double scale); int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize); int nextPascal(int r, double p); double nextT(double df); double nextWeibull(double shape, double scale); int nextZipf(int numberOfElements, double exponent); double nextBeta(double alpha, double beta); int nextBinomial(int numberOfTrials, double probabilityOfSuccess); double nextCauchy(double median, double scale); double nextChiSquare(double df); double nextF(double numeratorDf, double denominatorDf); double nextUniform(double lower, double upper); double nextUniform(double lower, double upper, boolean lowerInclusive); int[] nextPermutation(int n, int k); Object[] nextSample(Collection<?> c, int k); void reSeed(long seed); void reSeedSecure(); void reSeedSecure(long seed); void reSeed(); void setSecureAlgorithm(String algorithm, String provider); RandomGenerator getRandomGenerator();  }
@Test @Retry(3) public void testNextSecureHex() { try { randomData.nextSecureHexString(-1); Assert.fail("negative length -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { } try { randomData.nextSecureHexString(0); Assert.fail("zero length -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { } String hexString = randomData.nextSecureHexString(3); if (hexString.length() != 3) { Assert.fail("incorrect length for generated string"); } hexString = randomData.nextSecureHexString(1); if (hexString.length() != 1) { Assert.fail("incorrect length for generated string"); } try { hexString = randomData.nextSecureHexString(0); Assert.fail("zero length requested -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { } Frequency f = new Frequency(); for (int i = 0; i < smallSampleSize; i++) { hexString = randomData.nextSecureHexString(100); if (hexString.length() != 100) { Assert.fail("incorrect length for generated string"); } for (int j = 0; j < hexString.length(); j++) { f.addValue(hexString.substring(j, j + 1)); } } double[] expected = new double[16]; long[] observed = new long[16]; for (int i = 0; i < 16; i++) { expected[i] = (double) smallSampleSize * 100 / 16; observed[i] = f.getCount(hex[i]); } TestUtils.assertChiSquareAccept(expected, observed, 0.001); }