1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javax.crypto;
import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
/**
* The <i>Service-Provider Interface</i> (<b>SPI</b>) definition for the {@code
* Mac} class.
*
* @see Mac
*/
public abstract class MacSpi {
/**
* Creates a new {@code MacSpi} instance.
*/
public MacSpi() {
}
/**
* Returns the length of this MAC (in bytes).
*
* @return the length of this MAC (in bytes).
*/
protected abstract int engineGetMacLength();
/**
* Initializes this {@code MacSpi} instance with the specified key and
* algorithm parameters.
*
* @param key
* the key to initialize this algorithm.
* @param params
* the parameters for this algorithm.
* @throws InvalidKeyException
* if the specified key cannot be used to initialize this
* algorithm, or it is {@code null}.
* @throws InvalidAlgorithmParameterException
* if the specified parameters cannot be used to initialize this
* algorithm.
*/
protected abstract void engineInit(Key key, AlgorithmParameterSpec params)
throws InvalidKeyException, InvalidAlgorithmParameterException;
/**
* Updates this {@code MacSpi} instance with the specified byte.
*
* @param input
* the byte.
*/
protected abstract void engineUpdate(byte input);
/**
* Updates this {@code MacSpi} instance with the data from the specified
* buffer {@code input} from the specified {@code offset} and length {@code
* len}.
*
* @param input
* the buffer.
* @param offset
* the offset in the buffer.
* @param len
* the length of the data in the buffer.
*/
protected abstract void engineUpdate(byte[] input, int offset, int len);
/**
* Updates this {@code MacSpi} instance with the data from the specified
* buffer, starting at {@link ByteBuffer#position()}, including the next
* {@link ByteBuffer#remaining()} bytes.
*
* @param input
* the buffer.
*/
protected void engineUpdate(ByteBuffer input) {
if (!input.hasRemaining()) {
return;
}
byte[] bInput;
if (input.hasArray()) {
bInput = input.array();
int offset = input.arrayOffset();
int position = input.position();
int limit = input.limit();
engineUpdate(bInput, offset + position, limit - position);
input.position(limit);
} else {
bInput = new byte[input.limit() - input.position()];
input.get(bInput);
engineUpdate(bInput, 0, bInput.length);
}
}
/**
* Computes the digest of this MAC based on the data previously specified in
* {@link #engineUpdate} calls.
* <p>
* This {@code MacSpi} instance is reverted to its initial state and
* can be used to start the next MAC computation with the same parameters or
* initialized with different parameters.
*
* @return the generated digest.
*/
protected abstract byte[] engineDoFinal();
/**
* Resets this {@code MacSpi} instance to its initial state.
* <p>
* This {@code MacSpi} instance is reverted to its initial state and can be
* used to start the next MAC computation with the same parameters or
* initialized with different parameters.
*/
protected abstract void engineReset();
/**
* Clones this {@code MacSpi} instance.
*
* @return the cloned instance.
* @throws CloneNotSupportedException
* if cloning is not supported.
*/
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
|