0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.security;
0N/A
0N/Aimport java.nio.ByteBuffer;
0N/A
0N/Aimport sun.security.jca.JCAUtil;
0N/A
0N/A/**
0N/A * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
0N/A * for the <code>MessageDigest</code> class, which provides the functionality
0N/A * of a message digest algorithm, such as MD5 or SHA. Message digests are
0N/A * secure one-way hash functions that take arbitrary-sized data and output a
0N/A * fixed-length hash value.
0N/A *
0N/A * <p> All the abstract methods in this class must be implemented by a
0N/A * cryptographic service provider who wishes to supply the implementation
0N/A * of a particular message digest algorithm.
0N/A *
0N/A * <p> Implementations are free to implement the Cloneable interface.
0N/A *
0N/A * @author Benjamin Renaud
0N/A *
0N/A *
0N/A * @see MessageDigest
0N/A */
0N/A
0N/Apublic abstract class MessageDigestSpi {
0N/A
0N/A // for re-use in engineUpdate(ByteBuffer input)
0N/A private byte[] tempArray;
0N/A
0N/A /**
0N/A * Returns the digest length in bytes.
0N/A *
0N/A * <p>This concrete method has been added to this previously-defined
0N/A * abstract class. (For backwards compatibility, it cannot be abstract.)
0N/A *
0N/A * <p>The default behavior is to return 0.
0N/A *
0N/A * <p>This method may be overridden by a provider to return the digest
0N/A * length.
0N/A *
0N/A * @return the digest length in bytes.
0N/A *
0N/A * @since 1.2
0N/A */
0N/A protected int engineGetDigestLength() {
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Updates the digest using the specified byte.
0N/A *
0N/A * @param input the byte to use for the update.
0N/A */
0N/A protected abstract void engineUpdate(byte input);
0N/A
0N/A /**
0N/A * Updates the digest using the specified array of bytes,
0N/A * starting at the specified offset.
0N/A *
0N/A * @param input the array of bytes to use for the update.
0N/A *
0N/A * @param offset the offset to start from in the array of bytes.
0N/A *
0N/A * @param len the number of bytes to use, starting at
0N/A * <code>offset</code>.
0N/A */
0N/A protected abstract void engineUpdate(byte[] input, int offset, int len);
0N/A
0N/A /**
0N/A * Update the digest using the specified ByteBuffer. The digest is
0N/A * updated using the <code>input.remaining()</code> bytes starting
0N/A * at <code>input.position()</code>.
0N/A * Upon return, the buffer's position will be equal to its limit;
0N/A * its limit will not have changed.
0N/A *
0N/A * @param input the ByteBuffer
0N/A * @since 1.5
0N/A */
0N/A protected void engineUpdate(ByteBuffer input) {
0N/A if (input.hasRemaining() == false) {
0N/A return;
0N/A }
0N/A if (input.hasArray()) {
0N/A byte[] b = input.array();
0N/A int ofs = input.arrayOffset();
0N/A int pos = input.position();
0N/A int lim = input.limit();
0N/A engineUpdate(b, ofs + pos, lim - pos);
0N/A input.position(lim);
0N/A } else {
0N/A int len = input.remaining();
0N/A int n = JCAUtil.getTempArraySize(len);
0N/A if ((tempArray == null) || (n > tempArray.length)) {
0N/A tempArray = new byte[n];
0N/A }
0N/A while (len > 0) {
0N/A int chunk = Math.min(len, tempArray.length);
0N/A input.get(tempArray, 0, chunk);
0N/A engineUpdate(tempArray, 0, chunk);
0N/A len -= chunk;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Completes the hash computation by performing final
0N/A * operations such as padding. Once <code>engineDigest</code> has
0N/A * been called, the engine should be reset (see
0N/A * {@link #engineReset() engineReset}).
0N/A * Resetting is the responsibility of the
0N/A * engine implementor.
0N/A *
0N/A * @return the array of bytes for the resulting hash value.
0N/A */
0N/A protected abstract byte[] engineDigest();
0N/A
0N/A /**
0N/A * Completes the hash computation by performing final
0N/A * operations such as padding. Once <code>engineDigest</code> has
0N/A * been called, the engine should be reset (see
0N/A * {@link #engineReset() engineReset}).
0N/A * Resetting is the responsibility of the
0N/A * engine implementor.
0N/A *
0N/A * This method should be abstract, but we leave it concrete for
0N/A * binary compatibility. Knowledgeable providers should override this
0N/A * method.
0N/A *
0N/A * @param buf the output buffer in which to store the digest
0N/A *
0N/A * @param offset offset to start from in the output buffer
0N/A *
0N/A * @param len number of bytes within buf allotted for the digest.
0N/A * Both this default implementation and the SUN provider do not
0N/A * return partial digests. The presence of this parameter is solely
0N/A * for consistency in our API's. If the value of this parameter is less
0N/A * than the actual digest length, the method will throw a DigestException.
0N/A * This parameter is ignored if its value is greater than or equal to
0N/A * the actual digest length.
0N/A *
0N/A * @return the length of the digest stored in the output buffer.
0N/A *
0N/A * @exception DigestException if an error occurs.
0N/A *
0N/A * @since 1.2
0N/A */
0N/A protected int engineDigest(byte[] buf, int offset, int len)
0N/A throws DigestException {
0N/A
0N/A byte[] digest = engineDigest();
0N/A if (len < digest.length)
0N/A throw new DigestException("partial digests not returned");
0N/A if (buf.length - offset < digest.length)
0N/A throw new DigestException("insufficient space in the output "
0N/A + "buffer to store the digest");
0N/A System.arraycopy(digest, 0, buf, offset, digest.length);
0N/A return digest.length;
0N/A }
0N/A
0N/A /**
0N/A * Resets the digest for further use.
0N/A */
0N/A protected abstract void engineReset();
0N/A
0N/A /**
0N/A * Returns a clone if the implementation is cloneable.
0N/A *
0N/A * @return a clone if the implementation is cloneable.
0N/A *
0N/A * @exception CloneNotSupportedException if this is called on an
0N/A * implementation that does not support <code>Cloneable</code>.
0N/A */
0N/A public Object clone() throws CloneNotSupportedException {
0N/A if (this instanceof Cloneable) {
0N/A return super.clone();
0N/A } else {
0N/A throw new CloneNotSupportedException();
0N/A }
0N/A }
0N/A}