Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/security/MessageDigestSpi.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.security;2627import java.nio.ByteBuffer;2829import sun.security.jca.JCAUtil;3031/**32* This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)33* for the {@code MessageDigest} class, which provides the functionality34* of a message digest algorithm, such as MD5 or SHA. Message digests are35* secure one-way hash functions that take arbitrary-sized data and output a36* fixed-length hash value.37*38* <p> All the abstract methods in this class must be implemented by a39* cryptographic service provider who wishes to supply the implementation40* of a particular message digest algorithm.41*42* <p> Implementations are free to implement the Cloneable interface.43*44* @author Benjamin Renaud45*46*47* @see MessageDigest48*/4950public abstract class MessageDigestSpi {5152// for re-use in engineUpdate(ByteBuffer input)53private byte[] tempArray;5455/**56* Returns the digest length in bytes.57*58* <p>This concrete method has been added to this previously-defined59* abstract class. (For backwards compatibility, it cannot be abstract.)60*61* <p>The default behavior is to return 0.62*63* <p>This method may be overridden by a provider to return the digest64* length.65*66* @return the digest length in bytes.67*68* @since 1.269*/70protected int engineGetDigestLength() {71return 0;72}7374/**75* Updates the digest using the specified byte.76*77* @param input the byte to use for the update.78*/79protected abstract void engineUpdate(byte input);8081/**82* Updates the digest using the specified array of bytes,83* starting at the specified offset.84*85* @param input the array of bytes to use for the update.86*87* @param offset the offset to start from in the array of bytes.88*89* @param len the number of bytes to use, starting at90* {@code offset}.91*/92protected abstract void engineUpdate(byte[] input, int offset, int len);9394/**95* Update the digest using the specified ByteBuffer. The digest is96* updated using the {@code input.remaining()} bytes starting97* at {@code input.position()}.98* Upon return, the buffer's position will be equal to its limit;99* its limit will not have changed.100*101* @param input the ByteBuffer102* @since 1.5103*/104protected void engineUpdate(ByteBuffer input) {105if (input.hasRemaining() == false) {106return;107}108if (input.hasArray()) {109byte[] b = input.array();110int ofs = input.arrayOffset();111int pos = input.position();112int lim = input.limit();113engineUpdate(b, ofs + pos, lim - pos);114input.position(lim);115} else {116int len = input.remaining();117int n = JCAUtil.getTempArraySize(len);118if ((tempArray == null) || (n > tempArray.length)) {119tempArray = new byte[n];120}121while (len > 0) {122int chunk = Math.min(len, tempArray.length);123input.get(tempArray, 0, chunk);124engineUpdate(tempArray, 0, chunk);125len -= chunk;126}127}128}129130/**131* Completes the hash computation by performing final132* operations such as padding. Once {@code engineDigest} has133* been called, the engine should be reset (see134* {@link #engineReset() engineReset}).135* Resetting is the responsibility of the136* engine implementor.137*138* @return the array of bytes for the resulting hash value.139*/140protected abstract byte[] engineDigest();141142/**143* Completes the hash computation by performing final144* operations such as padding. Once {@code engineDigest} has145* been called, the engine should be reset (see146* {@link #engineReset() engineReset}).147* Resetting is the responsibility of the148* engine implementor.149*150* This method should be abstract, but we leave it concrete for151* binary compatibility. Knowledgeable providers should override this152* method.153*154* @param buf the output buffer in which to store the digest155*156* @param offset offset to start from in the output buffer157*158* @param len number of bytes within buf allotted for the digest.159* Both this default implementation and the SUN provider do not160* return partial digests. The presence of this parameter is solely161* for consistency in our API's. If the value of this parameter is less162* than the actual digest length, the method will throw a DigestException.163* This parameter is ignored if its value is greater than or equal to164* the actual digest length.165*166* @return the length of the digest stored in the output buffer.167*168* @exception DigestException if an error occurs.169*170* @since 1.2171*/172protected int engineDigest(byte[] buf, int offset, int len)173throws DigestException {174175byte[] digest = engineDigest();176if (len < digest.length)177throw new DigestException("partial digests not returned");178if (buf.length - offset < digest.length)179throw new DigestException("insufficient space in the output "180+ "buffer to store the digest");181System.arraycopy(digest, 0, buf, offset, digest.length);182return digest.length;183}184185/**186* Resets the digest for further use.187*/188protected abstract void engineReset();189190/**191* Returns a clone if the implementation is cloneable.192*193* @return a clone if the implementation is cloneable.194*195* @exception CloneNotSupportedException if this is called on an196* implementation that does not support {@code Cloneable}.197*/198public Object clone() throws CloneNotSupportedException {199if (this instanceof Cloneable) {200return super.clone();201} else {202throw new CloneNotSupportedException();203}204}205}206207208