2006-05-11 jrandom

* PRNG bugfix (thanks cervantes and Complication!)
This commit is contained in:
jrandom
2006-05-12 03:31:44 +00:00
committed by zzz
parent 0920462060
commit 2ad5a6f907
3 changed files with 47 additions and 11 deletions

View File

@ -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(); }
}
}

View File

@ -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

View File

@ -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);