001: /*
002: * @(#)MD2.java 1.3 02/07/24 @(#)
003: *
004: * Copyright (c) 2001 Sun Microsystems, Inc. All rights reserved.
005: * PROPRIETARY/CONFIDENTIAL
006: * Use is subject to license terms.
007: */
008:
009: package com.sun.portal.ksecurity;
010:
011: /**
012: * Implements the MD2 hashing algorithm as described in IETF RFC 1321
013: * (see http://www.ietf.org/rfc/rfc1321.txt)
014: */
015: final class MD2 extends MessageDigest {
016: /*
017: * The compute intensive operations are implemented in C
018: * based on the OpenSSL MD2 code (from pilotSSLeay). Here we
019: * replicate the state the C code needs.
020: */
021:
022: // Internal context
023: private int[] num = new int[1];
024: private byte[] data = new byte[16];
025: private int[] cksm = new int[16];
026: private int[] state = new int[16];
027:
028: private byte type; // message digest algorithm
029: private byte len; // length of hash in bytes
030:
031: MD2() {
032: type = ALG_MD2;
033: len = 16;
034: reset();
035: }
036:
037: public byte getAlgorithm() {
038: return type;
039: }
040:
041: public byte getLength() {
042: return len;
043: }
044:
045: /* MD2 initialization. Begins an MD2 operation, writing a new context */
046: public void reset() {
047: num[0] = 0;
048:
049: for (int i = 0; i < 16; i++) {
050: data[i] = (byte) 0;
051: cksm[i] = 0;
052: state[i] = 0;
053: }
054: }
055:
056: /*
057: * MD2 block update operation. Continues an MD2 message-digest
058: * operation, processing another message block, and updating internal
059: * context.
060: */
061: public void update(byte[] inBuf, int inOff, int inLen) {
062: if (inLen == 0)
063: return;
064:
065: // check parameters to prevent VM from crashing
066: int test = inBuf[inOff] + inBuf[inLen - 1]
067: + inBuf[inOff + inLen - 1];
068:
069: nativeUpdate(inBuf, inOff, inLen, state, num, cksm, data);
070: }
071:
072: private static native void nativeUpdate(byte[] inBuf, int inOff,
073: int inLen, int[] state, int[] num, int[] cksm, byte[] data);
074:
075: public short doFinal(byte[] inBuf, int inOff, int inLen,
076: byte[] outBuf, int outOff) {
077: // check the parameters to prevent a VM crash
078: int test = outBuf[outOff] + outBuf[outOff + 15];
079: if (inLen != 0) {
080: test = inBuf[inOff] + inBuf[inLen - 1]
081: + inBuf[inOff + inLen - 1];
082: }
083:
084: nativeFinal(inBuf, inOff, inLen, outBuf, outOff, state, num,
085: cksm, data);
086: return (short) 16;
087: }
088:
089: private static native void nativeFinal(byte[] inBuf, int inOff,
090: int inLen, byte[] outBuf, int outOff, int[] state,
091: int[] num, int[] cksm, byte[] data);
092:
093: public Object clone() {
094: MD2 cpy = new MD2();
095:
096: System.arraycopy(this .state, 0, cpy.state, 0, 16);
097: System.arraycopy(this .num, 0, cpy.num, 0, 1);
098: System.arraycopy(this .cksm, 0, cpy.cksm, 0, 16);
099: System.arraycopy(this .data, 0, cpy.data, 0, 16);
100: cpy.type = this.type;
101: cpy.len = this.len;
102: return cpy;
103: }
104: }
|