From 7c8e5c6d66764cfe88432fbc5c772374059d4127 Mon Sep 17 00:00:00 2001 From: zzz <zzz@mail.i2p> Date: Fri, 17 Dec 2010 22:04:57 +0000 Subject: [PATCH] retrofit SimpleDataStructure over SHA1Hash --- core/java/src/net/i2p/crypto/SHA1Hash.java | 46 ++++++---------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/core/java/src/net/i2p/crypto/SHA1Hash.java b/core/java/src/net/i2p/crypto/SHA1Hash.java index acbb68d4b8..c9a0f783db 100644 --- a/core/java/src/net/i2p/crypto/SHA1Hash.java +++ b/core/java/src/net/i2p/crypto/SHA1Hash.java @@ -14,8 +14,7 @@ import java.io.InputStream; import java.io.OutputStream; import net.i2p.data.DataFormatException; -import net.i2p.data.DataHelper; -import net.i2p.data.DataStructureImpl; +import net.i2p.data.SimpleDataStructure; /** * Because DSAEngine was abusing Hash for 20-byte hashes @@ -23,44 +22,31 @@ import net.i2p.data.DataStructureImpl; * @since 0.8.1 * @author zzz */ -public class SHA1Hash extends DataStructureImpl { - private byte[] _data; +public class SHA1Hash extends SimpleDataStructure { private int _cachedHashCode; public final static int HASH_LENGTH = SHA1.HASH_LENGTH; /** @throws IllegalArgumentException if data is not 20 bytes (null is ok) */ public SHA1Hash(byte data[]) { - setData(data); + super(data); } - public byte[] getData() { - return _data; + public int length() { + return HASH_LENGTH; } /** @throws IllegalArgumentException if data is not 20 bytes (null is ok) */ + @Override public void setData(byte[] data) { - // FIXME DSAEngine uses a SHA-1 "Hash" as parameters and return values! - if (data != null && data.length != HASH_LENGTH) - throw new IllegalArgumentException("Hash must be 20 bytes"); - _data = data; - _cachedHashCode = calcHashCode(); - } - - /** @throws IOException always */ - public void readBytes(InputStream in) throws DataFormatException, IOException { - throw new IOException("unimplemented"); - } - - /** @throws IOException always */ - public void writeBytes(OutputStream out) throws DataFormatException, IOException { - throw new IOException("unimplemented"); + super.setData(data); + _cachedHashCode = super.hashCode(); } @Override - public boolean equals(Object obj) { - if ((obj == null) || !(obj instanceof SHA1Hash)) return false; - return DataHelper.eq(_data, ((SHA1Hash) obj)._data); + public void readBytes(InputStream in) throws DataFormatException, IOException { + super.readBytes(in); + _cachedHashCode = super.hashCode(); } /** a Hash is a hash, so just use the first 4 bytes for speed */ @@ -68,14 +54,4 @@ public class SHA1Hash extends DataStructureImpl { public int hashCode() { return _cachedHashCode; } - - /** a Hash is a hash, so just use the first 4 bytes for speed */ - private int calcHashCode() { - int rv = 0; - if (_data != null) { - for (int i = 0; i < 4; i++) - rv ^= (_data[i] << (i*8)); - } - return rv; - } } -- GitLab