1 7 8 package java.security; 9 10 import java.security.spec.AlgorithmParameterSpec ; 11 import java.util.*; 12 import java.io.*; 13 14 import java.nio.ByteBuffer ; 15 16 import sun.security.jca.JCAUtil; 17 18 34 35 public abstract class SignatureSpi { 36 37 40 protected SecureRandom appRandom = null; 41 42 52 protected abstract void engineInitVerify(PublicKey publicKey) 53 throws InvalidKeyException ; 54 55 65 protected abstract void engineInitSign(PrivateKey privateKey) 66 throws InvalidKeyException ; 67 68 82 protected void engineInitSign(PrivateKey privateKey, 83 SecureRandom random) 84 throws InvalidKeyException { 85 this.appRandom = random; 86 engineInitSign(privateKey); 87 } 88 89 98 protected abstract void engineUpdate(byte b) throws SignatureException ; 99 100 111 protected abstract void engineUpdate(byte[] b, int off, int len) 112 throws SignatureException ; 113 114 124 protected void engineUpdate(ByteBuffer input) { 125 if (input.hasRemaining() == false) { 126 return; 127 } 128 try { 129 if (input.hasArray()) { 130 byte[] b = input.array(); 131 int ofs = input.arrayOffset(); 132 int pos = input.position(); 133 int lim = input.limit(); 134 engineUpdate(b, ofs + pos, lim - pos); 135 input.position(lim); 136 } else { 137 int len = input.remaining(); 138 byte[] b = new byte[JCAUtil.getTempArraySize(len)]; 139 while (len > 0) { 140 int chunk = Math.min(len, b.length); 141 input.get(b, 0, chunk); 142 engineUpdate(b, 0, chunk); 143 len -= chunk; 144 } 145 } 146 } catch (SignatureException e) { 147 throw new ProviderException ("update() failed", e); 150 } 151 } 152 153 165 protected abstract byte[] engineSign() throws SignatureException ; 166 167 207 protected int engineSign(byte[] outbuf, int offset, int len) 208 throws SignatureException { 209 byte[] sig = engineSign(); 210 if (len < sig.length) { 211 throw new SignatureException 212 ("partial signatures not returned"); 213 } 214 if (outbuf.length - offset < sig.length) { 215 throw new SignatureException 216 ("insufficient space in the output buffer to store the " 217 + "signature"); 218 } 219 System.arraycopy(sig, 0, outbuf, offset, sig.length); 220 return sig.length; 221 } 222 223 235 protected abstract boolean engineVerify(byte[] sigBytes) 236 throws SignatureException ; 237 238 256 protected boolean engineVerify(byte[] sigBytes, int offset, int length) 257 throws SignatureException { 258 byte[] sigBytesCopy = new byte[length]; 259 System.arraycopy(sigBytes, offset, sigBytesCopy, 0, length); 260 return engineVerify(sigBytesCopy); 261 } 262 263 287 @Deprecated 288 protected abstract void engineSetParameter(String param, Object value) 289 throws InvalidParameterException ; 290 291 304 protected void engineSetParameter(AlgorithmParameterSpec params) 305 throws InvalidAlgorithmParameterException { 306 throw new UnsupportedOperationException (); 307 } 308 309 326 protected AlgorithmParameters engineGetParameters() { 327 throw new UnsupportedOperationException (); 328 } 329 330 352 @Deprecated 353 protected abstract Object engineGetParameter(String param) 354 throws InvalidParameterException ; 355 356 364 public Object clone() throws CloneNotSupportedException { 365 if (this instanceof Cloneable ) { 366 return super.clone(); 367 } else { 368 throw new CloneNotSupportedException (); 369 } 370 } 371 } 372 373 374 375 376 377 378 379 380 | Popular Tags |