diff --git a/core/java/src/net/i2p/crypto/SHA256Generator.java b/core/java/src/net/i2p/crypto/SHA256Generator.java index 162b9d0c16c6645cce76e6f869d80662383c3031..217147f7b5037394ebf3d7ad61aea267d97f7dec 100644 --- a/core/java/src/net/i2p/crypto/SHA256Generator.java +++ b/core/java/src/net/i2p/crypto/SHA256Generator.java @@ -2,6 +2,7 @@ package net.i2p.crypto; import gnu.crypto.hash.Sha256Standalone; +import java.security.DigestException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.concurrent.LinkedBlockingQueue; @@ -20,6 +21,7 @@ public final class SHA256Generator { private final LinkedBlockingQueue<MessageDigest> _digests; private static final boolean _useGnu; + static { boolean useGnu = false; try { @@ -53,13 +55,13 @@ public final class SHA256Generator { /** * Calculate the hash and cache the result. + * @param source what to hash */ public final Hash calculateHash(byte[] source, int start, int len) { MessageDigest digest = acquire(); digest.update(source, start, len); byte rv[] = digest.digest(); release(digest); - //return new Hash(rv); return Hash.create(rv); } @@ -71,9 +73,12 @@ public final class SHA256Generator { public final void calculateHash(byte[] source, int start, int len, byte out[], int outOffset) { MessageDigest digest = acquire(); digest.update(source, start, len); - byte rv[] = digest.digest(); + try { + digest.digest(out, outOffset, Hash.HASH_LENGTH); + } catch (DigestException e) { + throw new RuntimeException(e); + } release(digest); - System.arraycopy(rv, 0, out, outOffset, rv.length); } private MessageDigest acquire() { diff --git a/router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java b/router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java index be6b54aa8e4e7e067364c0e8cf72f17079171f58..432a5e813394ec816f679d91e5435c2b0b531730 100644 --- a/router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java +++ b/router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java @@ -199,24 +199,23 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM cur += DataHelper.DATE_LENGTH; int size = (int)DataHelper.fromLong(data, cur, 2); cur += 2; - //Hash h = new Hash(); - byte hdata[] = new byte[CHECKSUM_LENGTH]; - System.arraycopy(data, cur, hdata, 0, CHECKSUM_LENGTH); - cur += CHECKSUM_LENGTH; - //h.setData(hdata); if (cur + size > data.length || headerSize + size > maxLen) - throw new I2NPMessageException("Payload is too short [" + throw new I2NPMessageException("Payload is too short [" + "data.len=" + data.length + "maxLen=" + maxLen + " offset=" + offset - + " cur=" + cur + + " cur=" + cur + " wanted=" + size + "]: " + getClass().getSimpleName()); int sz = Math.min(size, maxLen - headerSize); byte[] calc = SimpleByteCache.acquire(Hash.HASH_LENGTH); - _context.sha().calculateHash(data, cur, sz, calc, 0); - boolean eq = DataHelper.eq(hdata, 0, calc, 0, CHECKSUM_LENGTH); + + // Compare the checksum in data to the checksum of the data after the checksum + _context.sha().calculateHash(data, cur + CHECKSUM_LENGTH, sz, calc, 0); + boolean eq = DataHelper.eq(data, cur, calc, 0, CHECKSUM_LENGTH); + cur += CHECKSUM_LENGTH; + SimpleByteCache.release(calc); if (!eq) throw new I2NPMessageException("Bad checksum on " + size + " byte I2NP " + getClass().getSimpleName());