Changeset 7c8e5c6
- Timestamp:
- Dec 17, 2010 10:04:57 PM (10 years ago)
- Branches:
- master
- Children:
- 91f1ece
- Parents:
- d699eaae
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
core/java/src/net/i2p/crypto/SHA1Hash.java
rd699eaae r7c8e5c6 15 15 16 16 import net.i2p.data.DataFormatException; 17 import net.i2p.data.DataHelper; 18 import net.i2p.data.DataStructureImpl; 17 import net.i2p.data.SimpleDataStructure; 19 18 20 19 /** … … 24 23 * @author zzz 25 24 */ 26 public class SHA1Hash extends DataStructureImpl { 27 private byte[] _data; 25 public class SHA1Hash extends SimpleDataStructure { 28 26 private int _cachedHashCode; 29 27 … … 32 30 /** @throws IllegalArgumentException if data is not 20 bytes (null is ok) */ 33 31 public SHA1Hash(byte data[]) { 34 s etData(data);32 super(data); 35 33 } 36 34 37 public byte[] getData() {38 return _data;35 public int length() { 36 return HASH_LENGTH; 39 37 } 40 38 41 39 /** @throws IllegalArgumentException if data is not 20 bytes (null is ok) */ 40 @Override 42 41 public void setData(byte[] data) { 43 // FIXME DSAEngine uses a SHA-1 "Hash" as parameters and return values! 44 if (data != null && data.length != HASH_LENGTH) 45 throw new IllegalArgumentException("Hash must be 20 bytes"); 46 _data = data; 47 _cachedHashCode = calcHashCode(); 48 } 49 50 /** @throws IOException always */ 51 public void readBytes(InputStream in) throws DataFormatException, IOException { 52 throw new IOException("unimplemented"); 53 } 54 55 /** @throws IOException always */ 56 public void writeBytes(OutputStream out) throws DataFormatException, IOException { 57 throw new IOException("unimplemented"); 42 super.setData(data); 43 _cachedHashCode = super.hashCode(); 58 44 } 59 45 60 46 @Override 61 public boolean equals(Object obj){62 if ((obj == null) || !(obj instanceof SHA1Hash)) return false;63 return DataHelper.eq(_data, ((SHA1Hash) obj)._data);47 public void readBytes(InputStream in) throws DataFormatException, IOException { 48 super.readBytes(in); 49 _cachedHashCode = super.hashCode(); 64 50 } 65 51 … … 69 55 return _cachedHashCode; 70 56 } 71 72 /** a Hash is a hash, so just use the first 4 bytes for speed */73 private int calcHashCode() {74 int rv = 0;75 if (_data != null) {76 for (int i = 0; i < 4; i++)77 rv ^= (_data[i] << (i*8));78 }79 return rv;80 }81 57 }
Note: See TracChangeset
for help on using the changeset viewer.