KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 package ch.ethz.ssh2.crypto.digest;
3
4 /**
5  * MAC.
6  *
7  * @author Christian Plattner, plattner@inf.ethz.ch
8  * @version $Id: MAC.java,v 1.4 2006/02/02 09:11:03 cplattne Exp $
9  */

10 public final class MAC
11 {
12     Digest mac;
13     int size;
14
15     public final static String JavaDoc[] getMacList()
16     {
17         /* Higher Priority First */
18
19         return new String JavaDoc[] { "hmac-sha1-96", "hmac-sha1", "hmac-md5-96", "hmac-md5" };
20     }
21
22     public final static void checkMacList(String JavaDoc[] macs)
23     {
24         for (int i = 0; i < macs.length; i++)
25             getKeyLen(macs[i]);
26     }
27
28     public final static int getKeyLen(String JavaDoc type)
29     {
30         if (type.equals("hmac-sha1"))
31             return 20;
32         if (type.equals("hmac-sha1-96"))
33             return 20;
34         if (type.equals("hmac-md5"))
35             return 16;
36         if (type.equals("hmac-md5-96"))
37             return 16;
38         throw new IllegalArgumentException JavaDoc("Unkown algorithm " + type);
39     }
40
41     public MAC(String JavaDoc type, byte[] key)
42     {
43         if (type.equals("hmac-sha1"))
44         {
45             mac = new HMAC(new SHA1(), key, 20);
46         }
47         else if (type.equals("hmac-sha1-96"))
48         {
49             mac = new HMAC(new SHA1(), key, 12);
50         }
51         else if (type.equals("hmac-md5"))
52         {
53             mac = new HMAC(new MD5(), key, 16);
54         }
55         else if (type.equals("hmac-md5-96"))
56         {
57             mac = new HMAC(new MD5(), key, 12);
58         }
59         else
60             throw new IllegalArgumentException JavaDoc("Unkown algorithm " + type);
61
62         size = mac.getDigestLength();
63     }
64
65     public final void initMac(int seq)
66     {
67         mac.reset();
68         mac.update((byte) (seq >> 24));
69         mac.update((byte) (seq >> 16));
70         mac.update((byte) (seq >> 8));
71         mac.update((byte) (seq));
72     }
73
74     public final void update(byte[] packetdata, int off, int len)
75     {
76         mac.update(packetdata, off, len);
77     }
78
79     public final void getMac(byte[] out, int off)
80     {
81         mac.digest(out, off);
82     }
83
84     public final int size()
85     {
86         return size;
87     }
88 }
89
Popular Tags