< prev index next >

src/java.base/share/classes/java/security/MessageDigest.java

Print this page




  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
  23  * questions.
  24  */
  25 
  26 package java.security;
  27 
  28 import java.util.*;
  29 import java.lang.*;
  30 import java.io.IOException;
  31 import java.io.ByteArrayOutputStream;
  32 import java.io.PrintStream;
  33 import java.io.InputStream;
  34 import java.io.ByteArrayInputStream;
  35 
  36 import java.nio.ByteBuffer;
  37 
  38 import sun.security.util.Debug;

  39 


  40 /**
  41  * This MessageDigest class provides applications the functionality of a
  42  * message digest algorithm, such as SHA-1 or SHA-256.
  43  * Message digests are secure one-way hash functions that take arbitrary-sized
  44  * data and output a fixed-length hash value.
  45  *
  46  * <p>A MessageDigest object starts out initialized. The data is
  47  * processed through it using the {@link #update(byte) update}
  48  * methods. At any point {@link #reset() reset} can be called
  49  * to reset the digest. Once all the data to be updated has been
  50  * updated, one of the {@link #digest() digest} methods should
  51  * be called to complete the hash computation.
  52  *
  53  * <p>The {@code digest} method can be called once for a given number
  54  * of updates. After {@code digest} has been called, the MessageDigest
  55  * object is reset to its initialized state.
  56  *
  57  * <p>Implementations are free to implement the Cloneable interface.
  58  * Client applications can test cloneability by attempting cloning
  59  * and catching the CloneNotSupportedException:


 531         }
 532     }
 533 
 534 
 535 
 536 
 537     /*
 538      * The following class allows providers to extend from MessageDigestSpi
 539      * rather than from MessageDigest. It represents a MessageDigest with an
 540      * encapsulated, provider-supplied SPI object (of type MessageDigestSpi).
 541      * If the provider implementation is an instance of MessageDigestSpi,
 542      * the getInstance() methods above return an instance of this class, with
 543      * the SPI object encapsulated.
 544      *
 545      * Note: All SPI methods from the original MessageDigest class have been
 546      * moved up the hierarchy into a new class (MessageDigestSpi), which has
 547      * been interposed in the hierarchy between the API (MessageDigest)
 548      * and its original parent (Object).
 549      */
 550 
 551     static class Delegate extends MessageDigest {
 552 
 553         // The provider implementation (delegate)
 554         private MessageDigestSpi digestSpi;
 555 
 556         // constructor
 557         public Delegate(MessageDigestSpi digestSpi, String algorithm) {
 558             super(algorithm);
 559             this.digestSpi = digestSpi;
 560         }
 561 
 562         /**
 563          * Returns a clone if the delegate is cloneable.
 564          *
 565          * @return a clone if the delegate is cloneable.
 566          *
 567          * @exception CloneNotSupportedException if this is called on a
 568          * delegate that does not support {@code Cloneable}.
 569          */
 570         public Object clone() throws CloneNotSupportedException {
 571             if (digestSpi instanceof Cloneable) {


 584                 throw new CloneNotSupportedException();
 585             }
 586         }
 587 
 588         protected int engineGetDigestLength() {
 589             return digestSpi.engineGetDigestLength();
 590         }
 591 
 592         protected void engineUpdate(byte input) {
 593             digestSpi.engineUpdate(input);
 594         }
 595 
 596         protected void engineUpdate(byte[] input, int offset, int len) {
 597             digestSpi.engineUpdate(input, offset, len);
 598         }
 599 
 600         protected void engineUpdate(ByteBuffer input) {
 601             digestSpi.engineUpdate(input);
 602         }
 603 








 604         protected byte[] engineDigest() {
 605             return digestSpi.engineDigest();
 606         }
 607 
 608         protected int engineDigest(byte[] buf, int offset, int len)
 609             throws DigestException {
 610                 return digestSpi.engineDigest(buf, offset, len);
 611         }
 612 
 613         protected void engineReset() {
 614             digestSpi.engineReset();
 615         }
 616     }
 617 }


  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
  23  * questions.
  24  */
  25 
  26 package java.security;
  27 
  28 import java.util.*;
  29 import java.lang.*;
  30 import java.io.IOException;
  31 import java.io.ByteArrayOutputStream;
  32 import java.io.PrintStream;
  33 import java.io.InputStream;
  34 import java.io.ByteArrayInputStream;
  35 import java.security.InvalidKeyException;
  36 import java.nio.ByteBuffer;
  37 
  38 import sun.security.util.Debug;
  39 import sun.security.util.MessageDigestSpi2;
  40 
  41 import javax.crypto.SecretKey;
  42 
  43 /**
  44  * This MessageDigest class provides applications the functionality of a
  45  * message digest algorithm, such as SHA-1 or SHA-256.
  46  * Message digests are secure one-way hash functions that take arbitrary-sized
  47  * data and output a fixed-length hash value.
  48  *
  49  * <p>A MessageDigest object starts out initialized. The data is
  50  * processed through it using the {@link #update(byte) update}
  51  * methods. At any point {@link #reset() reset} can be called
  52  * to reset the digest. Once all the data to be updated has been
  53  * updated, one of the {@link #digest() digest} methods should
  54  * be called to complete the hash computation.
  55  *
  56  * <p>The {@code digest} method can be called once for a given number
  57  * of updates. After {@code digest} has been called, the MessageDigest
  58  * object is reset to its initialized state.
  59  *
  60  * <p>Implementations are free to implement the Cloneable interface.
  61  * Client applications can test cloneability by attempting cloning
  62  * and catching the CloneNotSupportedException:


 534         }
 535     }
 536 
 537 
 538 
 539 
 540     /*
 541      * The following class allows providers to extend from MessageDigestSpi
 542      * rather than from MessageDigest. It represents a MessageDigest with an
 543      * encapsulated, provider-supplied SPI object (of type MessageDigestSpi).
 544      * If the provider implementation is an instance of MessageDigestSpi,
 545      * the getInstance() methods above return an instance of this class, with
 546      * the SPI object encapsulated.
 547      *
 548      * Note: All SPI methods from the original MessageDigest class have been
 549      * moved up the hierarchy into a new class (MessageDigestSpi), which has
 550      * been interposed in the hierarchy between the API (MessageDigest)
 551      * and its original parent (Object).
 552      */
 553 
 554     static class Delegate extends MessageDigest implements MessageDigestSpi2 {
 555 
 556         // The provider implementation (delegate)
 557         private MessageDigestSpi digestSpi;
 558 
 559         // constructor
 560         public Delegate(MessageDigestSpi digestSpi, String algorithm) {
 561             super(algorithm);
 562             this.digestSpi = digestSpi;
 563         }
 564 
 565         /**
 566          * Returns a clone if the delegate is cloneable.
 567          *
 568          * @return a clone if the delegate is cloneable.
 569          *
 570          * @exception CloneNotSupportedException if this is called on a
 571          * delegate that does not support {@code Cloneable}.
 572          */
 573         public Object clone() throws CloneNotSupportedException {
 574             if (digestSpi instanceof Cloneable) {


 587                 throw new CloneNotSupportedException();
 588             }
 589         }
 590 
 591         protected int engineGetDigestLength() {
 592             return digestSpi.engineGetDigestLength();
 593         }
 594 
 595         protected void engineUpdate(byte input) {
 596             digestSpi.engineUpdate(input);
 597         }
 598 
 599         protected void engineUpdate(byte[] input, int offset, int len) {
 600             digestSpi.engineUpdate(input, offset, len);
 601         }
 602 
 603         protected void engineUpdate(ByteBuffer input) {
 604             digestSpi.engineUpdate(input);
 605         }
 606 
 607         public void engineUpdate(SecretKey key) throws InvalidKeyException {
 608             if (digestSpi instanceof MessageDigestSpi2) {
 609                 ((MessageDigestSpi2)digestSpi).engineUpdate(key);
 610             } else {
 611                 throw new UnsupportedOperationException
 612                 ("Digest does not support update of SecretKey object");
 613             }
 614         }
 615         protected byte[] engineDigest() {
 616             return digestSpi.engineDigest();
 617         }
 618 
 619         protected int engineDigest(byte[] buf, int offset, int len)
 620             throws DigestException {
 621                 return digestSpi.engineDigest(buf, offset, len);
 622         }
 623 
 624         protected void engineReset() {
 625             digestSpi.engineReset();
 626         }
 627     }
 628 }
< prev index next >