Path: blob/master/src/java.base/share/classes/java/security/MessageDigestSpi.java
41152 views
/*1* Copyright (c) 1997, 2020, 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* @since 1.246*47*48* @see MessageDigest49*/5051public abstract class MessageDigestSpi {5253// for re-use in engineUpdate(ByteBuffer input)54private byte[] tempArray;5556/**57* Constructor for subclasses to call.58*/59public MessageDigestSpi() {}6061/**62* Returns the digest length in bytes.63*64* <p>This concrete method has been added to this previously-defined65* abstract class. (For backwards compatibility, it cannot be abstract.)66*67* <p>The default behavior is to return 0.68*69* <p>This method may be overridden by a provider to return the digest70* length.71*72* @return the digest length in bytes.73*74* @since 1.275*/76protected int engineGetDigestLength() {77return 0;78}7980/**81* Updates the digest using the specified byte.82*83* @param input the byte to use for the update.84*/85protected abstract void engineUpdate(byte input);8687/**88* Updates the digest using the specified array of bytes,89* starting at the specified offset.90*91* @param input the array of bytes to use for the update.92*93* @param offset the offset to start from in the array of bytes.94*95* @param len the number of bytes to use, starting at96* {@code offset}.97*/98protected abstract void engineUpdate(byte[] input, int offset, int len);99100/**101* Update the digest using the specified ByteBuffer. The digest is102* updated using the {@code input.remaining()} bytes starting103* at {@code input.position()}.104* Upon return, the buffer's position will be equal to its limit;105* its limit will not have changed.106*107* @param input the ByteBuffer108* @since 1.5109*/110protected void engineUpdate(ByteBuffer input) {111if (input.hasRemaining() == false) {112return;113}114if (input.hasArray()) {115byte[] b = input.array();116int ofs = input.arrayOffset();117int pos = input.position();118int lim = input.limit();119engineUpdate(b, ofs + pos, lim - pos);120input.position(lim);121} else {122int len = input.remaining();123int n = JCAUtil.getTempArraySize(len);124if ((tempArray == null) || (n > tempArray.length)) {125tempArray = new byte[n];126}127while (len > 0) {128int chunk = Math.min(len, tempArray.length);129input.get(tempArray, 0, chunk);130engineUpdate(tempArray, 0, chunk);131len -= chunk;132}133}134}135136/**137* Completes the hash computation by performing final138* operations such as padding. Once {@code engineDigest} has139* been called, the engine should be reset (see140* {@link #engineReset() engineReset}).141* Resetting is the responsibility of the142* engine implementor.143*144* @return the array of bytes for the resulting hash value.145*/146protected abstract byte[] engineDigest();147148/**149* Completes the hash computation by performing final150* operations such as padding. Once {@code engineDigest} has151* been called, the engine should be reset (see152* {@link #engineReset() engineReset}).153* Resetting is the responsibility of the154* engine implementor.155*156* This method should be abstract, but we leave it concrete for157* binary compatibility. Knowledgeable providers should override this158* method.159*160* @param buf the output buffer in which to store the digest161*162* @param offset offset to start from in the output buffer163*164* @param len number of bytes within buf allotted for the digest.165* Both this default implementation and the SUN provider do not166* return partial digests. The presence of this parameter is solely167* for consistency in our API's. If the value of this parameter is less168* than the actual digest length, the method will throw a DigestException.169* This parameter is ignored if its value is greater than or equal to170* the actual digest length.171*172* @return the length of the digest stored in the output buffer.173*174* @throws DigestException if an error occurs.175*176* @since 1.2177*/178protected int engineDigest(byte[] buf, int offset, int len)179throws DigestException {180181byte[] digest = engineDigest();182if (len < digest.length)183throw new DigestException("partial digests not returned");184if (buf.length - offset < digest.length)185throw new DigestException("insufficient space in the output "186+ "buffer to store the digest");187System.arraycopy(digest, 0, buf, offset, digest.length);188return digest.length;189}190191/**192* Resets the digest for further use.193*/194protected abstract void engineReset();195196/**197* Returns a clone if the implementation is cloneable.198*199* @return a clone if the implementation is cloneable.200*201* @throws CloneNotSupportedException if this is called on an202* implementation that does not support {@code Cloneable}.203*/204public Object clone() throws CloneNotSupportedException {205if (this instanceof Cloneable) {206return super.clone();207} else {208throw new CloneNotSupportedException();209}210}211}212213214