retrofit SimpleDataStructure over SHA1Hash

This commit is contained in:
zzz
2010-12-17 22:04:57 +00:00
parent d699eaaec9
commit 7c8e5c6d66

View File

@ -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;
}
}