1 |
771 |
jeremybenn |
/* MessageDigestSpi.java --- The message digest service provider interface.
|
2 |
|
|
Copyright (C) 1999, 2005 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
package java.security;
|
39 |
|
|
|
40 |
|
|
import java.nio.ByteBuffer;
|
41 |
|
|
|
42 |
|
|
/**
|
43 |
|
|
This is the Service Provider Interface (SPI) for MessageDigest
|
44 |
|
|
class in java.security. It provides the back end functionality
|
45 |
|
|
for the MessageDigest class so that it can compute message
|
46 |
|
|
hashes. The default hashes are SHA-1 and MD5. A message hash
|
47 |
|
|
takes data of arbitrary length and produces a unique number
|
48 |
|
|
representing it.
|
49 |
|
|
|
50 |
|
|
Cryptography service providers who want to implement their
|
51 |
|
|
own message digest hashes need only to subclass this class.
|
52 |
|
|
|
53 |
|
|
The implementation of a Cloneable interface is left to up to
|
54 |
|
|
the programmer of a subclass.
|
55 |
|
|
|
56 |
|
|
@version 0.0
|
57 |
|
|
|
58 |
|
|
@author Mark Benvenuto (ivymccough@worldnet.att.net)
|
59 |
|
|
*/
|
60 |
|
|
public abstract class MessageDigestSpi
|
61 |
|
|
{
|
62 |
|
|
/**
|
63 |
|
|
Default constructor of the MessageDigestSpi class
|
64 |
|
|
*/
|
65 |
|
|
public MessageDigestSpi()
|
66 |
|
|
{
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
/**
|
70 |
|
|
Returns the length of the digest. It may be overridden by the
|
71 |
|
|
provider to return the length of the digest. Default is to
|
72 |
|
|
return 0. It is concrete for backwards compatibility with JDK1.1
|
73 |
|
|
message digest classes.
|
74 |
|
|
|
75 |
|
|
@return Length of Digest in Bytes
|
76 |
|
|
|
77 |
|
|
@since 1.2
|
78 |
|
|
*/
|
79 |
|
|
protected int engineGetDigestLength()
|
80 |
|
|
{
|
81 |
|
|
return 0;
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
/**
|
85 |
|
|
Updates the digest with the specified byte.
|
86 |
|
|
|
87 |
|
|
@param input the byte to update digest with
|
88 |
|
|
*/
|
89 |
|
|
protected abstract void engineUpdate(byte input);
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
/**
|
93 |
|
|
Updates the digest with the specified bytes starting with the
|
94 |
|
|
offset and proceeding for the specified length.
|
95 |
|
|
|
96 |
|
|
@param input the byte array to update digest with
|
97 |
|
|
@param offset the offset of the byte to start with
|
98 |
|
|
@param len the number of the bytes to update with
|
99 |
|
|
*/
|
100 |
|
|
protected abstract void engineUpdate(byte[]input, int offset, int len);
|
101 |
|
|
|
102 |
|
|
/**
|
103 |
|
|
* Updates this digest with the remaining bytes of a byte buffer.
|
104 |
|
|
*
|
105 |
|
|
* @param input The input buffer.
|
106 |
|
|
* @since 1.5
|
107 |
|
|
*/
|
108 |
|
|
protected void engineUpdate (ByteBuffer input)
|
109 |
|
|
{
|
110 |
|
|
byte[] buf = new byte[1024];
|
111 |
|
|
while (input.hasRemaining())
|
112 |
|
|
{
|
113 |
|
|
int n = Math.min(input.remaining(), buf.length);
|
114 |
|
|
input.get (buf, 0, n);
|
115 |
|
|
engineUpdate (buf, 0, n);
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
/**
|
120 |
|
|
Computes the final digest of the stored bytes and returns
|
121 |
|
|
them. It performs any necessary padding. The message digest
|
122 |
|
|
should reset sensitive data after performing the digest.
|
123 |
|
|
|
124 |
|
|
@return An array of bytes containing the digest
|
125 |
|
|
*/
|
126 |
|
|
protected abstract byte[] engineDigest();
|
127 |
|
|
|
128 |
|
|
/**
|
129 |
|
|
Computes the final digest of the stored bytes and returns
|
130 |
|
|
them. It performs any necessary padding. The message digest
|
131 |
|
|
should reset sensitive data after performing the digest. This
|
132 |
|
|
method is left concrete for backwards compatibility with JDK1.1
|
133 |
|
|
message digest classes.
|
134 |
|
|
|
135 |
|
|
@param buf An array of bytes to store the digest
|
136 |
|
|
@param offset An offset to start storing the digest at
|
137 |
|
|
@param len The length of the buffer
|
138 |
|
|
@return Returns the length of the buffer
|
139 |
|
|
|
140 |
|
|
@since 1.2
|
141 |
|
|
*/
|
142 |
|
|
protected int engineDigest(byte[]buf, int offset, int len)
|
143 |
|
|
throws DigestException
|
144 |
|
|
{
|
145 |
|
|
if (engineGetDigestLength() > len)
|
146 |
|
|
throw new DigestException("Buffer is too small.");
|
147 |
|
|
|
148 |
|
|
byte[] tmp = engineDigest();
|
149 |
|
|
if (tmp.length > len)
|
150 |
|
|
throw new DigestException("Buffer is too small");
|
151 |
|
|
|
152 |
|
|
System.arraycopy(tmp, 0, buf, offset, tmp.length);
|
153 |
|
|
return tmp.length;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
/**
|
157 |
|
|
Resets the digest engine. Reinitializes internal variables
|
158 |
|
|
and clears sensitive data.
|
159 |
|
|
*/
|
160 |
|
|
protected abstract void engineReset();
|
161 |
|
|
|
162 |
|
|
/**
|
163 |
|
|
Returns a clone of this class.
|
164 |
|
|
|
165 |
|
|
If cloning is not supported, then by default the class throws a
|
166 |
|
|
CloneNotSupportedException. The MessageDigestSpi provider
|
167 |
|
|
implementation has to overload this class in order to be
|
168 |
|
|
cloneable.
|
169 |
|
|
*/
|
170 |
|
|
public Object clone() throws CloneNotSupportedException
|
171 |
|
|
{
|
172 |
|
|
return super.clone();
|
173 |
|
|
}
|
174 |
|
|
}
|