Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/javax/crypto/MacSpi.java
41152 views
1
/*
2
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.crypto;
27
28
import java.security.*;
29
import java.security.spec.*;
30
31
import java.nio.ByteBuffer;
32
33
/**
34
* This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
35
* for the <code>Mac</code> class.
36
* All the abstract methods in this class must be implemented by each
37
* cryptographic service provider who wishes to supply the implementation
38
* of a particular MAC algorithm.
39
*
40
* <p> Implementations are free to implement the Cloneable interface.
41
*
42
* @author Jan Luehe
43
*
44
* @since 1.4
45
*/
46
47
public abstract class MacSpi {
48
49
/**
50
* Constructor for subclasses to call.
51
*/
52
public MacSpi() {}
53
54
/**
55
* Returns the length of the MAC in bytes.
56
*
57
* @return the MAC length in bytes.
58
*/
59
protected abstract int engineGetMacLength();
60
61
/**
62
* Initializes the MAC with the given (secret) key and algorithm
63
* parameters.
64
*
65
* @param key the (secret) key.
66
* @param params the algorithm parameters.
67
*
68
* @exception InvalidKeyException if the given key is inappropriate for
69
* initializing this MAC.
70
* @exception InvalidAlgorithmParameterException if the given algorithm
71
* parameters are inappropriate for this MAC.
72
*/
73
protected abstract void engineInit(Key key,
74
AlgorithmParameterSpec params)
75
throws InvalidKeyException, InvalidAlgorithmParameterException ;
76
77
/**
78
* Processes the given byte.
79
*
80
* @param input the input byte to be processed.
81
*/
82
protected abstract void engineUpdate(byte input);
83
84
/**
85
* Processes the first <code>len</code> bytes in <code>input</code>,
86
* starting at <code>offset</code> inclusive.
87
*
88
* @param input the input buffer.
89
* @param offset the offset in <code>input</code> where the input starts.
90
* @param len the number of bytes to process.
91
*/
92
protected abstract void engineUpdate(byte[] input, int offset, int len);
93
94
/**
95
* Processes <code>input.remaining()</code> bytes in the ByteBuffer
96
* <code>input</code>, starting at <code>input.position()</code>.
97
* Upon return, the buffer's position will be equal to its limit;
98
* its limit will not have changed.
99
*
100
* <p>Subclasses should consider overriding this method if they can
101
* process ByteBuffers more efficiently than byte arrays.
102
*
103
* @param input the ByteBuffer
104
* @since 1.5
105
*/
106
protected void engineUpdate(ByteBuffer input) {
107
if (input.hasRemaining() == false) {
108
return;
109
}
110
if (input.hasArray()) {
111
byte[] b = input.array();
112
int ofs = input.arrayOffset();
113
int pos = input.position();
114
int lim = input.limit();
115
engineUpdate(b, ofs + pos, lim - pos);
116
input.position(lim);
117
} else {
118
int len = input.remaining();
119
byte[] b = new byte[CipherSpi.getTempArraySize(len)];
120
while (len > 0) {
121
int chunk = Math.min(len, b.length);
122
input.get(b, 0, chunk);
123
engineUpdate(b, 0, chunk);
124
len -= chunk;
125
}
126
}
127
}
128
129
/**
130
* Completes the MAC computation and resets the MAC for further use,
131
* maintaining the secret key that the MAC was initialized with.
132
*
133
* @return the MAC result.
134
*/
135
protected abstract byte[] engineDoFinal();
136
137
/**
138
* Resets the MAC for further use, maintaining the secret key that the
139
* MAC was initialized with.
140
*/
141
protected abstract void engineReset();
142
143
/**
144
* Returns a clone if the implementation is cloneable.
145
*
146
* @return a clone if the implementation is cloneable.
147
*
148
* @exception CloneNotSupportedException if this is called
149
* on an implementation that does not support <code>Cloneable</code>.
150
*/
151
public Object clone() throws CloneNotSupportedException {
152
if (this instanceof Cloneable) {
153
return super.clone();
154
} else {
155
throw new CloneNotSupportedException();
156
}
157
}
158
}
159
160