001 /* MessageDigestSpi.java --- The message digest service provider interface. 002 Copyright (C) 1999, 2005 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 package java.security; 039 040 import java.nio.ByteBuffer; 041 042 /** 043 This is the Service Provider Interface (SPI) for MessageDigest 044 class in java.security. It provides the back end functionality 045 for the MessageDigest class so that it can compute message 046 hashes. The default hashes are SHA-1 and MD5. A message hash 047 takes data of arbitrary length and produces a unique number 048 representing it. 049 050 Cryptography service providers who want to implement their 051 own message digest hashes need only to subclass this class. 052 053 The implementation of a Cloneable interface is left to up to 054 the programmer of a subclass. 055 056 @version 0.0 057 058 @author Mark Benvenuto (ivymccough@worldnet.att.net) 059 */ 060 public abstract class MessageDigestSpi 061 { 062 /** 063 Default constructor of the MessageDigestSpi class 064 */ 065 public MessageDigestSpi() 066 { 067 } 068 069 /** 070 Returns the length of the digest. It may be overridden by the 071 provider to return the length of the digest. Default is to 072 return 0. It is concrete for backwards compatibility with JDK1.1 073 message digest classes. 074 075 @return Length of Digest in Bytes 076 077 @since 1.2 078 */ 079 protected int engineGetDigestLength() 080 { 081 return 0; 082 } 083 084 /** 085 Updates the digest with the specified byte. 086 087 @param input the byte to update digest with 088 */ 089 protected abstract void engineUpdate(byte input); 090 091 092 /** 093 Updates the digest with the specified bytes starting with the 094 offset and proceeding for the specified length. 095 096 @param input the byte array to update digest with 097 @param offset the offset of the byte to start with 098 @param len the number of the bytes to update with 099 */ 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 }