TraceAndSpanIdGenerator { protected static Random getRandomInstance(String desiredSecureRandomImplementation) { Random randomToUse; try { randomToUse = SecureRandom.getInstance(desiredSecureRandomImplementation); randomToUse.setSeed(System.nanoTime()); } catch (NoSuchAlgorithmException e) { logger.error("Unable to retrieve the {} SecureRandom instance. Defaulting to a new Random(System.nanoTime()) instead. NOTE: This means random longs will not cover " + "the full 64 bits of possible values! See the javadocs for Random.nextLong() for details. dtracer_error=true", desiredSecureRandomImplementation, e); randomToUse = new Random(System.nanoTime()); } return randomToUse; } private  TraceAndSpanIdGenerator(); static String generateId(); static long generate64BitRandomLong(); static String longToUnsignedLowerHexString(long primitiveLong); static long unsignedLowerHexStringToLong(String hexString); static long unsignedLowerHexStringToLong(String hexString, int index);  }
@Test public void getRandomInstance_should_return_requested_instance_if_available() { Provider[] availableProviders = Security.getProviders(); assertThat(availableProviders).isNotNull(); assertThat(availableProviders.length).isGreaterThanOrEqualTo(1); List<String> excludedAlgorithms = Collections.singletonList("NativePRNGNonBlocking"); Set<String> availableSecureRandomAlgorithms = new HashSet<>(); for (Provider provider : availableProviders) { Set<Provider.Service> services = provider.getServices(); for (Provider.Service service : services) { String type = service.getType(); if ("SecureRandom".equals(type) && !excludedAlgorithms.contains(service.getAlgorithm())) availableSecureRandomAlgorithms.add(service.getAlgorithm()); } } assertThat(availableSecureRandomAlgorithms).isNotEmpty(); for (String algorithm : availableSecureRandomAlgorithms) { Random randomGenerator = TraceAndSpanIdGenerator.getRandomInstance(algorithm); assertThat(randomGenerator).isNotNull(); assertThat(randomGenerator).isInstanceOf(SecureRandom.class); assertThat(((SecureRandom)randomGenerator).getAlgorithm()).isEqualTo(algorithm); } }
@Test public void getRandomInstance_should_default_to_normal_random_if_desired_algorithm_is_not_available() { Random randomGenerator = TraceAndSpanIdGenerator.getRandomInstance("QuantumDoohickey"); assertThat(randomGenerator).isNotNull(); assertThat(randomGenerator).isNotInstanceOf(SecureRandom.class); }