1 /* MessageDigestSpi.java --- The message digest service provider interface.
2 Copyright (C) 1999, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
38 package java
.security
;
40 import java
.nio
.ByteBuffer
;
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
50 Cryptography service providers who want to implement their
51 own message digest hashes need only to subclass this class.
53 The implementation of a Cloneable interface is left to up to
54 the programmer of a subclass.
58 @author Mark Benvenuto (ivymccough@worldnet.att.net)
60 public abstract class MessageDigestSpi
63 Default constructor of the MessageDigestSpi class
65 public MessageDigestSpi()
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.
75 @return Length of Digest in Bytes
79 protected int engineGetDigestLength()
85 Updates the digest with the specified byte.
87 @param input the byte to update digest with
89 protected abstract void engineUpdate(byte input
);
93 Updates the digest with the specified bytes starting with the
94 offset and proceeding for the specified length.
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
100 protected abstract void engineUpdate(byte[]input
, int offset
, int len
);
103 * Updates this digest with the remaining bytes of a byte buffer.
105 * @param input The input buffer.
108 protected void engineUpdate (ByteBuffer input
)
110 byte[] buf
= new byte[1024];
111 while (input
.hasRemaining())
113 int n
= Math
.min(input
.remaining(), buf
.length
);
114 input
.get (buf
, 0, n
);
115 engineUpdate (buf
, 0, n
);
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.
124 @return An array of bytes containing the digest
126 protected abstract byte[] engineDigest();
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.
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
142 protected int engineDigest(byte[]buf
, int offset
, int len
)
143 throws DigestException
145 if (engineGetDigestLength() > len
)
146 throw new DigestException("Buffer is too small.");
148 byte[] tmp
= engineDigest();
149 if (tmp
.length
> len
)
150 throw new DigestException("Buffer is too small");
152 System
.arraycopy(tmp
, 0, buf
, offset
, tmp
.length
);
157 Resets the digest engine. Reinitializes internal variables
158 and clears sensitive data.
160 protected abstract void engineReset();
163 Returns a clone of this class.
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
170 public Object
clone() throws CloneNotSupportedException
172 return super.clone();