01: package ch.ethz.ssh2.crypto.digest;
02:
03: import java.math.BigInteger;
04:
05: /**
06: * HashForSSH2Types.
07: *
08: * @author Christian Plattner, plattner@inf.ethz.ch
09: * @version $Id: HashForSSH2Types.java,v 1.3 2005/08/12 23:37:18 cplattne Exp $
10: */
11: public class HashForSSH2Types {
12: Digest md;
13:
14: public HashForSSH2Types(Digest md) {
15: this .md = md;
16: }
17:
18: public HashForSSH2Types(String type) {
19: if (type.equals("SHA1")) {
20: md = new SHA1();
21: } else if (type.equals("MD5")) {
22: md = new MD5();
23: } else
24: throw new IllegalArgumentException("Unknown algorithm "
25: + type);
26: }
27:
28: public void updateByte(byte b) {
29: /* HACK - to test it with J2ME */
30: byte[] tmp = new byte[1];
31: tmp[0] = b;
32: md.update(tmp);
33: }
34:
35: public void updateBytes(byte[] b) {
36: md.update(b);
37: }
38:
39: public void updateUINT32(int v) {
40: md.update((byte) (v >> 24));
41: md.update((byte) (v >> 16));
42: md.update((byte) (v >> 8));
43: md.update((byte) (v));
44: }
45:
46: public void updateByteString(byte[] b) {
47: updateUINT32(b.length);
48: updateBytes(b);
49: }
50:
51: public void updateBigInt(BigInteger b) {
52: updateByteString(b.toByteArray());
53: }
54:
55: public void reset() {
56: md.reset();
57: }
58:
59: public int getDigestLength() {
60: return md.getDigestLength();
61: }
62:
63: public byte[] getDigest() {
64: byte[] tmp = new byte[md.getDigestLength()];
65: getDigest(tmp);
66: return tmp;
67: }
68:
69: public void getDigest(byte[] out) {
70: getDigest(out, 0);
71: }
72:
73: public void getDigest(byte[] out, int off) {
74: md.digest(out, off);
75: }
76: }
|