home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / security / MessageDigestSpi.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.2 KB  |  161 lines

  1. /*
  2.  * @(#)MessageDigestSpi.java    1.3 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. import java.util.*;
  18. import java.lang.*;
  19. import java.io.IOException;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.PrintStream;
  22. import java.io.InputStream;
  23. import java.io.ByteArrayInputStream;
  24.  
  25. /**
  26.  * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
  27.  * for the <code>MessageDigest</code> class, which provides the functionality
  28.  * of a message digest algorithm, such as MD5 or SHA. Message digests are
  29.  * secure one-way hash functions that take arbitrary-sized data and output a
  30.  * fixed-length hash value.
  31.  *
  32.  * <p> All the abstract methods in this class must be implemented by a
  33.  * cryptographic service provider who wishes to supply the implementation
  34.  * of a particular message digest algorithm.
  35.  *
  36.  * <p> Implementations are free to implement the Cloneable interface.
  37.  *
  38.  * @author Benjamin Renaud 
  39.  *
  40.  * @version 1.3 98/03/18
  41.  *
  42.  * @see MessageDigest
  43.  */
  44.  
  45. public abstract class MessageDigestSpi {
  46.  
  47.     /**
  48.      * Returns the digest length in bytes.
  49.      *
  50.      * <p>This concrete method has been added to this previously-defined
  51.      * abstract class. (For backwards compatibility, it cannot be abstract.)
  52.      * 
  53.      * <p>The default behavior is to return 0.
  54.      * 
  55.      * <p>This method may be overridden by a provider to return the digest
  56.      * length.
  57.      *
  58.      * @return the digest length in bytes.
  59.      *
  60.      * @since JDK1.2
  61.      */
  62.     protected int engineGetDigestLength() {
  63.     return 0;
  64.     }
  65.  
  66.     /**
  67.      * Updates the digest using the specified byte.
  68.      *
  69.      * @param input the byte to use for the update.
  70.      */
  71.     protected abstract void engineUpdate(byte input);
  72.  
  73.     /**
  74.      * Updates the digest using the specified array of bytes,    
  75.      * starting at the specified offset. This should be a no-op if
  76.      * the digest has been finalized.
  77.      *
  78.      * @param input the array of bytes to use for the update.
  79.      *
  80.      * @param offset the offset to start from in the array of bytes.
  81.      *
  82.      * @param len the number of bytes to use, starting at 
  83.      * <code>offset</code>.
  84.      */
  85.     protected abstract void engineUpdate(byte[] input, int offset, int len);
  86.  
  87.     /**
  88.      * Completes the hash computation by performing final
  89.      * operations such as padding. Once <code>engineDigest</code> has 
  90.      * been called, the engine should be reset (see <a href =
  91.      * "#engineReset">engineReset</a>).  Resetting is the responsibility of the
  92.      * engine implementor.
  93.      *
  94.      * @return the array of bytes for the resulting hash value.  
  95.      */
  96.     protected abstract byte[] engineDigest();
  97.  
  98.     /**
  99.      * Completes the hash computation by performing final
  100.      * operations such as padding. Once <code>engineDigest</code> has
  101.      * been called, the engine should be reset (see <a href =
  102.      * "#engineReset">engineReset</a>).  Resetting is the responsibility of the
  103.      * engine implementor.
  104.      *
  105.      * This method should be abstract, but we leave it concrete for
  106.      * binary compatibility.  Knowledgeable providers should override this
  107.      * method.
  108.      *
  109.      * @param buf the output buffer in which to store the digest
  110.      *
  111.      * @param offset offset to start from in the output buffer
  112.      *
  113.      * @param len number of bytes within buf allotted for the digest.
  114.      * Both this default implementation and the SUN provider do not
  115.      * return partial digests.  The presence of this parameter is solely
  116.      * for consistency in our API's.  If the value of this parameter is less
  117.      * than the actual digest length, the method will throw a DigestException.
  118.      * This parameter is ignored if its value is greater than or equal to
  119.      * the actual digest length.
  120.      *
  121.      * @return the length of the digest stored in the output buffer.
  122.      * 
  123.      * @exception DigestException if an error occurs.
  124.      *
  125.      * @since JDK1.2
  126.      */
  127.     protected int engineDigest(byte[] buf, int offset, int len)
  128.                         throws DigestException {
  129.  
  130.     byte[] digest = engineDigest();
  131.     if (len < digest.length)
  132.         throw new DigestException("partial digests not returned");
  133.     if (buf.length - offset < digest.length)
  134.         throw new DigestException("insufficient space in the output "
  135.                       + "buffer to store the digest");
  136.     System.arraycopy(digest, 0, buf, offset, digest.length);
  137.     return digest.length;
  138.     }
  139.  
  140.     /**
  141.      * Resets the digest for further use.
  142.      */
  143.     protected abstract void engineReset();    
  144.  
  145.     /**    
  146.      * Returns a clone if the implementation is cloneable.    
  147.      * 
  148.      * @return a clone if the implementation is cloneable.
  149.      *
  150.      * @exception CloneNotSupportedException if this is called on an
  151.      * implementation that does not support <code>Cloneable</code>.
  152.      */
  153.     public Object clone() throws CloneNotSupportedException {
  154.     if (this instanceof Cloneable) {
  155.         return super.clone();
  156.     } else {
  157.         throw new CloneNotSupportedException();
  158.     }
  159.     }
  160. }
  161.