1 7 8 package java.security; 9 10 import java.util.*; 11 import java.lang.*; 12 import java.io.IOException ; 13 import java.io.ByteArrayOutputStream ; 14 import java.io.PrintStream ; 15 import java.io.InputStream ; 16 import java.io.ByteArrayInputStream ; 17 18 import java.nio.ByteBuffer ; 19 20 73 74 public abstract class MessageDigest extends MessageDigestSpi { 75 76 private String algorithm; 77 78 private static final int INITIAL = 0; 80 private static final int IN_PROGRESS = 1; 81 private int state = INITIAL; 82 83 private Provider provider; 85 86 95 protected MessageDigest(String algorithm) { 96 this.algorithm = algorithm; 97 } 98 99 119 public static MessageDigest getInstance(String algorithm) 120 throws NoSuchAlgorithmException { 121 try { 122 Object [] objs = Security.getImpl(algorithm, "MessageDigest", 123 (String )null); 124 if (objs[0] instanceof MessageDigest ) { 125 MessageDigest md = (MessageDigest )objs[0]; 126 md.provider = (Provider )objs[1]; 127 return md; 128 } else { 129 MessageDigest delegate = 130 new Delegate((MessageDigestSpi )objs[0], algorithm); 131 delegate.provider = (Provider )objs[1]; 132 return delegate; 133 } 134 } catch(NoSuchProviderException e) { 135 throw new NoSuchAlgorithmException (algorithm + " not found"); 136 } 137 } 138 139 167 public static MessageDigest getInstance(String algorithm, String provider) 168 throws NoSuchAlgorithmException , NoSuchProviderException 169 { 170 if (provider == null || provider.length() == 0) 171 throw new IllegalArgumentException ("missing provider"); 172 Object [] objs = Security.getImpl(algorithm, "MessageDigest", provider); 173 if (objs[0] instanceof MessageDigest ) { 174 MessageDigest md = (MessageDigest )objs[0]; 175 md.provider = (Provider )objs[1]; 176 return md; 177 } else { 178 MessageDigest delegate = 179 new Delegate((MessageDigestSpi )objs[0], algorithm); 180 delegate.provider = (Provider )objs[1]; 181 return delegate; 182 } 183 } 184 185 213 public static MessageDigest getInstance(String algorithm, 214 Provider provider) 215 throws NoSuchAlgorithmException 216 { 217 if (provider == null) 218 throw new IllegalArgumentException ("missing provider"); 219 Object [] objs = Security.getImpl(algorithm, "MessageDigest", provider); 220 if (objs[0] instanceof MessageDigest ) { 221 MessageDigest md = (MessageDigest )objs[0]; 222 md.provider = (Provider )objs[1]; 223 return md; 224 } else { 225 MessageDigest delegate = 226 new Delegate((MessageDigestSpi )objs[0], algorithm); 227 delegate.provider = (Provider )objs[1]; 228 return delegate; 229 } 230 } 231 232 237 public final Provider getProvider() { 238 return this.provider; 239 } 240 241 246 public void update(byte input) { 247 engineUpdate(input); 248 state = IN_PROGRESS; 249 } 250 251 262 public void update(byte[] input, int offset, int len) { 263 if (input == null) { 264 throw new IllegalArgumentException ("No input buffer given"); 265 } 266 if (input.length - offset < len) { 267 throw new IllegalArgumentException ("Input buffer too short"); 268 } 269 engineUpdate(input, offset, len); 270 state = IN_PROGRESS; 271 } 272 273 278 public void update(byte[] input) { 279 engineUpdate(input, 0, input.length); 280 state = IN_PROGRESS; 281 } 282 283 293 public final void update(ByteBuffer input) { 294 if (input == null) { 295 throw new NullPointerException (); 296 } 297 engineUpdate(input); 298 state = IN_PROGRESS; 299 } 300 301 307 public byte[] digest() { 308 309 byte[] result = engineDigest(); 310 state = INITIAL; 311 return result; 312 } 313 314 328 public int digest(byte[] buf, int offset, int len) throws DigestException { 329 if (buf == null) { 330 throw new IllegalArgumentException ("No output buffer given"); 331 } 332 if (buf.length - offset < len) { 333 throw new IllegalArgumentException 334 ("Output buffer too small for specified offset and length"); 335 } 336 int numBytes = engineDigest(buf, offset, len); 337 state = INITIAL; 338 return numBytes; 339 } 340 341 353 public byte[] digest(byte[] input) { 354 update(input); 355 return digest(); 356 } 357 358 361 public String toString() { 362 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 363 PrintStream p = new PrintStream (baos); 364 p.print(algorithm+" Message Digest from "+provider.getName()+", "); 365 switch (state) { 366 case INITIAL: 367 p.print("<initialized>"); 368 break; 369 case IN_PROGRESS: 370 p.print("<in progress>"); 371 break; 372 } 373 p.println(); 374 return (baos.toString()); 375 } 376 377 386 public static boolean isEqual(byte digesta[], byte digestb[]) { 387 if (digesta.length != digestb.length) 388 return false; 389 390 for (int i = 0; i < digesta.length; i++) { 391 if (digesta[i] != digestb[i]) { 392 return false; 393 } 394 } 395 return true; 396 } 397 398 401 public void reset() { 402 engineReset(); 403 state = INITIAL; 404 } 405 406 417 public final String getAlgorithm() { 418 return this.algorithm; 419 } 420 421 430 public final int getDigestLength() { 431 int digestLen = engineGetDigestLength(); 432 if (digestLen == 0) { 433 try { 434 MessageDigest md = (MessageDigest )clone(); 435 byte[] digest = md.digest(); 436 return digest.length; 437 } catch (CloneNotSupportedException e) { 438 return digestLen; 439 } 440 } 441 return digestLen; 442 } 443 444 452 public Object clone() throws CloneNotSupportedException { 453 if (this instanceof Cloneable ) { 454 return super.clone(); 455 } else { 456 throw new CloneNotSupportedException (); 457 } 458 } 459 460 461 462 463 476 477 static class Delegate extends MessageDigest { 478 479 private MessageDigestSpi digestSpi; 481 482 public Delegate(MessageDigestSpi digestSpi, String algorithm) { 484 super(algorithm); 485 this.digestSpi = digestSpi; 486 } 487 488 496 public Object clone() throws CloneNotSupportedException { 497 if (digestSpi instanceof Cloneable ) { 498 MessageDigestSpi digestSpiClone = 499 (MessageDigestSpi )digestSpi.clone(); 500 MessageDigest that = 504 new Delegate(digestSpiClone, 505 ((MessageDigest )this).algorithm); 506 that.provider = ((MessageDigest )this).provider; 507 that.state = ((MessageDigest )this).state; 508 return that; 509 } else { 510 throw new CloneNotSupportedException (); 511 } 512 } 513 514 protected int engineGetDigestLength() { 515 return digestSpi.engineGetDigestLength(); 516 } 517 518 protected void engineUpdate(byte input) { 519 digestSpi.engineUpdate(input); 520 } 521 522 protected void engineUpdate(byte[] input, int offset, int len) { 523 digestSpi.engineUpdate(input, offset, len); 524 } 525 526 protected void engineUpdate(ByteBuffer input) { 527 digestSpi.engineUpdate(input); 528 } 529 530 protected byte[] engineDigest() { 531 return digestSpi.engineDigest(); 532 } 533 534 protected int engineDigest(byte[] buf, int offset, int len) 535 throws DigestException { 536 return digestSpi.engineDigest(buf, offset, len); 537 } 538 539 protected void engineReset() { 540 digestSpi.engineReset(); 541 } 542 } 543 } 544 | Popular Tags |