Author: toad
Date: 2008-06-28 14:49:23 +0000 (Sat, 28 Jun 2008)
New Revision: 20845

Modified:
   
branches/db4o/freenet/src/freenet/support/io/PaddedEphemerallyEncryptedBucket.java
Log:
Don't store the RNG here. Just store a seed created on startup.

Modified: 
branches/db4o/freenet/src/freenet/support/io/PaddedEphemerallyEncryptedBucket.java
===================================================================
--- 
branches/db4o/freenet/src/freenet/support/io/PaddedEphemerallyEncryptedBucket.java
  2008-06-28 14:48:55 UTC (rev 20844)
+++ 
branches/db4o/freenet/src/freenet/support/io/PaddedEphemerallyEncryptedBucket.java
  2008-06-28 14:49:23 UTC (rev 20845)
@@ -18,6 +18,8 @@
 import freenet.support.api.Bucket;
 import java.util.Random;

+import org.spaceroots.mantissa.random.MersenneTwister;
+
 import com.db4o.ObjectContainer;

 /**
@@ -29,10 +31,10 @@

        private final Bucket bucket;
        private final int minPaddedSize;
-       private final Random randomSource;
        private transient SoftReference /* <Rijndael> */ aesRef;
        /** The decryption key. */
        private final byte[] key;
+       private final byte[] randomSeed;
        private long dataLength;
        private boolean readOnly;
        private int lastOutputStream;
@@ -49,10 +51,11 @@
         * @throws UnsupportedCipherException 
         */
        public PaddedEphemerallyEncryptedBucket(Bucket bucket, int minSize, 
RandomSource strongPRNG, Random weakPRNG) {
-               this.randomSource = weakPRNG;
                this.bucket = bucket;
                if(bucket.size() != 0) throw new 
IllegalArgumentException("Bucket must be empty");
                byte[] tempKey = new byte[32];
+               randomSeed = new byte[32];
+               weakPRNG.nextBytes(randomSeed);
                strongPRNG.nextBytes(tempKey);
                this.key = tempKey;
                this.minPaddedSize = minSize;
@@ -76,9 +79,10 @@
                if(bucket.size() < knownSize)
                        throw new IOException("Bucket "+bucket+" is too small 
on disk - knownSize="+knownSize+" but bucket.size="+bucket.size()+" for 
"+bucket);
                this.dataLength = knownSize;
-               this.randomSource = origRandom;
                this.bucket = bucket;
                if(key.length != 32) throw new IllegalArgumentException("Key 
wrong length: "+key.length);
+               randomSeed = new byte[32];
+               origRandom.nextBytes(randomSeed);
                this.key = key;
                this.minPaddedSize = minSize;
                readOnly = false;
@@ -86,7 +90,6 @@
        }

        public PaddedEphemerallyEncryptedBucket(SimpleFieldSet fs, RandomSource 
origRandom, PersistentFileTracker f) throws CannotCreateFromFieldSetException {
-               this.randomSource = origRandom;
                String tmp = fs.get("DataLength");
                if(tmp == null)
                        throw new CannotCreateFromFieldSetException("No 
DataLength");
@@ -116,6 +119,8 @@
                }
                if(dataLength > bucket.size())
                        throw new CannotCreateFromFieldSetException("Underlying 
bucket "+bucket+" is too small: should be "+dataLength+" actually 
"+bucket.size());
+               randomSeed = new byte[32];
+               origRandom.nextBytes(randomSeed);
        }

        public OutputStream getOutputStream() throws IOException {
@@ -184,6 +189,7 @@
                                        Logger.normal(this, "Not padding out to 
length because have been superceded: "+getName());
                                        return;
                                }
+                               Random random = new MersenneTwister(randomSeed);
                                
synchronized(PaddedEphemerallyEncryptedBucket.this) {
                                        long finalLength = paddedLength();
                                        long padding = finalLength - dataLength;
@@ -191,7 +197,7 @@
                                        long writtenPadding = 0;
                                        while(writtenPadding < padding) {
                                                int left = (int) 
Math.min((padding - writtenPadding), (long)buf.length);
-                                               randomSource.nextBytes(buf);
+                                               random.nextBytes(buf);
                                                out.write(buf, 0, left);
                                                writtenPadding += left;
                                        }


Reply via email to