diff --git a/core/java/src/gnu/crypto/hash/BaseHashStandalone.java b/core/java/src/gnu/crypto/hash/BaseHashStandalone.java index 2ba779d326..26d51158f1 100644 --- a/core/java/src/gnu/crypto/hash/BaseHashStandalone.java +++ b/core/java/src/gnu/crypto/hash/BaseHashStandalone.java @@ -46,6 +46,10 @@ package gnu.crypto.hash; /** *

A base abstract class to facilitate hash implementations.

* + * WARNING - DEPRECATED - Use SHA256Generator.getDigestInstance() to get a + * MessageDigest that will be faster in almost all cases. + * See SHA256Generator for more information. + * * @version $Revision: 1.1 $ */ public abstract class BaseHashStandalone implements IMessageDigestStandalone { diff --git a/core/java/src/gnu/crypto/hash/IMessageDigestStandalone.java b/core/java/src/gnu/crypto/hash/IMessageDigestStandalone.java index 79fbab2020..dbba10bb87 100644 --- a/core/java/src/gnu/crypto/hash/IMessageDigestStandalone.java +++ b/core/java/src/gnu/crypto/hash/IMessageDigestStandalone.java @@ -49,6 +49,10 @@ package gnu.crypto.hash; *

A hash (or message digest) algorithm produces its output by iterating a * basic compression function on blocks of data.

* + * WARNING - DEPRECATED - Use SHA256Generator.getDigestInstance() to get a + * MessageDigest that will be faster in almost all cases. + * See SHA256Generator for more information. + * * @version $Revision: 1.1 $ */ public interface IMessageDigestStandalone extends Cloneable { diff --git a/core/java/src/gnu/crypto/hash/Sha256Standalone.java b/core/java/src/gnu/crypto/hash/Sha256Standalone.java index 85d5cb97e0..f26f844c83 100644 --- a/core/java/src/gnu/crypto/hash/Sha256Standalone.java +++ b/core/java/src/gnu/crypto/hash/Sha256Standalone.java @@ -59,6 +59,10 @@ package gnu.crypto.hash; * renamed from Sha256 to avoid conflicts with JVMs using gnu-crypto as their JCE * provider. * + * WARNING - DEPRECATED - Use SHA256Generator.getDigestInstance() to get a + * MessageDigest that will be faster in almost all cases. + * See SHA256Generator for more information. + * * @version $Revision: 1.2 $ */ public class Sha256Standalone extends BaseHashStandalone { diff --git a/core/java/src/gnu/crypto/hash/package.html b/core/java/src/gnu/crypto/hash/package.html new file mode 100644 index 0000000000..bf26a4ab38 --- /dev/null +++ b/core/java/src/gnu/crypto/hash/package.html @@ -0,0 +1,13 @@ + +

+WARNING - DEPRECATED - Use SHA256Generator.getDigestInstance() to get a +MessageDigest that will be faster in almost all cases. +

+This is the old GNU SHA-256 Hash implementation. +It is deprecated and mostly unused, unless the java.security.MessageDigest +implementation for SHA-256 is not available in the JVM. +And it always should be, right? +Do not instantiate this directly - use I2PAppContext.sha(). +See net.i2p.crypto.SHA256Generator for more information and test results. +

+ diff --git a/core/java/src/gnu/crypto/prng/FortunaStandalone.java b/core/java/src/gnu/crypto/prng/FortunaStandalone.java index 909e59ee2d..1031d7bdf8 100644 --- a/core/java/src/gnu/crypto/prng/FortunaStandalone.java +++ b/core/java/src/gnu/crypto/prng/FortunaStandalone.java @@ -41,19 +41,19 @@ exception statement from your version. */ package gnu.crypto.prng; -import gnu.crypto.hash.Sha256Standalone; - import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.security.InvalidKeyException; +import java.security.MessageDigest; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import net.i2p.crypto.CryptixAESKeyCache; import net.i2p.crypto.CryptixRijndael_Algorithm; +import net.i2p.crypto.SHA256Generator; /** * The Fortuna continuously-seeded pseudo-random number generator. This @@ -93,6 +93,7 @@ import net.i2p.crypto.CryptixRijndael_Algorithm; * Renamed from Fortuna to FortunaStandalone so it doesn't conflict with the * gnu-crypto implementation, which has been imported into GNU/classpath * + * NOTE: As of 0.8.8, uses the java.security.MessageDigest instead of GNU Sha256Standalone */ public class FortunaStandalone extends BasePRNGStandalone implements Serializable, RandomEventListenerStandalone { @@ -103,7 +104,7 @@ public class FortunaStandalone extends BasePRNGStandalone implements Serializabl static final int NUM_POOLS = 32; static final int MIN_POOL_SIZE = 64; final Generator generator; - final Sha256Standalone[] pools; + final MessageDigest[] pools; long lastReseed; int pool; int pool0Count; @@ -117,9 +118,9 @@ public class FortunaStandalone extends BasePRNGStandalone implements Serializabl { super("Fortuna i2p"); generator = new Generator(); - pools = new Sha256Standalone[NUM_POOLS]; + pools = new MessageDigest[NUM_POOLS]; for (int i = 0; i < NUM_POOLS; i++) - pools[i] = new Sha256Standalone(); + pools[i] = SHA256Generator.getDigestInstance(); lastReseed = 0; pool = 0; pool0Count = 0; @@ -227,7 +228,7 @@ public class FortunaStandalone extends BasePRNGStandalone implements Serializabl private static final int LIMIT = 1 << 20; - private final Sha256Standalone hash; + private final MessageDigest hash; private final byte[] counter; private final byte[] key; /** current encryption key built from the keying material */ @@ -238,7 +239,7 @@ public class FortunaStandalone extends BasePRNGStandalone implements Serializabl public Generator () { super("Fortuna.generator.i2p"); - this.hash = new Sha256Standalone(); + this.hash = SHA256Generator.getDigestInstance(); counter = new byte[16]; //cipher.defaultBlockSize()]; buffer = new byte[16]; //cipher.defaultBlockSize()]; int keysize = 32; diff --git a/core/java/src/net/i2p/crypto/SHA256Generator.java b/core/java/src/net/i2p/crypto/SHA256Generator.java index 4224d2e219..23efa3b361 100644 --- a/core/java/src/net/i2p/crypto/SHA256Generator.java +++ b/core/java/src/net/i2p/crypto/SHA256Generator.java @@ -90,7 +90,12 @@ public final class SHA256Generator { _digests.offer(digest); } - private static MessageDigest getDigestInstance() { + /** + * Return a new MessageDigest from the system libs unless unavailable + * in this JVM, in that case return a wrapped GNU Sha256Standalone + * @since 0.8.7, public since 0.8.8 for FortunaStandalone + */ + public static MessageDigest getDigestInstance() { if (!_useGnu) { try { return MessageDigest.getInstance("SHA-256"); diff --git a/core/java/src/net/i2p/data/DataHelper.java b/core/java/src/net/i2p/data/DataHelper.java index b5206bf2af..cac36c20d4 100644 --- a/core/java/src/net/i2p/data/DataHelper.java +++ b/core/java/src/net/i2p/data/DataHelper.java @@ -26,6 +26,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.math.BigInteger; +import java.security.MessageDigest; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; @@ -1010,13 +1011,14 @@ public class DataHelper { * Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded * Warning - not UTF-8 */ - public static String readLine(InputStream in) throws IOException { return readLine(in, (Sha256Standalone)null); } + public static String readLine(InputStream in) throws IOException { return readLine(in, (MessageDigest) null); } /** * update the hash along the way * Warning - strips \n but not \r * Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded * Warning - not UTF-8 + * @deprecated use MessageDigest version */ public static String readLine(InputStream in, Sha256Standalone hash) throws IOException { StringBuilder buf = new StringBuilder(128); @@ -1027,6 +1029,22 @@ public class DataHelper { return null; } + /** + * update the hash along the way + * Warning - strips \n but not \r + * Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded + * Warning - not UTF-8 + * @since 0.8.8 + */ + public static String readLine(InputStream in, MessageDigest hash) throws IOException { + StringBuilder buf = new StringBuilder(128); + boolean ok = readLine(in, buf, hash); + if (ok) + return buf.toString(); + else + return null; + } + /** * Read in a line, placing it into the buffer (excluding the newline). * Warning - strips \n but not \r @@ -1050,7 +1068,7 @@ public class DataHelper { * Warning - strips \n but not \r * Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded * Warning - not UTF-8 - * @deprecated use StringBuilder version + * @deprecated use StringBuilder / MessageDigest version */ @Deprecated public static boolean readLine(InputStream in, StringBuffer buf, Sha256Standalone hash) throws IOException { @@ -1080,7 +1098,7 @@ public class DataHelper { * newline was found */ public static boolean readLine(InputStream in, StringBuilder buf) throws IOException { - return readLine(in, buf, null); + return readLine(in, buf, (MessageDigest) null); } /** @@ -1088,6 +1106,7 @@ public class DataHelper { * Warning - strips \n but not \r * Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded * Warning - not UTF-8 + * @deprecated use MessageDigest version */ public static boolean readLine(InputStream in, StringBuilder buf, Sha256Standalone hash) throws IOException { int c = -1; @@ -1103,10 +1122,44 @@ public class DataHelper { return c != -1; } + /** + * update the hash along the way + * Warning - strips \n but not \r + * Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded + * Warning - not UTF-8 + * @since 0.8.8 + */ + public static boolean readLine(InputStream in, StringBuilder buf, MessageDigest hash) throws IOException { + int c = -1; + int i = 0; + while ( (c = in.read()) != -1) { + if (++i > MAX_LINE_LENGTH) + throw new IOException("Line too long - max " + MAX_LINE_LENGTH); + if (hash != null) hash.update((byte)c); + if (c == '\n') + break; + buf.append((char)c); + } + return c != -1; + } + + /** + * update the hash along the way + * @deprecated use MessageDigest version + */ public static void write(OutputStream out, byte data[], Sha256Standalone hash) throws IOException { hash.update(data); out.write(data); } + + /** + * update the hash along the way + * @since 0.8.8 + */ + public static void write(OutputStream out, byte data[], MessageDigest hash) throws IOException { + hash.update(data); + out.write(data); + } /** * Sort based on the Hash of the DataStructure. diff --git a/history.txt b/history.txt index af0f12d632..b878c8c94d 100644 --- a/history.txt +++ b/history.txt @@ -1,3 +1,11 @@ +2011-07-08 zzz + * NetDB: Fix NPE at startup (ticket #493) + * Sha256Standalone: + - Use system SHA-256 MessageDigest instead of Sha256Standalone in PRNG + - Deprecate DataHelper functions using Sha256Standalone arguments; + used only by Syndie + - Note deprecation in javadocs + 2011-07-07 zzz * Blockfile: - Log error on out-of-order spans diff --git a/router/java/src/net/i2p/router/RouterVersion.java b/router/java/src/net/i2p/router/RouterVersion.java index c10128fe62..fecba78d69 100644 --- a/router/java/src/net/i2p/router/RouterVersion.java +++ b/router/java/src/net/i2p/router/RouterVersion.java @@ -18,7 +18,7 @@ public class RouterVersion { /** deprecated */ public final static String ID = "Monotone"; public final static String VERSION = CoreVersion.VERSION; - public final static long BUILD = 6; + public final static long BUILD = 7; /** for example "-test" */ public final static String EXTRA = "";