CipherSpipublic abstract class CipherSpi extends Object This class defines the Service Provider Interface (SPI) for
cryptographic ciphers.
Implementers of cryptographic ciphers must implement all the abstract methods
for every cipher they implement. {@code CipherSpi} instances are created
along with ciphers when the {@link Cipher#getInstance} method is called. A
{@code Cipher} is referenced by a transformation, which is a string
that describes the operation (or set of operations), always consisting of the
cipher's name and optionally followed by a mode and a padding, in the form:
- "algorithm"
or
- "algorithm/mode/padding"
The following behavior should be implemented for obtaining {@code Cipher}
instances.
When one of the {@link Cipher#getInstance} factory methods is called with a
transformation that is only an algorithm, check if the provider
defines a {@code CipherSpi} for "algorithm", if so: return it, otherwise
throw a {@link NoSuchAlgorithmException}.
The following rules apply when a transformation is of the form
"algorithm/mode/padding":
1. The Provider has a {@code CipherSpi} subclass registered for
"algorithm/mode/padding": return it, otherwise go to step 2.
2. The Provider has a {@code CipherSpi} subclass registered for
"algorithm/mode": instantiate it, call
{@link CipherSpi#engineSetPadding(String) engineSetPadding(String)} for the
padding name and return it, otherwise go to step 3.
3. The Provider has a {@code CipherSpi} subclass registered for
"algorithm//padding": instantiate it, call
{@link CipherSpi#engineSetMode(String) engineSetMode(String)} for the mode
name and return it, otherwise go to step 4.
4. The Provider has a {@code CipherSpi} subclass registered for "algorithm":
instantiate it, call {@link CipherSpi#engineSetMode(String)
engineSetMode(String)} for the mode name , call
{@link CipherSpi#engineSetPadding(String) engineSetPadding(String)} for the
padding name and return it, otherwise throw a
{@link NoSuchAlgorithmException}.
|
Constructors Summary |
---|
public CipherSpi()Creates a new {@code CipherSpi} instance.
|
Methods Summary |
---|
protected abstract byte[] | engineDoFinal(byte[] input, int inputOffset, int inputLen)Finishes a multi-part transformation (encryption or decryption).
Processes the {@code inputLen} bytes in {@code input} buffer at {@code
inputOffset}, and any bytes that have been buffered in previous {@code
update} calls.
| protected abstract int | engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)Finishes a multi-part transformation (encryption or decryption).
Processes the {@code inputLen} bytes in {@code input} buffer at
{@code inputOffset}, and any bytes that have been buffered in previous
{@code update} calls.
| protected int | engineDoFinal(java.nio.ByteBuffer input, java.nio.ByteBuffer output)Finishes a multi-part transformation (encryption or decryption).
Processes the {@code input.remaining()} bytes in {@code input} buffer at
{@code input.position()}, and any bytes that have been buffered in
previous {@code update} calls. The transformed bytes are placed into
{@code output} buffer.
if (input == null) {
throw new NullPointerException(Messages.getString("crypto.0C")); //$NON-NLS-1$
}
if (output == null) {
throw new NullPointerException(Messages.getString("crypto.0D")); //$NON-NLS-1$
}
int position = input.position();
int limit = input.limit();
if ((limit - position) <= 0) {
return 0;
}
byte[] bInput;
byte[] bOutput;
if (input.hasArray()) {
bInput = input.array();
int offset = input.arrayOffset();
bOutput = engineDoFinal(bInput, offset + position, limit - position);
input.position(limit);
} else {
bInput = new byte[limit - position];
input.get(bInput);
bOutput = engineDoFinal(bInput, 0, limit - position);
}
if (output.remaining() < bOutput.length) {
throw new ShortBufferException(Messages.getString("crypto.0E")); //$NON-NLS-1$
}
try {
output.put(bOutput);
} catch (java.nio.BufferOverflowException e) {
throw new ShortBufferException(Messages.getString("crypto.0F", e)); //$NON-NLS-1$
}
return bOutput.length;
| protected abstract int | engineGetBlockSize()Returns the block size of this cipher (in bytes)
| protected abstract byte[] | engineGetIV()Returns the Initialization Vector (IV) that was used to initialize this
cipher or {@code null} if none was used.
| protected int | engineGetKeySize(java.security.Key key)Returns the size of a specified key object in bits. This method has been
added to this class (for backwards compatibility, it cannot be abstract).
If this method is not overridden, it throws an {@code
UnsupportedOperationException}.
throw new UnsupportedOperationException(
Messages.getString("crypto.12")); //$NON-NLS-1$
| protected abstract int | engineGetOutputSize(int inputLen)Returns the size for a buffer (in bytes), that the next call to {@code
update} of {@code doFinal} would return, taking into account any buffered
data from previous {@code update} calls and padding.
The actual output length of the next call to {@code update} or {@code
doFinal} may be smaller than the length returned by this method.
| protected abstract java.security.AlgorithmParameters | engineGetParameters()Returns the parameters that where used to create this cipher instance.
These may be a the same parameters that were used to create this cipher
instance, or may be a combination of default and random parameters,
depending on the underlying cipher implementation.
| protected abstract void | engineInit(int opmode, java.security.Key key, java.security.AlgorithmParameters params, java.security.SecureRandom random)Initializes this cipher instance with the specified key, algorithm
parameters and a source of randomness.
The cipher will be initialized for the specified operation (one of:
encryption, decryption, key wrapping or key unwrapping) depending on
{@code opmode}.
If this cipher instance needs any algorithm parameters and {@code params}
is {@code null}, the underlying implementation of this cipher is supposed
to generate the required parameters (using its provider or random
values). Random values are generated using {@code random}.
When a cipher instance is initialized by a call to any of the {@code
init} methods, the state of the instance is overridden, means it is
equivalent to creating a new instance and calling it {@code init} method.
| protected abstract void | engineInit(int opmode, java.security.Key key, java.security.SecureRandom random)Initializes this cipher instance with the specified key and a source of
randomness.
The cipher will be initialized for the specified operation (one of:
encryption, decryption, key wrapping or key unwrapping) depending on
{@code opmode}.
If this cipher instance needs any algorithm parameters or random values
that the specified key cannot provide, the underlying implementation of
this cipher is supposed to generate the required parameters (using its
provider or random values). Random values will be generated using {@code
random};
When a cipher instance is initialized by a call to any of the {@code
init} methods, the state of the instance is overridden, means it is
equivalent to creating a new instance and calling it {@code init} method.
| protected abstract void | engineInit(int opmode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom random)Initializes this cipher instance with the specified key, algorithm
parameters and a source of randomness.
The cipher will be initialized for the specified operation (one of:
encryption, decryption, key wrapping or key unwrapping) depending on
{@code opmode}.
If this cipher instance needs any algorithm parameters and {@code params}
is {@code null}, the underlying implementation of this cipher is supposed
to generate the required parameters (using its provider or random
values). Random values are generated using {@code random}.
When a cipher instance is initialized by a call to any of the {@code
init} methods, the state of the instance is overridden, means it is
equivalent to creating a new instance and calling it {@code init} method.
| protected abstract void | engineSetMode(java.lang.String mode)Sets the mode for this cipher.
| protected abstract void | engineSetPadding(java.lang.String padding)Sets the padding method for this cipher.
| protected java.security.Key | engineUnwrap(byte[] wrappedKey, java.lang.String wrappedKeyAlgorithm, int wrappedKeyType)Unwraps a key using this cipher instance.
This method has been added to this class (for backwards compatibility, it
cannot be abstract). If this method is not overridden, it throws an
{@code UnsupportedOperationException}.
throw new UnsupportedOperationException(
Messages.getString("crypto.11")); //$NON-NLS-1$
| protected abstract byte[] | engineUpdate(byte[] input, int inputOffset, int inputLen)Continues a multi-part transformation (encryption or decryption). The
transformed bytes are returned.
| protected abstract int | engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)Continues a multi-part transformation (encryption or decryption). The
transformed bytes are stored in the {@code output} buffer.
If the size of the {@code output} buffer is too small to hold the result,
a {@code ShortBufferException} is thrown. Use
{@link Cipher#getOutputSize getOutputSize} to check for the size of the
output buffer.
| protected int | engineUpdate(java.nio.ByteBuffer input, java.nio.ByteBuffer output)Continues a multi-part transformation (encryption or decryption). The
{@code input.remaining()} bytes starting at {@code input.position()} are
transformed and stored in the {@code output} buffer.
If the {@code output.remaining()} is too small to hold the transformed
bytes a {@code ShortBufferException} is thrown. Use
{@link Cipher#getOutputSize getOutputSize} to check for the size of the
output buffer.
if (input == null) {
throw new NullPointerException(Messages.getString("crypto.0C")); //$NON-NLS-1$
}
if (output == null) {
throw new NullPointerException(Messages.getString("crypto.0D")); //$NON-NLS-1$
}
int position = input.position();
int limit = input.limit();
if ((limit - position) <= 0) {
return 0;
}
byte[] bInput;
byte[] bOutput;
if (input.hasArray()) {
bInput = input.array();
int offset = input.arrayOffset();
bOutput = engineUpdate(bInput, offset + position, limit - position);
input.position(limit);
} else {
bInput = new byte[limit - position];
input.get(bInput);
bOutput = engineUpdate(bInput, 0, limit - position);
}
if (output.remaining() < bOutput.length) {
throw new ShortBufferException(Messages.getString("crypto.0E")); //$NON-NLS-1$
}
try {
output.put(bOutput);
} catch (java.nio.BufferOverflowException e) {
throw new ShortBufferException(Messages.getString("crypto.0F", e)); //$NON-NLS-1$
}
return bOutput.length;
| protected byte[] | engineWrap(java.security.Key key)Wraps a key using this cipher instance. This method has been added to
this class (for backwards compatibility, it cannot be abstract). If this
method is not overridden, it throws an {@code
UnsupportedOperationException}.
throw new UnsupportedOperationException(
Messages.getString("crypto.10")); //$NON-NLS-1$
|
|