KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > digests > GeneralDigest


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.digests;
21
22 /**
23  * base implementation of MD4 family style digest as outlined in
24  * "Handbook of Applied Cryptography", pages 344 - 347.
25  */

26 public abstract class GeneralDigest
27         implements Digest {
28   private byte[] xBuf;
29   private int xBufOff;
30
31   private long byteCount;
32
33   /**
34    * Standard constructor
35    */

36   protected GeneralDigest() {
37     xBuf = new byte[4];
38     xBufOff = 0;
39   }
40
41   /**
42    * Copy constructor. We are using copy constructors in place
43    * of the Object.clone() interface as this interface is not
44    * supported by J2ME.
45    */

46   protected GeneralDigest(GeneralDigest t) {
47     xBuf = new byte[t.xBuf.length];
48     System.arraycopy(t.xBuf, 0, xBuf, 0, t.xBuf.length);
49
50     xBufOff = t.xBufOff;
51     byteCount = t.byteCount;
52   }
53
54   public void update(
55       byte in) {
56     xBuf[xBufOff++] = in;
57
58     if (xBufOff == xBuf.length) {
59       processWord(xBuf, 0);
60       xBufOff = 0;
61     }
62
63     byteCount++;
64   }
65
66   public void update(
67       byte[] in,
68       int inOff,
69       int len) {
70     //
71
// fill the current word
72
//
73
while ( (xBufOff != 0) && (len > 0)) {
74       update(in[inOff]);
75
76       inOff++;
77       len--;
78     }
79
80     //
81
// process whole words.
82
//
83
while (len > xBuf.length) {
84       processWord(in, inOff);
85
86       inOff += xBuf.length;
87       len -= xBuf.length;
88       byteCount += xBuf.length;
89     }
90
91     //
92
// load in the remainder.
93
//
94
while (len > 0) {
95       update(in[inOff]);
96
97       inOff++;
98       len--;
99     }
100   }
101
102   public void finish() {
103     long bitLength = (byteCount << 3);
104
105     //
106
// add the pad bytes.
107
//
108
update( (byte) 128);
109
110     while (xBufOff != 0) {
111       update( (byte) 0);
112     }
113
114     processLength(bitLength);
115
116     processBlock();
117   }
118
119   public void reset() {
120     byteCount = 0;
121
122     xBufOff = 0;
123     for (int i = 0; i < xBuf.length; i++) {
124       xBuf[i] = 0;
125     }
126   }
127
128   protected abstract void processWord(byte[] in, int inOff);
129
130   protected abstract void processLength(long bitLength);
131
132   protected abstract void processBlock();
133
134   public abstract int getDigestSize();
135
136   public abstract int doFinal(byte[] output, int offset);
137
138   public abstract String JavaDoc getAlgorithmName();
139 }
140
Popular Tags