KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > crypto > digest > HashForSSH2Types


1
2 package ch.ethz.ssh2.crypto.digest;
3
4 import java.math.BigInteger JavaDoc;
5
6 /**
7  * HashForSSH2Types.
8  *
9  * @author Christian Plattner, plattner@inf.ethz.ch
10  * @version $Id: HashForSSH2Types.java,v 1.3 2005/08/12 23:37:18 cplattne Exp $
11  */

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 JavaDoc 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 JavaDoc("Unknown algorithm " + type);
33     }
34
35     public void updateByte(byte b)
36     {
37         /* HACK - to test it with J2ME */
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 JavaDoc 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