001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Boris V. Kuznetsov
020: * @version $Revision$
021: */package java.security;
022:
023: import java.nio.ByteBuffer;
024:
025: import org.apache.harmony.security.internal.nls.Messages;
026:
027: /**
028: * This class is a Service Provider Interface (therefore the Spi suffix) for
029: * digest algorithms to be supplied by providers. Examples of digest algorithms
030: * are MD5 and SHA.
031: *
032: * A digest is a secure hash function for a stream of bytes, like a fingerprint
033: * for the stream of bytes.
034: *
035: */
036: public abstract class MessageDigestSpi {
037:
038: /**
039: * @com.intel.drl.spec_ref
040: *
041: */
042: protected int engineGetDigestLength() {
043: return 0;
044: }
045:
046: /**
047: * @com.intel.drl.spec_ref
048: *
049: */
050: protected abstract void engineUpdate(byte input);
051:
052: /**
053: * @com.intel.drl.spec_ref
054: *
055: */
056: protected abstract void engineUpdate(byte[] input, int offset,
057: int len);
058:
059: /**
060: * @com.intel.drl.spec_ref
061: *
062: */
063: protected void engineUpdate(ByteBuffer input) {
064: if (!input.hasRemaining()) {
065: return;
066: }
067: byte[] tmp;
068: if (input.hasArray()) {
069: tmp = input.array();
070: int offset = input.arrayOffset();
071: int position = input.position();
072: int limit = input.limit();
073: engineUpdate(tmp, offset + position, limit - position);
074: input.position(limit);
075: } else {
076: tmp = new byte[input.limit() - input.position()];
077: input.get(tmp);
078: engineUpdate(tmp, 0, tmp.length);
079: }
080: }
081:
082: /**
083: * @com.intel.drl.spec_ref
084: *
085: */
086: protected abstract byte[] engineDigest();
087:
088: /**
089: * @com.intel.drl.spec_ref
090: *
091: */
092: protected int engineDigest(byte[] buf, int offset, int len)
093: throws DigestException {
094: if (len < engineGetDigestLength()) {
095: engineReset();
096: throw new DigestException(Messages.getString("security.1B")); //$NON-NLS-1$
097: }
098: if (offset < 0) {
099: engineReset();
100: throw new DigestException(Messages.getString("security.1C")); //$NON-NLS-1$
101: }
102: if (offset + len > buf.length) {
103: engineReset();
104: throw new DigestException(Messages.getString("security.1D")); //$NON-NLS-1$
105: }
106: byte tmp[] = engineDigest();
107: if (len < tmp.length) {
108: throw new DigestException(Messages.getString("security.1B")); //$NON-NLS-1$
109: }
110: System.arraycopy(tmp, 0, buf, offset, tmp.length);
111: return tmp.length;
112: }
113:
114: /**
115: * @com.intel.drl.spec_ref
116: *
117: */
118: protected abstract void engineReset();
119:
120: /**
121: * @com.intel.drl.spec_ref
122: *
123: */
124: public Object clone() throws CloneNotSupportedException {
125: return super.clone();
126: }
127: }
|