This commit is contained in:
zzz
2010-12-26 12:42:44 +00:00
parent 34973371ac
commit 824d5a0d36
5 changed files with 31 additions and 6 deletions

View File

@ -914,9 +914,20 @@ public class DataHelper {
return c; return c;
} }
/**
* This is different than InputStream.read(target), in that it
* does repeated reads until the full data is received.
*/
public static int read(InputStream in, byte target[]) throws IOException { public static int read(InputStream in, byte target[]) throws IOException {
return read(in, target, 0, target.length); return read(in, target, 0, target.length);
} }
/**
* This is different than InputStream.read(target, offset, length), in that it
* returns the new offset (== old offset + bytes read).
* It also does repeated reads until the full data is received.
* @return the new offset (== old offset + bytes read)
*/
public static int read(InputStream in, byte target[], int offset, int length) throws IOException { public static int read(InputStream in, byte target[], int offset, int length) throws IOException {
int cur = offset; int cur = offset;
while (cur < length) { while (cur < length) {

View File

@ -28,7 +28,10 @@ public class Destination extends KeysAndCert {
fromBase64(s); fromBase64(s);
} }
/** deprecated, used only by Packet.java in streaming */ /**
* deprecated, used only by Packet.java in streaming
* @return the written length (NOT the new offset)
*/
public int writeBytes(byte target[], int offset) { public int writeBytes(byte target[], int offset) {
int cur = offset; int cur = offset;
System.arraycopy(_publicKey.getData(), 0, target, cur, PublicKey.KEYSIZE_BYTES); System.arraycopy(_publicKey.getData(), 0, target, cur, PublicKey.KEYSIZE_BYTES);

View File

@ -93,6 +93,10 @@ public class Payload extends DataStructureImpl {
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("wrote payload: " + _encryptedData.length); _log.debug("wrote payload: " + _encryptedData.length);
} }
/**
* @return the written length (NOT the new offset)
*/
public int writeBytes(byte target[], int offset) { public int writeBytes(byte target[], int offset) {
if (_encryptedData == null) throw new IllegalStateException("Not yet encrypted. Please set the encrypted data"); if (_encryptedData == null) throw new IllegalStateException("Not yet encrypted. Please set the encrypted data");
DataHelper.toLong(target, offset, 4, _encryptedData.length); DataHelper.toLong(target, offset, 4, _encryptedData.length);

View File

@ -23,7 +23,7 @@ import net.i2p.crypto.EntropyHarvester;
* *
*/ */
public class FortunaRandomSource extends RandomSource implements EntropyHarvester { public class FortunaRandomSource extends RandomSource implements EntropyHarvester {
private AsyncFortunaStandalone _fortuna; private final AsyncFortunaStandalone _fortuna;
private double _nextGaussian; private double _nextGaussian;
private boolean _haveNextGaussian; private boolean _haveNextGaussian;
@ -210,6 +210,7 @@ public class FortunaRandomSource extends RandomSource implements EntropyHarveste
_fortuna.addRandomBytes(data, offset, len); _fortuna.addRandomBytes(data, offset, len);
} }
/*****
public static void main(String args[]) { public static void main(String args[]) {
try { try {
RandomSource rand = I2PAppContext.getGlobalContext().random(); RandomSource rand = I2PAppContext.getGlobalContext().random();
@ -231,4 +232,5 @@ public class FortunaRandomSource extends RandomSource implements EntropyHarveste
System.out.println("Compressed size of 1MB: " + compressed.length); System.out.println("Compressed size of 1MB: " + compressed.length);
} catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); }
} }
*****/
} }

View File

@ -25,18 +25,23 @@ import net.i2p.data.Base64;
* @author jrandom * @author jrandom
*/ */
public class RandomSource extends SecureRandom implements EntropyHarvester { public class RandomSource extends SecureRandom implements EntropyHarvester {
private Log _log; private final EntropyHarvester _entropyHarvester;
private EntropyHarvester _entropyHarvester; protected final I2PAppContext _context;
protected I2PAppContext _context;
public RandomSource(I2PAppContext context) { public RandomSource(I2PAppContext context) {
super(); super();
_context = context; _context = context;
_log = context.logManager().getLog(RandomSource.class);
// when we replace to have hooks for fortuna (etc), replace with // when we replace to have hooks for fortuna (etc), replace with
// a factory (or just a factory method) // a factory (or just a factory method)
_entropyHarvester = this; _entropyHarvester = this;
} }
/**
* Singleton for whatever PRNG i2p uses.
* Same as I2PAppContext.getGlobalContext().random();
* use context.random() if you have a context already.
* @return I2PAppContext.getGlobalContext().random()
*/
public static RandomSource getInstance() { public static RandomSource getInstance() {
return I2PAppContext.getGlobalContext().random(); return I2PAppContext.getGlobalContext().random();
} }