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;
}
/**
* 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 {
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 {
int cur = offset;
while (cur < length) {

View File

@ -28,7 +28,10 @@ public class Destination extends KeysAndCert {
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) {
int cur = offset;
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))
_log.debug("wrote payload: " + _encryptedData.length);
}
/**
* @return the written length (NOT the new offset)
*/
public int writeBytes(byte target[], int offset) {
if (_encryptedData == null) throw new IllegalStateException("Not yet encrypted. Please set the encrypted data");
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 {
private AsyncFortunaStandalone _fortuna;
private final AsyncFortunaStandalone _fortuna;
private double _nextGaussian;
private boolean _haveNextGaussian;
@ -210,6 +210,7 @@ public class FortunaRandomSource extends RandomSource implements EntropyHarveste
_fortuna.addRandomBytes(data, offset, len);
}
/*****
public static void main(String args[]) {
try {
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);
} catch (Exception e) { e.printStackTrace(); }
}
*****/
}

View File

@ -25,18 +25,23 @@ import net.i2p.data.Base64;
* @author jrandom
*/
public class RandomSource extends SecureRandom implements EntropyHarvester {
private Log _log;
private EntropyHarvester _entropyHarvester;
protected I2PAppContext _context;
private final EntropyHarvester _entropyHarvester;
protected final I2PAppContext _context;
public RandomSource(I2PAppContext context) {
super();
_context = context;
_log = context.logManager().getLog(RandomSource.class);
// when we replace to have hooks for fortuna (etc), replace with
// a factory (or just a factory method)
_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() {
return I2PAppContext.getGlobalContext().random();
}