001: /******************************************************************************
002: * Copyright (c) 2000 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)
003: * Permission is hereby granted, free of charge, to any person obtaining a
004: * copy of this software and associated documentation files (the "Software"),
005: * to deal in the Software without restriction, including without limitation
006: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
007: * and/or sell copies of the Software, and to permit persons to whom the
008: * Software is furnished to do so, subject to the following conditions:
009: *
010: * The above copyright notice and this permission notice shall be included
011: * in all copies or substantial portions of the Software.
012: *
013: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
014: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
015: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
016: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
017: * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
018: * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
019: * USE OR OTHER DEALINGS IN THE SOFTWARE.
020: ******************************************************************************/package net.sourceforge.jtds.util;
021:
022: /**
023: * base implementation of MD4 family style digest as outlined in
024: * "Handbook of Applied Cryptography", pages 344 - 347.
025: *
026: * @version $Id: GeneralDigest.java,v 1.2 2004/06/27 17:00:55 bheineman Exp $
027: */
028: public abstract class GeneralDigest
029: // implements Digest - mdb: we don't care about this interface
030: {
031: private byte[] xBuf;
032: private int xBufOff;
033:
034: private long byteCount;
035:
036: /**
037: * Standard constructor
038: */
039: protected GeneralDigest() {
040: xBuf = new byte[4];
041: xBufOff = 0;
042: }
043:
044: /**
045: * Copy constructor. We are using copy constructors in place
046: * of the Object.clone() interface as this interface is not
047: * supported by J2ME.
048: */
049: protected GeneralDigest(GeneralDigest t) {
050: xBuf = new byte[t.xBuf.length];
051: System.arraycopy(t.xBuf, 0, xBuf, 0, t.xBuf.length);
052:
053: xBufOff = t.xBufOff;
054: byteCount = t.byteCount;
055: }
056:
057: public void update(byte in) {
058: xBuf[xBufOff++] = in;
059:
060: if (xBufOff == xBuf.length) {
061: processWord(xBuf, 0);
062: xBufOff = 0;
063: }
064:
065: byteCount++;
066: }
067:
068: public void update(byte[] in, int inOff, int len) {
069: //
070: // fill the current word
071: //
072: while ((xBufOff != 0) && (len > 0)) {
073: update(in[inOff]);
074:
075: inOff++;
076: len--;
077: }
078:
079: //
080: // process whole words.
081: //
082: while (len > xBuf.length) {
083: processWord(in, inOff);
084:
085: inOff += xBuf.length;
086: len -= xBuf.length;
087: byteCount += xBuf.length;
088: }
089:
090: //
091: // load in the remainder.
092: //
093: while (len > 0) {
094: update(in[inOff]);
095:
096: inOff++;
097: len--;
098: }
099: }
100:
101: public void finish() {
102: long bitLength = (byteCount << 3);
103:
104: //
105: // add the pad bytes.
106: //
107: update((byte) 128);
108:
109: while (xBufOff != 0) {
110: update((byte) 0);
111: }
112:
113: processLength(bitLength);
114:
115: processBlock();
116: }
117:
118: public void reset() {
119: byteCount = 0;
120:
121: xBufOff = 0;
122: for (int i = 0; i < xBuf.length; i++) {
123: xBuf[i] = 0;
124: }
125: }
126:
127: protected abstract void processWord(byte[] in, int inOff);
128:
129: protected abstract void processLength(long bitLength);
130:
131: protected abstract void processBlock();
132: }
|