01: ///////////////////////////////////////////////////////////////////////////////
02: //
03: // This program is free software; you can redistribute it and/or modify
04: // it under the terms of the GNU General Public License and GNU Library
05: // General Public License as published by the Free Software Foundation;
06: // either version 2, or (at your option) any later version.
07: //
08: // This program is distributed in the hope that it will be useful,
09: // but WITHOUT ANY WARRANTY; without even the implied warranty of
10: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11: // GNU General Public License and GNU Library General Public License
12: // for more details.
13: //
14: // You should have received a copy of the GNU General Public License
15: // and GNU Library General Public License along with this program; if
16: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
17: // MA 02139, USA.
18: //
19: ///////////////////////////////////////////////////////////////////////////////
20:
21: package org.rdesktop.server.rdp.crypto;
22:
23: public abstract class BlockMessageDigest {
24: private static final int MAX_COUNT = (1 << 28) - 1;
25:
26: private int m_count;
27: private int m_data_length;
28: private byte[] m_buffer;
29: private int m_buffered;
30: private String m_algorithm;
31:
32: protected abstract void engineTransform(byte[] data);
33:
34: protected abstract int engineGetDataLength();
35:
36: protected BlockMessageDigest(String algorithm) {
37: m_algorithm = algorithm;
38: m_data_length = engineGetDataLength();
39: m_buffer = new byte[m_data_length];
40: }
41:
42: protected int bitcount() {
43: return m_count * 8;
44: }
45:
46: protected String getAlgorithm() {
47: return m_algorithm;
48: }
49:
50: public abstract byte[] engineDigest(byte[] data, int length);
51:
52: public void engineReset() {
53: m_buffered = 0;
54: m_count = 0;
55: }
56:
57: public void engineUpdate(byte b) throws CryptoException {
58: byte[] data = { b };
59: engineUpdate(data, 0, 1);
60: }
61:
62: public void engineUpdate(byte[] data, int offset, int length)
63: throws CryptoException {
64: m_count += length;
65: if (m_count > MAX_COUNT) {
66: throw new CryptoException(getAlgorithm()
67: + ": Maximum input length exceeded");
68: }
69:
70: int datalen = m_data_length;
71: int remainder;
72:
73: while (length >= (remainder = datalen - m_buffered)) {
74: System.arraycopy(data, offset, m_buffer, m_buffered,
75: remainder);
76: engineTransform(m_buffer);
77: length -= remainder;
78: offset += remainder;
79: m_buffered = 0;
80: }
81:
82: if (length > 0) {
83: System
84: .arraycopy(data, offset, m_buffer, m_buffered,
85: length);
86: m_buffered += length;
87: }
88: }
89:
90: public byte[] engineDigest() {
91: return engineDigest(m_buffer, m_buffered);
92: }
93: }
|