1 /* MessageDigestSpi.java --- The message digest service provider interface. 2 Copyright (C) 1999 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., 59 Temple Place, Suite 330, Boston, MA 19 02111-1307 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 /** 41 This is the Service Provider Interface (SPI) for MessageDigest 42 class in java.security. It provides the back end functionality 43 for the MessageDigest class so that it can compute message 44 hashes. The default hashes are SHA-1 and MD5. A message hash 45 takes data of arbitrary length and produces a unique number 46 representing it. 47 48 Cryptography service providers who want to implement their 49 own message digest hashes need only to subclass this class. 50 51 The implementation of a Cloneable interface is left to up to 52 the programmer of a subclass. 53 54 @version 0.0 55 56 @author Mark Benvenuto <ivymccough@worldnet.att.net> 57 */ 58 public abstract class MessageDigestSpi 59 { 60 /** 61 Default constructor of the MessageDigestSpi class 62 */ MessageDigestSpi()63 public MessageDigestSpi() 64 { 65 } 66 67 /** 68 Returns the length of the digest. It may be overridden by the 69 provider to return the length of the digest. Default is to 70 return 0. It is concrete for backwards compatibility with JDK1.1 71 message digest classes. 72 73 @return Length of Digest in Bytes 74 75 @since 1.2 76 */ engineGetDigestLength()77 protected int engineGetDigestLength() 78 { 79 return 0; 80 } 81 82 /** 83 Updates the digest with the specified byte. 84 85 @param input the byte to update digest with 86 */ engineUpdate(byte input)87 protected abstract void engineUpdate(byte input); 88 89 90 /** 91 Updates the digest with the specified bytes starting with the 92 offset and proceeding for the specified length. 93 94 @param input the byte array to update digest with 95 @param offset the offset of the byte to start with 96 @param len the number of the bytes to update with 97 */ engineUpdate(byte[]input, int offset, int len)98 protected abstract void engineUpdate(byte[]input, int offset, int len); 99 100 /** 101 Computes the final digest of the stored bytes and returns 102 them. It performs any necessary padding. The message digest 103 should reset sensitive data after performing the digest. 104 105 @return An array of bytes containing the digest 106 */ engineDigest()107 protected abstract byte[] engineDigest(); 108 109 /** 110 Computes the final digest of the stored bytes and returns 111 them. It performs any necessary padding. The message digest 112 should reset sensitive data after performing the digest. This 113 method is left concrete for backwards compatibility with JDK1.1 114 message digest classes. 115 116 @param buf An array of bytes to store the digest 117 @param offset An offset to start storing the digest at 118 @param len The length of the buffer 119 @return Returns the length of the buffer 120 121 @since 1.2 122 */ engineDigest(byte[]buf, int offset, int len)123 protected int engineDigest(byte[]buf, int offset, int len) 124 throws DigestException 125 { 126 if (engineGetDigestLength() > len) 127 throw new DigestException("Buffer is too small."); 128 129 byte tmp[] = engineDigest(); 130 if (tmp.length > len) 131 throw new DigestException("Buffer is too small"); 132 133 System.arraycopy(tmp, 0, buf, offset, tmp.length); 134 return tmp.length; 135 } 136 137 /** 138 Resets the digest engine. Reinitializes internal variables 139 and clears sensitive data. 140 */ engineReset()141 protected abstract void engineReset(); 142 143 /** 144 Returns a clone of this class. 145 146 If cloning is not supported, then by default the class throws a 147 CloneNotSupportedException. The MessageDigestSpi provider 148 implementation has to overload this class in order to be 149 cloneable. 150 */ clone()151 public Object clone() throws CloneNotSupportedException 152 { 153 if (this instanceof Cloneable) 154 return super.clone(); 155 else 156 throw new CloneNotSupportedException(); 157 } 158 } 159