jdk/src/share/classes/java/security/MessageDigestSpi.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 18579 b678846778ad
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.jca.JCAUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * for the <code>MessageDigest</code> class, which provides the functionality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * of a message digest algorithm, such as MD5 or SHA. Message digests are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * secure one-way hash functions that take arbitrary-sized data and output a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * fixed-length hash value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p> All the abstract methods in this class must be implemented by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * cryptographic service provider who wishes to supply the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of a particular message digest algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p> Implementations are free to implement the Cloneable interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public abstract class MessageDigestSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // for re-use in engineUpdate(ByteBuffer input)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private byte[] tempArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Returns the digest length in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * <p>This concrete method has been added to this previously-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * abstract class. (For backwards compatibility, it cannot be abstract.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * <p>The default behavior is to return 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <p>This method may be overridden by a provider to return the digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @return the digest length in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected int engineGetDigestLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Updates the digest using the specified byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param input the byte to use for the update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected abstract void engineUpdate(byte input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Updates the digest using the specified array of bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * starting at the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param input the array of bytes to use for the update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param offset the offset to start from in the array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param len the number of bytes to use, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * <code>offset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected abstract void engineUpdate(byte[] input, int offset, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Update the digest using the specified ByteBuffer. The digest is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * updated using the <code>input.remaining()</code> bytes starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * at <code>input.position()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Upon return, the buffer's position will be equal to its limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * its limit will not have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param input the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected void engineUpdate(ByteBuffer input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (input.hasRemaining() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (input.hasArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            byte[] b = input.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            int ofs = input.arrayOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            int pos = input.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            int lim = input.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            engineUpdate(b, ofs + pos, lim - pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            input.position(lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            int len = input.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            int n = JCAUtil.getTempArraySize(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if ((tempArray == null) || (n > tempArray.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                tempArray = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                int chunk = Math.min(len, tempArray.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                input.get(tempArray, 0, chunk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                engineUpdate(tempArray, 0, chunk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                len -= chunk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Completes the hash computation by performing final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * operations such as padding. Once <code>engineDigest</code> has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * been called, the engine should be reset (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * {@link #engineReset() engineReset}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Resetting is the responsibility of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * engine implementor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @return the array of bytes for the resulting hash value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    protected abstract byte[] engineDigest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Completes the hash computation by performing final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * operations such as padding. Once <code>engineDigest</code> has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * been called, the engine should be reset (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * {@link #engineReset() engineReset}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Resetting is the responsibility of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * engine implementor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * This method should be abstract, but we leave it concrete for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * binary compatibility.  Knowledgeable providers should override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param buf the output buffer in which to store the digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param offset offset to start from in the output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param len number of bytes within buf allotted for the digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Both this default implementation and the SUN provider do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * return partial digests.  The presence of this parameter is solely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * for consistency in our API's.  If the value of this parameter is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * than the actual digest length, the method will throw a DigestException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * This parameter is ignored if its value is greater than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * the actual digest length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return the length of the digest stored in the output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @exception DigestException if an error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected int engineDigest(byte[] buf, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                                throws DigestException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        byte[] digest = engineDigest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (len < digest.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                throw new DigestException("partial digests not returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (buf.length - offset < digest.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                throw new DigestException("insufficient space in the output "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                          + "buffer to store the digest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        System.arraycopy(digest, 0, buf, offset, digest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return digest.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Resets the digest for further use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    protected abstract void engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Returns a clone if the implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return a clone if the implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @exception CloneNotSupportedException if this is called on an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * implementation that does not support <code>Cloneable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (this instanceof Cloneable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new CloneNotSupportedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}