retrofit SimpleDataStructure over SHA1Hash
This commit is contained in:
@ -14,8 +14,7 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import net.i2p.data.DataFormatException;
|
import net.i2p.data.DataFormatException;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.SimpleDataStructure;
|
||||||
import net.i2p.data.DataStructureImpl;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Because DSAEngine was abusing Hash for 20-byte hashes
|
* Because DSAEngine was abusing Hash for 20-byte hashes
|
||||||
@ -23,44 +22,31 @@ import net.i2p.data.DataStructureImpl;
|
|||||||
* @since 0.8.1
|
* @since 0.8.1
|
||||||
* @author zzz
|
* @author zzz
|
||||||
*/
|
*/
|
||||||
public class SHA1Hash extends DataStructureImpl {
|
public class SHA1Hash extends SimpleDataStructure {
|
||||||
private byte[] _data;
|
|
||||||
private int _cachedHashCode;
|
private int _cachedHashCode;
|
||||||
|
|
||||||
public final static int HASH_LENGTH = SHA1.HASH_LENGTH;
|
public final static int HASH_LENGTH = SHA1.HASH_LENGTH;
|
||||||
|
|
||||||
/** @throws IllegalArgumentException if data is not 20 bytes (null is ok) */
|
/** @throws IllegalArgumentException if data is not 20 bytes (null is ok) */
|
||||||
public SHA1Hash(byte data[]) {
|
public SHA1Hash(byte data[]) {
|
||||||
setData(data);
|
super(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getData() {
|
public int length() {
|
||||||
return _data;
|
return HASH_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @throws IllegalArgumentException if data is not 20 bytes (null is ok) */
|
/** @throws IllegalArgumentException if data is not 20 bytes (null is ok) */
|
||||||
|
@Override
|
||||||
public void setData(byte[] data) {
|
public void setData(byte[] data) {
|
||||||
// FIXME DSAEngine uses a SHA-1 "Hash" as parameters and return values!
|
super.setData(data);
|
||||||
if (data != null && data.length != HASH_LENGTH)
|
_cachedHashCode = super.hashCode();
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public void readBytes(InputStream in) throws DataFormatException, IOException {
|
||||||
if ((obj == null) || !(obj instanceof SHA1Hash)) return false;
|
super.readBytes(in);
|
||||||
return DataHelper.eq(_data, ((SHA1Hash) obj)._data);
|
_cachedHashCode = super.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** a Hash is a hash, so just use the first 4 bytes for speed */
|
/** 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() {
|
public int hashCode() {
|
||||||
return _cachedHashCode;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user