Code example for SecureRandom

Methods: generateSeedgetProvidernextBytessetSeed

0
 
    private void test_SecureRandom(SecureRandom sr) throws Exception {
        byte[] out1 = new byte[20];
        byte[] out2 = new byte[20];
 
        sr.nextBytes(out1);
        sr.nextBytes(out2);
        assertFalse(Arrays.equals(out1, out2));
 
        byte[] seed1 = sr.generateSeed(20);
        byte[] seed2 = sr.generateSeed(20);
        assertFalse(Arrays.equals(seed1, seed2));
 
        sr.setSeed(STATIC_SEED_BYTES);
        sr.nextBytes(out1);
        sr.nextBytes(out2);
        assertFalse(Arrays.equals(out1, out2));
 
        sr.setSeed(STATIC_SEED_LONG);
        sr.nextBytes(out1);
        sr.nextBytes(out2);
        assertFalse(Arrays.equals(out1, out2));
    } 
 
    public void testGetCommonInstances_Success() throws Exception { 
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        assertNotNull(sr);
        assertEquals(EXPECTED_PROVIDER, sr.getProvider().getClass());
    } 
 
    public void testNewConstructors_Success() throws Exception { 
        SecureRandom sr1 = new SecureRandom();
        assertEquals(EXPECTED_PROVIDER, sr1.getProvider().getClass());
        test_SecureRandom(sr1);
 
        SecureRandom sr2 = new SecureRandom(STATIC_SEED_BYTES);
        assertEquals(EXPECTED_PROVIDER, sr2.getProvider().getClass());
        test_SecureRandom(sr2);
Stop searching for code, let great code find you!  Add Codota to your java IDE