KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > util > HMACT64


1 /* HMACT64 keyed hashing algorithm
2  * Copyright (C) 2003 "Eric Glass" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package jcifs.util;
20
21 import java.security.MessageDigest JavaDoc;
22
23 /**
24  * This is an implementation of the HMACT64 keyed hashing algorithm.
25  * HMACT64 is defined by Luke Leighton as a modified HMAC-MD5 (RFC 2104)
26  * in which the key is truncated at 64 bytes (rather than being hashed
27  * via MD5).
28  */

29 public class HMACT64 extends MessageDigest JavaDoc implements Cloneable JavaDoc {
30
31     private static final int BLOCK_LENGTH = 64;
32
33     private static final byte IPAD = (byte) 0x36;
34
35     private static final byte OPAD = (byte) 0x5c;
36
37     private MessageDigest JavaDoc md5;
38
39     private byte[] ipad = new byte[BLOCK_LENGTH];
40
41     private byte[] opad = new byte[BLOCK_LENGTH];
42
43     /**
44      * Creates an HMACT64 instance which uses the given secret key material.
45      *
46      * @param key The key material to use in hashing.
47      */

48     public HMACT64(byte[] key) {
49         super("HMACT64");
50         int length = Math.min(key.length, BLOCK_LENGTH);
51         for (int i = 0; i < length; i++) {
52             ipad[i] = (byte) (key[i] ^ IPAD);
53             opad[i] = (byte) (key[i] ^ OPAD);
54         }
55         for (int i = length; i < BLOCK_LENGTH; i++) {
56             ipad[i] = IPAD;
57             opad[i] = OPAD;
58         }
59         try {
60             md5 = MessageDigest.getInstance("MD5");
61         } catch (Exception JavaDoc ex) {
62             throw new IllegalStateException JavaDoc(ex.getMessage());
63         }
64         engineReset();
65     }
66
67     private HMACT64(HMACT64 hmac) throws CloneNotSupportedException JavaDoc {
68         super("HMACT64");
69         this.ipad = hmac.ipad;
70         this.opad = hmac.opad;
71         this.md5 = (MessageDigest JavaDoc) hmac.md5.clone();
72     }
73
74     public Object JavaDoc clone() {
75         try {
76             return new HMACT64(this);
77         } catch (CloneNotSupportedException JavaDoc ex) {
78             throw new IllegalStateException JavaDoc(ex.getMessage());
79         }
80     }
81
82     protected byte[] engineDigest() {
83         byte[] digest = md5.digest();
84         md5.update(opad);
85         return md5.digest(digest);
86     }
87
88     protected int engineDigest(byte[] buf, int offset, int len) {
89         byte[] digest = md5.digest();
90         md5.update(opad);
91         md5.update(digest);
92         try {
93             return md5.digest(buf, offset, len);
94         } catch (Exception JavaDoc ex) {
95             throw new IllegalStateException JavaDoc();
96         }
97     }
98
99     protected int engineGetDigestLength() {
100         return md5.getDigestLength();
101     }
102
103     protected void engineReset() {
104         md5.reset();
105         md5.update(ipad);
106     }
107
108     protected void engineUpdate(byte b) {
109         md5.update(b);
110     }
111
112     protected void engineUpdate(byte[] input, int offset, int len) {
113         md5.update(input, offset, len);
114     }
115
116 }
117
Popular Tags