1 2 package ch.ethz.ssh2.crypto.digest; 3 4 import java.math.BigInteger ; 5 6 12 public class HashForSSH2Types 13 { 14 Digest md; 15 16 public HashForSSH2Types(Digest md) 17 { 18 this.md = md; 19 } 20 21 public HashForSSH2Types(String type) 22 { 23 if (type.equals("SHA1")) 24 { 25 md = new SHA1(); 26 } 27 else if (type.equals("MD5")) 28 { 29 md = new MD5(); 30 } 31 else 32 throw new IllegalArgumentException ("Unknown algorithm " + type); 33 } 34 35 public void updateByte(byte b) 36 { 37 38 byte[] tmp = new byte[1]; 39 tmp[0] = b; 40 md.update(tmp); 41 } 42 43 public void updateBytes(byte[] b) 44 { 45 md.update(b); 46 } 47 48 public void updateUINT32(int v) 49 { 50 md.update((byte) (v >> 24)); 51 md.update((byte) (v >> 16)); 52 md.update((byte) (v >> 8)); 53 md.update((byte) (v)); 54 } 55 56 public void updateByteString(byte[] b) 57 { 58 updateUINT32(b.length); 59 updateBytes(b); 60 } 61 62 public void updateBigInt(BigInteger b) 63 { 64 updateByteString(b.toByteArray()); 65 } 66 67 public void reset() 68 { 69 md.reset(); 70 } 71 72 public int getDigestLength() 73 { 74 return md.getDigestLength(); 75 } 76 77 public byte[] getDigest() 78 { 79 byte[] tmp = new byte[md.getDigestLength()]; 80 getDigest(tmp); 81 return tmp; 82 } 83 84 public void getDigest(byte[] out) 85 { 86 getDigest(out, 0); 87 } 88 89 public void getDigest(byte[] out, int off) 90 { 91 md.digest(out, off); 92 } 93 } 94 | Popular Tags |