001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.j2ssh.transport.hmac;
027:
028: import com.sshtools.j2ssh.transport.AlgorithmInitializationException;
029:
030: import java.security.InvalidKeyException;
031: import java.security.NoSuchAlgorithmException;
032:
033: import javax.crypto.Mac;
034: import javax.crypto.spec.SecretKeySpec;
035:
036: /**
037: *
038: *
039: * @author $author$
040: * @version $Revision: 1.15 $
041: */
042: public class HmacMd5 implements SshHmac {
043: private Mac mac;
044:
045: /**
046: * Creates a new HmacMd5 object.
047: */
048: public HmacMd5() {
049: }
050:
051: /**
052: *
053: *
054: * @return
055: */
056: public int getMacLength() {
057: return mac.getMacLength();
058: }
059:
060: /**
061: *
062: *
063: * @param sequenceNo
064: * @param data
065: * @param offset
066: * @param len
067: *
068: * @return
069: */
070: public byte[] generate(long sequenceNo, byte[] data, int offset,
071: int len) {
072: // Write the sequence no
073: byte[] sequenceBytes = new byte[4];
074: sequenceBytes[0] = (byte) (sequenceNo >> 24);
075: sequenceBytes[1] = (byte) (sequenceNo >> 16);
076: sequenceBytes[2] = (byte) (sequenceNo >> 8);
077: sequenceBytes[3] = (byte) (sequenceNo >> 0);
078: mac.update(sequenceBytes);
079: mac.update(data, offset, len);
080:
081: return mac.doFinal();
082: }
083:
084: /**
085: *
086: *
087: * @param keydata
088: *
089: * @throws AlgorithmInitializationException
090: */
091: public void init(byte[] keydata)
092: throws AlgorithmInitializationException {
093: try {
094: mac = Mac.getInstance("HmacMD5");
095:
096: // Create a key of 16 bytes
097: byte[] key = new byte[16];
098: System.arraycopy(keydata, 0, key, 0, key.length);
099:
100: SecretKeySpec keyspec = new SecretKeySpec(key, "HmacMD5");
101: mac.init(keyspec);
102: } catch (NoSuchAlgorithmException nsae) {
103: throw new AlgorithmInitializationException(
104: "No provider exists for the HmacSha1 algorithm");
105: } catch (InvalidKeyException ike) {
106: throw new AlgorithmInitializationException("Invalid key");
107: }
108: }
109:
110: /**
111: *
112: *
113: * @param sequenceNo
114: * @param data
115: *
116: * @return
117: */
118: public boolean verify(long sequenceNo, byte[] data) {
119: int len = getMacLength();
120: byte[] generated = generate(sequenceNo, data, 0, data.length
121: - len);
122: String compare1 = new String(generated);
123: String compare2 = new String(data, data.length - len, len);
124:
125: return compare1.equals(compare2);
126: }
127: }
|