15506fed5029c9fd7ca8d70b6c2272cb8e7e1684
[org.ibex.crypto.git] / src / org / ibex / crypto / Digest.java
1 /* Copyright (c) 2000 The Legion Of The Bouncy Castle 
2  * (http://www.bouncycastle.org)
3  * 
4  * Permission is hereby granted, free of charge, to any person obtaining a 
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation 
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
8  * and/or sell copies of the Software, and to permit persons to whom the 
9  * Software is furnished to do so, subject to the following conditions:
10  * 
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  * 
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
20  * DEALINGS IN THE SOFTWARE.
21  */
22
23 package com.brian_web.crypto;
24
25 /**
26  * base implementation of MD4 family style digest as outlined in
27  * "Handbook of Applied Cryptography", pages 344 - 347.
28  */
29 abstract class Digest
30 {
31     private byte[]  xBuf;
32     private int     xBufOff;
33
34     private long    byteCount;
35
36     /**
37      * Standard constructor
38      */
39     protected Digest()
40     {
41         xBuf = new byte[4];
42         xBufOff = 0;
43     }
44
45     public void update(
46         byte in)
47     {
48         xBuf[xBufOff++] = in;
49
50         if (xBufOff == xBuf.length)
51         {
52             processWord(xBuf, 0);
53             xBufOff = 0;
54         }
55
56         byteCount++;
57     }
58
59     public void update(
60         byte[]  in,
61         int     inOff,
62         int     len)
63     {
64         //
65         // fill the current word
66         //
67         while ((xBufOff != 0) && (len > 0))
68         {
69             update(in[inOff]);
70
71             inOff++;
72             len--;
73         }
74
75         //
76         // process whole words.
77         //
78         while (len > xBuf.length)
79         {
80             processWord(in, inOff);
81
82             inOff += xBuf.length;
83             len -= xBuf.length;
84             byteCount += xBuf.length;
85         }
86
87         //
88         // load in the remainder.
89         //
90         while (len > 0)
91         {
92             update(in[inOff]);
93
94             inOff++;
95             len--;
96         }
97     }
98
99     protected void finish()
100     {
101         long    bitLength = (byteCount << 3);
102
103         //
104         // add the pad bytes.
105         //
106         update((byte)128);
107
108         while (xBufOff != 0)
109         {
110             update((byte)0);
111         }
112
113         processLength(bitLength);
114
115         processBlock();
116     }
117
118     public void reset()
119     {
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 }