Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.crypto.prng;
  
Random generation based on the digest with counter. Calling addSeedMaterial will always increase the entropy of the hash.

Internal access to the digest is synchronized so a single one of these can be shared.

 
 public class DigestRandomGenerator
     implements RandomGenerator
 {
     private static long         CYCLE_COUNT = 10;
 
     private long                stateCounter;
     private long                seedCounter;
     private Digest              digest;
     private byte[]              state;
     private byte[]              seed;
 
     // public constructors
     public DigestRandomGenerator(
         Digest digest)
     {
         this. = digest;
 
         this. = new byte[digest.getDigestSize()];
         this. = 1;
 
         this. = new byte[digest.getDigestSize()];
         this. = 1;
     }
 
     public void addSeedMaterial(byte[] inSeed)
     {
         synchronized (this)
         {
             digestUpdate(inSeed);
             digestUpdate();
             digestDoFinal();
         }
     }
 
     public void addSeedMaterial(long rSeed)
     {
         synchronized (this)
         {
             digestAddCounter(rSeed);
             digestUpdate();
 
             digestDoFinal();
         }
     }
 
     public void nextBytes(byte[] bytes)
     {
         nextBytes(bytes, 0, bytes.length);
     }
 
     public void nextBytes(byte[] bytesint startint len)
     {
         synchronized (this)
         {
             int stateOff = 0;
 
             generateState();
 
             int end = start + len;
             for (int i = starti != endi++)
             {
                 if (stateOff == .)
                 {
                     generateState();
                     stateOff = 0;
                 }
                 bytes[i] = [stateOff++];
             }
         }
     }
 
     private void cycleSeed()
     {
         digestUpdate();
         digestAddCounter(++);
 
         digestDoFinal();
     }
 
     private void generateState()
     {
         digestAddCounter(++);
         digestUpdate();
         digestUpdate();
 
         digestDoFinal();
 
         if (( % ) == 0)
        {
            cycleSeed();
        }
    }
    private void digestAddCounter(long seed)
    {
        for (int i = 0; i != 8; i++)
        {
            .update((byte)seed);
            seed >>>= 8;
        }
    }
    private void digestUpdate(byte[] inSeed)
    {
        .update(inSeed, 0, inSeed.length);
    }
    private void digestDoFinal(byte[] result)
    {
        .doFinal(result, 0);
    }
New to GrepCode? Check out our FAQ X