001: /* HMACT64 keyed hashing algorithm
002: * Copyright (C) 2003 "Eric Glass" <jcifs at samba dot org>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package jcifs.util;
020:
021: import java.security.MessageDigest;
022:
023: /**
024: * This is an implementation of the HMACT64 keyed hashing algorithm.
025: * HMACT64 is defined by Luke Leighton as a modified HMAC-MD5 (RFC 2104)
026: * in which the key is truncated at 64 bytes (rather than being hashed
027: * via MD5).
028: */
029: public class HMACT64 extends MessageDigest implements Cloneable {
030:
031: private static final int BLOCK_LENGTH = 64;
032:
033: private static final byte IPAD = (byte) 0x36;
034:
035: private static final byte OPAD = (byte) 0x5c;
036:
037: private MessageDigest md5;
038:
039: private byte[] ipad = new byte[BLOCK_LENGTH];
040:
041: private byte[] opad = new byte[BLOCK_LENGTH];
042:
043: /**
044: * Creates an HMACT64 instance which uses the given secret key material.
045: *
046: * @param key The key material to use in hashing.
047: */
048: public HMACT64(byte[] key) {
049: super ("HMACT64");
050: int length = Math.min(key.length, BLOCK_LENGTH);
051: for (int i = 0; i < length; i++) {
052: ipad[i] = (byte) (key[i] ^ IPAD);
053: opad[i] = (byte) (key[i] ^ OPAD);
054: }
055: for (int i = length; i < BLOCK_LENGTH; i++) {
056: ipad[i] = IPAD;
057: opad[i] = OPAD;
058: }
059: try {
060: md5 = MessageDigest.getInstance("MD5");
061: } catch (Exception ex) {
062: throw new IllegalStateException(ex.getMessage());
063: }
064: engineReset();
065: }
066:
067: private HMACT64(HMACT64 hmac) throws CloneNotSupportedException {
068: super ("HMACT64");
069: this .ipad = hmac.ipad;
070: this .opad = hmac.opad;
071: this .md5 = (MessageDigest) hmac.md5.clone();
072: }
073:
074: public Object clone() {
075: try {
076: return new HMACT64(this );
077: } catch (CloneNotSupportedException ex) {
078: throw new IllegalStateException(ex.getMessage());
079: }
080: }
081:
082: protected byte[] engineDigest() {
083: byte[] digest = md5.digest();
084: md5.update(opad);
085: return md5.digest(digest);
086: }
087:
088: protected int engineDigest(byte[] buf, int offset, int len) {
089: byte[] digest = md5.digest();
090: md5.update(opad);
091: md5.update(digest);
092: try {
093: return md5.digest(buf, offset, len);
094: } catch (Exception ex) {
095: throw new IllegalStateException();
096: }
097: }
098:
099: 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: }
|