Changeset 7c8e5c6


Ignore:
Timestamp:
Dec 17, 2010 10:04:57 PM (10 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
91f1ece
Parents:
d699eaae
Message:

retrofit SimpleDataStructure? over SHA1Hash

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/crypto/SHA1Hash.java

    rd699eaae r7c8e5c6  
    1515
    1616import net.i2p.data.DataFormatException;
    17 import net.i2p.data.DataHelper;
    18 import net.i2p.data.DataStructureImpl;
     17import net.i2p.data.SimpleDataStructure;
    1918
    2019/**
     
    2423 * @author zzz
    2524 */
    26 public class SHA1Hash extends DataStructureImpl {
    27     private byte[] _data;
     25public class SHA1Hash extends SimpleDataStructure {
    2826    private int _cachedHashCode;
    2927
     
    3230    /** @throws IllegalArgumentException if data is not 20 bytes (null is ok) */
    3331    public SHA1Hash(byte data[]) {
    34         setData(data);
     32        super(data);
    3533    }
    3634
    37     public byte[] getData() {
    38         return _data;
     35    public int length() {
     36        return HASH_LENGTH;
    3937    }
    4038
    4139    /** @throws IllegalArgumentException if data is not 20 bytes (null is ok) */
     40    @Override
    4241    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();
    5844    }
    5945
    6046    @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();
    6450    }
    6551   
     
    6955        return _cachedHashCode;
    7056    }
    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     }
    8157}
Note: See TracChangeset for help on using the changeset viewer.