src/share/classes/sun/security/provider/DigestBase.java

Print this page




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.provider;
  27 
  28 import java.security.MessageDigestSpi;
  29 import java.security.DigestException;
  30 import java.security.ProviderException;
  31 
  32 /**
  33  * Common base message digest implementation for the Sun provider.
  34  * It implements all the JCA methods as suitable for a Java message digest
  35  * implementation of an algorithm based on a compression function (as all
  36  * commonly used algorithms are). The individual digest subclasses only need to
  37  * implement the following methods:
  38  *
  39  *  . abstract void implCompress(byte[] b, int ofs);
  40  *  . abstract void implDigest(byte[] out, int ofs);
  41  *  . abstract void implReset();
  42  *  . public abstract Object clone();
  43  *
  44  * See the inline documentation for details.
  45  *
  46  * @since   1.5
  47  * @author  Andreas Sterbenz
  48  */
  49 abstract class DigestBase extends MessageDigestSpi implements Cloneable {
  50 
  51     // one element byte array, temporary storage for update(byte)
  52     private byte[] oneByte;
  53 
  54     // algorithm name to use in the exception message
  55     private final String algorithm;
  56     // length of the message digest in bytes
  57     private final int digestLength;
  58 
  59     // size of the input to the compression function in bytes
  60     private final int blockSize;
  61     // buffer to store partial blocks, blockSize bytes large
  62     // Subclasses should not access this array directly except possibly in their
  63     // implDigest() method. See MD5.java as an example.
  64     final byte[] buffer;
  65     // offset into buffer
  66     private int bufOfs;
  67 
  68     // number of bytes processed so far. subclasses should not modify
  69     // this value.
  70     // also used as a flag to indicate reset status
  71     // -1: need to call engineReset() before next call to update()
  72     //  0: is already reset
  73     long bytesProcessed;
  74 
  75     /**
  76      * Main constructor.
  77      */
  78     DigestBase(String algorithm, int digestLength, int blockSize) {
  79         super();
  80         this.algorithm = algorithm;
  81         this.digestLength = digestLength;
  82         this.blockSize = blockSize;
  83         buffer = new byte[blockSize];
  84     }
  85 
  86     /**
  87      * Constructor for cloning. Replicates common data.
  88      */
  89     DigestBase(DigestBase base) {
  90         this.algorithm = base.algorithm;
  91         this.digestLength = base.digestLength;
  92         this.blockSize = base.blockSize;
  93         this.buffer = base.buffer.clone();
  94         this.bufOfs = base.bufOfs;
  95         this.bytesProcessed = base.bytesProcessed;
  96     }
  97 
  98     // return digest length. See JCA doc.
  99     protected final int engineGetDigestLength() {
 100         return digestLength;
 101     }
 102 
 103     // single byte update. See JCA doc.
 104     protected final void engineUpdate(byte b) {
 105         if (oneByte == null) {
 106             oneByte = new byte[1];
 107         }
 108         oneByte[0] = b;
 109         engineUpdate(oneByte, 0, 1);
 110     }
 111 
 112     // array update. See JCA doc.
 113     protected final void engineUpdate(byte[] b, int ofs, int len) {
 114         if (len == 0) {
 115             return;
 116         }
 117         if ((ofs < 0) || (len < 0) || (ofs > b.length - len)) {


 189     }
 190 
 191     /**
 192      * Core compression function. Processes blockSize bytes at a time
 193      * and updates the state of this object.
 194      */
 195     abstract void implCompress(byte[] b, int ofs);
 196 
 197     /**
 198      * Return the digest. Subclasses do not need to reset() themselves,
 199      * DigestBase calls implReset() when necessary.
 200      */
 201     abstract void implDigest(byte[] out, int ofs);
 202 
 203     /**
 204      * Reset subclass specific state to their initial values. DigestBase
 205      * calls this method when necessary.
 206      */
 207     abstract void implReset();
 208 
 209     /**
 210      * Clone this digest. Should be implemented as "return new MyDigest(this)".
 211      * That constructor should first call "super(baseDigest)" and then copy
 212      * subclass specific data.
 213      */
 214     public abstract Object clone();
 215 
 216     // padding used for the MD5, and SHA-* message digests
 217     static final byte[] padding;
 218 
 219     static {
 220         // we need 128 byte padding for SHA-384/512
 221         // and an additional 8 bytes for the high 8 bytes of the 16
 222         // byte bit counter in SHA-384/512
 223         padding = new byte[136];
 224         padding[0] = (byte)0x80;
 225     }
 226 
 227 }


  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.provider;
  27 
  28 import java.security.MessageDigestSpi;
  29 import java.security.DigestException;
  30 import java.security.ProviderException;
  31 
  32 /**
  33  * Common base message digest implementation for the Sun provider.
  34  * It implements all the JCA methods as suitable for a Java message digest
  35  * implementation of an algorithm based on a compression function (as all
  36  * commonly used algorithms are). The individual digest subclasses only need to
  37  * implement the following methods:
  38  *
  39  *  . abstract void implCompress(byte[] b, int ofs);
  40  *  . abstract void implDigest(byte[] out, int ofs);
  41  *  . abstract void implReset();

  42  *
  43  * See the inline documentation for details.
  44  *
  45  * @since   1.5
  46  * @author  Andreas Sterbenz
  47  */
  48 abstract class DigestBase extends MessageDigestSpi implements Cloneable {
  49 
  50     // one element byte array, temporary storage for update(byte)
  51     private byte[] oneByte;
  52 
  53     // algorithm name to use in the exception message
  54     private final String algorithm;
  55     // length of the message digest in bytes
  56     private final int digestLength;
  57 
  58     // size of the input to the compression function in bytes
  59     private final int blockSize;
  60     // buffer to store partial blocks, blockSize bytes large
  61     // Subclasses should not access this array directly except possibly in their
  62     // implDigest() method. See MD5.java as an example.
  63     byte[] buffer;
  64     // offset into buffer
  65     private int bufOfs;
  66 
  67     // number of bytes processed so far. subclasses should not modify
  68     // this value.
  69     // also used as a flag to indicate reset status
  70     // -1: need to call engineReset() before next call to update()
  71     //  0: is already reset
  72     long bytesProcessed;
  73 
  74     /**
  75      * Main constructor.
  76      */
  77     DigestBase(String algorithm, int digestLength, int blockSize) {
  78         super();
  79         this.algorithm = algorithm;
  80         this.digestLength = digestLength;
  81         this.blockSize = blockSize;
  82         buffer = new byte[blockSize];
  83     }
  84 












  85     // return digest length. See JCA doc.
  86     protected final int engineGetDigestLength() {
  87         return digestLength;
  88     }
  89 
  90     // single byte update. See JCA doc.
  91     protected final void engineUpdate(byte b) {
  92         if (oneByte == null) {
  93             oneByte = new byte[1];
  94         }
  95         oneByte[0] = b;
  96         engineUpdate(oneByte, 0, 1);
  97     }
  98 
  99     // array update. See JCA doc.
 100     protected final void engineUpdate(byte[] b, int ofs, int len) {
 101         if (len == 0) {
 102             return;
 103         }
 104         if ((ofs < 0) || (len < 0) || (ofs > b.length - len)) {


 176     }
 177 
 178     /**
 179      * Core compression function. Processes blockSize bytes at a time
 180      * and updates the state of this object.
 181      */
 182     abstract void implCompress(byte[] b, int ofs);
 183 
 184     /**
 185      * Return the digest. Subclasses do not need to reset() themselves,
 186      * DigestBase calls implReset() when necessary.
 187      */
 188     abstract void implDigest(byte[] out, int ofs);
 189 
 190     /**
 191      * Reset subclass specific state to their initial values. DigestBase
 192      * calls this method when necessary.
 193      */
 194     abstract void implReset();
 195 
 196     public Object clone() throws CloneNotSupportedException {
 197         DigestBase copy = (DigestBase) super.clone();
 198         copy.buffer = copy.buffer.clone();
 199         return copy;
 200     }

 201 
 202     // padding used for the MD5, and SHA-* message digests
 203     static final byte[] padding;
 204 
 205     static {
 206         // we need 128 byte padding for SHA-384/512
 207         // and an additional 8 bytes for the high 8 bytes of the 16
 208         // byte bit counter in SHA-384/512
 209         padding = new byte[136];
 210         padding[0] = (byte)0x80;
 211     }

 212 }