1 /*
2 * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
382 */
383 public byte[] digest() {
384 /* Resetting is the responsibility of implementors. */
385 byte[] result = engineDigest();
386 state = INITIAL;
387 return result;
388 }
389
390 /**
391 * Completes the hash computation by performing final operations
392 * such as padding. The digest is reset after this call is made.
393 *
394 * @param buf output buffer for the computed digest
395 *
396 * @param offset offset into the output buffer to begin storing the digest
397 *
398 * @param len number of bytes within buf allotted for the digest
399 *
400 * @return the number of bytes placed into {@code buf}
401 *
402 * @exception DigestException if an error occurs.
403 */
404 public int digest(byte[] buf, int offset, int len) throws DigestException {
405 if (buf == null) {
406 throw new IllegalArgumentException("No output buffer given");
407 }
408 if (buf.length - offset < len) {
409 throw new IllegalArgumentException
410 ("Output buffer too small for specified offset and length");
411 }
412 int numBytes = engineDigest(buf, offset, len);
413 state = INITIAL;
414 return numBytes;
415 }
416
417 /**
418 * Performs a final update on the digest using the specified array
419 * of bytes, then completes the digest computation. That is, this
420 * method first calls {@link #update(byte[]) update(input)},
421 * passing the <i>input</i> array to the {@code update} method,
422 * then calls {@link #digest() digest()}.
519 */
520 public final int getDigestLength() {
521 int digestLen = engineGetDigestLength();
522 if (digestLen == 0) {
523 try {
524 MessageDigest md = (MessageDigest)clone();
525 byte[] digest = md.digest();
526 return digest.length;
527 } catch (CloneNotSupportedException e) {
528 return digestLen;
529 }
530 }
531 return digestLen;
532 }
533
534 /**
535 * Returns a clone if the implementation is cloneable.
536 *
537 * @return a clone if the implementation is cloneable.
538 *
539 * @exception CloneNotSupportedException if this is called on an
540 * implementation that does not support {@code Cloneable}.
541 */
542 public Object clone() throws CloneNotSupportedException {
543 if (this instanceof Cloneable) {
544 return super.clone();
545 } else {
546 throw new CloneNotSupportedException();
547 }
548 }
549
550
551
552
553 /*
554 * The following class allows providers to extend from MessageDigestSpi
555 * rather than from MessageDigest. It represents a MessageDigest with an
556 * encapsulated, provider-supplied SPI object (of type MessageDigestSpi).
557 * If the provider implementation is an instance of MessageDigestSpi,
558 * the getInstance() methods above return an instance of this class, with
559 * the SPI object encapsulated.
563 * been interposed in the hierarchy between the API (MessageDigest)
564 * and its original parent (Object).
565 */
566
567 static class Delegate extends MessageDigest implements MessageDigestSpi2 {
568
569 // The provider implementation (delegate)
570 private MessageDigestSpi digestSpi;
571
572 // constructor
573 public Delegate(MessageDigestSpi digestSpi, String algorithm) {
574 super(algorithm);
575 this.digestSpi = digestSpi;
576 }
577
578 /**
579 * Returns a clone if the delegate is cloneable.
580 *
581 * @return a clone if the delegate is cloneable.
582 *
583 * @exception CloneNotSupportedException if this is called on a
584 * delegate that does not support {@code Cloneable}.
585 */
586 public Object clone() throws CloneNotSupportedException {
587 if (digestSpi instanceof Cloneable) {
588 MessageDigestSpi digestSpiClone =
589 (MessageDigestSpi)digestSpi.clone();
590 // Because 'algorithm', 'provider', and 'state' are private
591 // members of our supertype, we must perform a cast to
592 // access them.
593 MessageDigest that =
594 new Delegate(digestSpiClone,
595 ((MessageDigest)this).algorithm);
596 that.provider = ((MessageDigest)this).provider;
597 that.state = ((MessageDigest)this).state;
598 return that;
599 } else {
600 throw new CloneNotSupportedException();
601 }
602 }
603
|
1 /*
2 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
382 */
383 public byte[] digest() {
384 /* Resetting is the responsibility of implementors. */
385 byte[] result = engineDigest();
386 state = INITIAL;
387 return result;
388 }
389
390 /**
391 * Completes the hash computation by performing final operations
392 * such as padding. The digest is reset after this call is made.
393 *
394 * @param buf output buffer for the computed digest
395 *
396 * @param offset offset into the output buffer to begin storing the digest
397 *
398 * @param len number of bytes within buf allotted for the digest
399 *
400 * @return the number of bytes placed into {@code buf}
401 *
402 * @throws DigestException if an error occurs.
403 */
404 public int digest(byte[] buf, int offset, int len) throws DigestException {
405 if (buf == null) {
406 throw new IllegalArgumentException("No output buffer given");
407 }
408 if (buf.length - offset < len) {
409 throw new IllegalArgumentException
410 ("Output buffer too small for specified offset and length");
411 }
412 int numBytes = engineDigest(buf, offset, len);
413 state = INITIAL;
414 return numBytes;
415 }
416
417 /**
418 * Performs a final update on the digest using the specified array
419 * of bytes, then completes the digest computation. That is, this
420 * method first calls {@link #update(byte[]) update(input)},
421 * passing the <i>input</i> array to the {@code update} method,
422 * then calls {@link #digest() digest()}.
519 */
520 public final int getDigestLength() {
521 int digestLen = engineGetDigestLength();
522 if (digestLen == 0) {
523 try {
524 MessageDigest md = (MessageDigest)clone();
525 byte[] digest = md.digest();
526 return digest.length;
527 } catch (CloneNotSupportedException e) {
528 return digestLen;
529 }
530 }
531 return digestLen;
532 }
533
534 /**
535 * Returns a clone if the implementation is cloneable.
536 *
537 * @return a clone if the implementation is cloneable.
538 *
539 * @throws CloneNotSupportedException if this is called on an
540 * implementation that does not support {@code Cloneable}.
541 */
542 public Object clone() throws CloneNotSupportedException {
543 if (this instanceof Cloneable) {
544 return super.clone();
545 } else {
546 throw new CloneNotSupportedException();
547 }
548 }
549
550
551
552
553 /*
554 * The following class allows providers to extend from MessageDigestSpi
555 * rather than from MessageDigest. It represents a MessageDigest with an
556 * encapsulated, provider-supplied SPI object (of type MessageDigestSpi).
557 * If the provider implementation is an instance of MessageDigestSpi,
558 * the getInstance() methods above return an instance of this class, with
559 * the SPI object encapsulated.
563 * been interposed in the hierarchy between the API (MessageDigest)
564 * and its original parent (Object).
565 */
566
567 static class Delegate extends MessageDigest implements MessageDigestSpi2 {
568
569 // The provider implementation (delegate)
570 private MessageDigestSpi digestSpi;
571
572 // constructor
573 public Delegate(MessageDigestSpi digestSpi, String algorithm) {
574 super(algorithm);
575 this.digestSpi = digestSpi;
576 }
577
578 /**
579 * Returns a clone if the delegate is cloneable.
580 *
581 * @return a clone if the delegate is cloneable.
582 *
583 * @throws CloneNotSupportedException if this is called on a
584 * delegate that does not support {@code Cloneable}.
585 */
586 public Object clone() throws CloneNotSupportedException {
587 if (digestSpi instanceof Cloneable) {
588 MessageDigestSpi digestSpiClone =
589 (MessageDigestSpi)digestSpi.clone();
590 // Because 'algorithm', 'provider', and 'state' are private
591 // members of our supertype, we must perform a cast to
592 // access them.
593 MessageDigest that =
594 new Delegate(digestSpiClone,
595 ((MessageDigest)this).algorithm);
596 that.provider = ((MessageDigest)this).provider;
597 that.state = ((MessageDigest)this).state;
598 return that;
599 } else {
600 throw new CloneNotSupportedException();
601 }
602 }
603
|