From 2ad5a6f90702bea2d171d61e7572917ce43b138e Mon Sep 17 00:00:00 2001 From: jrandom Date: Fri, 12 May 2006 03:31:44 +0000 Subject: [PATCH] 2006-05-11 jrandom * PRNG bugfix (thanks cervantes and Complication!) --- .../src/net/i2p/util/FortunaRandomSource.java | 49 ++++++++++++++++--- history.txt | 5 +- .../src/net/i2p/router/RouterVersion.java | 4 +- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/core/java/src/net/i2p/util/FortunaRandomSource.java b/core/java/src/net/i2p/util/FortunaRandomSource.java index 0ca277f84..74eb4938c 100644 --- a/core/java/src/net/i2p/util/FortunaRandomSource.java +++ b/core/java/src/net/i2p/util/FortunaRandomSource.java @@ -76,15 +76,33 @@ public class FortunaRandomSource extends RandomSource implements EntropyHarveste if (n<=0) throw new IllegalArgumentException("n must be positive"); - if ((n & -n) == n) // i.e., n is a power of 2 - return (int)((n * (long)nextBits(31)) >> 31); + //// + // this shortcut from sun's docs neither works nor is necessary. + // + //if ((n & -n) == n) { + // // i.e., n is a power of 2 + // return (int)((n * (long)nextBits(31)) >> 31); + //} - int bits, val; - do { - bits = nextBits(31); - val = bits % n; - } while(bits - val + (n-1) < 0); - return val; + int numBits = 0; + int remaining = n; + int rv = 0; + while (remaining > 0) { + remaining >>= 1; + rv += nextBits(8) << numBits*8; + numBits++; + } + if (rv < 0) + rv += n; + return rv % n; + + //int bits, val; + //do { + // bits = nextBits(31); + // val = bits % n; + //} while(bits - val + (n-1) < 0); + // + //return val; } /** @@ -180,4 +198,19 @@ public class FortunaRandomSource extends RandomSource implements EntropyHarveste public synchronized void feedEntropy(String source, byte[] data, int offset, int len) { _fortuna.addRandomBytes(data, offset, len); } + + public static void main(String args[]) { + try { + RandomSource rand = I2PAppContext.getGlobalContext().random(); + java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); + java.util.zip.GZIPOutputStream gos = new java.util.zip.GZIPOutputStream(baos); + for (int i = 0; i < 1024*1024; i++) { + int c = rand.nextInt(256); + gos.write((byte)c); + } + gos.finish(); + byte compressed[] = baos.toByteArray(); + System.out.println("Compressed size of 1MB: " + compressed.length); + } catch (Exception e) { e.printStackTrace(); } + } } diff --git a/history.txt b/history.txt index ac8835954..052f3baf4 100644 --- a/history.txt +++ b/history.txt @@ -1,4 +1,7 @@ -$Id: history.txt,v 1.471 2006/05/07 22:19:46 complication Exp $ +$Id: history.txt,v 1.472 2006/05/09 16:17:17 jrandom Exp $ + +2006-05-11 jrandom + * PRNG bugfix (thanks cervantes and Complication!) * 2006-05-09 0.6.1.18 released diff --git a/router/java/src/net/i2p/router/RouterVersion.java b/router/java/src/net/i2p/router/RouterVersion.java index 431d598db..61154d5d3 100644 --- a/router/java/src/net/i2p/router/RouterVersion.java +++ b/router/java/src/net/i2p/router/RouterVersion.java @@ -15,9 +15,9 @@ import net.i2p.CoreVersion; * */ public class RouterVersion { - public final static String ID = "$Revision: 1.411 $ $Date: 2006/05/07 22:19:47 $"; + public final static String ID = "$Revision: 1.412 $ $Date: 2006/05/09 16:17:22 $"; public final static String VERSION = "0.6.1.18"; - public final static long BUILD = 0; + public final static long BUILD = 1; public static void main(String args[]) { System.out.println("I2P Router version: " + VERSION + "-" + BUILD); System.out.println("Router ID: " + RouterVersion.ID);