java.lang.Object java.security.SignatureSpi java.security.Signaturepublic abstract class Signature extends SignatureSpi Signature is used to provide an interface to digital signature
algorithms. Digital signatures provide authentication and data integrity of
digital data.
The GNU provider provides the NIST standard DSA which uses DSA and SHA-1.
It can be specified by SHA/DSA, SHA-1/DSA or its OID. If the RSA signature
algorithm is provided then it could be MD2/RSA. MD5/RSA, or SHA-1/RSA. The
algorithm must be specified because there is no default.
Signature provides implementation-independent algorithms which are
requested by the user through the getInstance()<?code> methods. It can
be requested by specifying just the algorithm name or by specifying both the
algorithm name and provider name.
The three phases of using Signature are:
protected static int SIGN protected static int UNINITIALIZED protected static int VERIFY protected int state String getAlgorithm()static Signature getInstance(String algorithm)Signature representing the specified
signature.
static Signature getInstance(String algorithm, String provider)Signature representing the specified
signature from the named provider.
static Signature getInstance(String algorithm, Provider provider)Signature representing the specified
signature from the specified Provider.
Object getParameter(String param) AlgorithmParameters getParameters()AlgorithmParameters.
Provider getProvider()Provider of this instance.
void initSign(PrivateKey privateKey) void initSign(PrivateKey privateKey, SecureRandom random) void initVerify(PublicKey publicKey) void initVerify(Certificate certificate)Certificate. void setParameter(String param, Object value) void setParameter(AlgorithmParameterSpec params)AlgorithmParameterSpec.
byte[] sign() int sign(byte[] outbuf, int offset, int len) void update(byte b) void update(byte[] data) void update(byte[] data, int off, int len) void update(ByteBuffer input)Buffer.remaining()
bytes of the input buffer.
boolean verify(byte[] signature) boolean verify(byte[] signature, int offset, int length)protected static final int SIGN
Possible state value which signifies that this instance has been initialized for signing purposes.
- Field Value:
- 2
protected static final int UNINITIALIZED
Possible state value which signifies that this instance has not yet been initialized.
- Field Value:
- 0
protected static final int VERIFY
Possible state value which signifies that this instance has been initialized for verification purposes.
- Field Value:
- 3
public Object clone() throws CloneNotSupportedException
Returns a clone of this instance.
- Overrides:
- clone in interface SignatureSpi
- Returns:
- a clone of this instace.
- Throws:
CloneNotSupportedException- if the implementation does not support cloning.
public final String getAlgorithm()
Returns the name of the algorithm currently used. The names of algorithms are usually SHA/DSA or SHA/RSA.
- Returns:
- name of algorithm.
public static Signature getInstance(String algorithm) throws NoSuchAlgorithmException
Returns an instance ofSignaturerepresenting the specified signature.
- Parameters:
algorithm- the algorithm to use.
- Returns:
- a new instance repesenting the desired algorithm.
- Throws:
NoSuchAlgorithmException- if the algorithm is not implemented by any provider.IllegalArgumentException- ifalgorithmisnullor is an empty string.
public static Signature getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
Returns an instance ofSignaturerepresenting the specified signature from the named provider.
- Parameters:
algorithm- the algorithm to use.provider- the name of the provider to use.
- Returns:
- a new instance repesenting the desired algorithm.
- Throws:
NoSuchProviderException- if the named provider was not found.NoSuchAlgorithmException- if the algorithm is not implemented by the named provider.IllegalArgumentException- if eitheralgorithmorproviderisnullor empty.
public static Signature getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
Returns an instance ofSignaturerepresenting the specified signature from the specifiedProvider.
- Parameters:
algorithm- the algorithm to use.provider- theProviderto use.
- Returns:
- a new instance repesenting the desired algorithm.
- Throws:
NoSuchAlgorithmException- if the algorithm is not implemented by theProvider.IllegalArgumentException- if eitheralgorithmorproviderisnull, or ifalgorithmis an empty string.
public final Object getParameter(String param) throws InvalidParameterException
Deprecated. use the other getParameter
Returns the value for the specified algorithm parameter.
- Parameters:
param- the parameter name.
- Returns:
- the parameter value.
- Throws:
InvalidParameterException- if the parameter is invalid.
public final AlgorithmParameters getParameters()
Return the parameters of the algorithm used in this instance as anAlgorithmParameters.
- Returns:
- the parameters used with this instance, or
nullif this instance does not use any parameters.
public final Provider getProvider()
Returns theProviderof this instance.
- Returns:
- the
Providerof this instance.
public final void initSign(PrivateKey privateKey) throws InvalidKeyException
Initializes this class with the private key for signing purposes.
- Parameters:
privateKey- the private key to sign with.
- Throws:
InvalidKeyException- if the key is invalid.
public final void initSign(PrivateKey privateKey, SecureRandom random) throws InvalidKeyException
Initializes this class with the private key and source of randomness for signing purposes.
- Parameters:
privateKey- the private key to sign with.random- theSecureRandomto use.
- Throws:
InvalidKeyException- if the key is invalid.
public final void initVerify(PublicKey publicKey) throws InvalidKeyException
Initializes this instance with the public key for verification purposes.
- Parameters:
publicKey- the public key to verify with.
- Throws:
InvalidKeyException- if the key is invalid.
public final void initVerify(Certificate certificate) throws InvalidKeyException
Verify a signature with a designatedCertificate. This is a FIPS 140-1 compatible method since it verifies a signature with a certificate. If theCertificateis an X.509 one, has a KeyUsage parameter and that parameter indicates this key is not to be used for signing then an exception is thrown.
- Parameters:
certificate- aCertificatecontaining a public key to verify with.
- Throws:
InvalidKeyException- if the key is invalid.
public final void setParameter(String param, Object value) throws InvalidParameterException
Deprecated. use the other setParameter
Sets the specified algorithm parameter to the specified value.
- Parameters:
param- the parameter name.value- the parameter value.
- Throws:
InvalidParameterException- if the parameter is invalid, the parameter is already set and can not be changed, a security exception occured, etc.
public final void setParameter(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException
Sets the signature engine with the specifiedAlgorithmParameterSpec. By default, and unless overriden by the concrete SPI, this method always throws anUnsupportedOperationException.
- Parameters:
params- the parameters to use for intializing this instance.
- Throws:
InvalidParameterException- if the parameter is invalid, the parameter is already set and cannot be changed, a security exception occured, etc.
public final byte[] sign() throws SignatureException
Returns the signature bytes of all the data fed to this instance. The format of the output depends on the underlying signature algorithm.
- Returns:
- the signature bytes.
- Throws:
SignatureException- if the engine is not properly initialized.
public final int sign(byte[] outbuf, int offset, int len) throws SignatureException
Generates signature bytes of all the data fed to this instance and stores it in the designated array. The format of the result depends on the underlying signature algorithm. After calling this method, the instance is reset to its initial state and can then be used to generate additional signatures. IMPLEMENTATION NOTE: Neither this method nor the GNU provider will return partial digests. Iflenis less than the signature length, this method will throw aSignatureException. If it is greater than or equal then it is ignored.
- Parameters:
outbuf- array of bytes of where to store the resulting signature bytes.offset- the offset to start at in the array.len- the number of the bytes to use in the array.
- Returns:
- the real number of bytes used.
- Throws:
SignatureException- if the engine is not properly initialized.
- Since:
- 1.2
public String toString()
Returns a rstring representation of this instance.
- Returns:
- a rstring representation of this instance.
public final void update(byte b) throws SignatureException
Updates the data to be signed or verified with the specified byte.
- Parameters:
b- the byte to update with.
- Throws:
SignatureException- if the engine is not properly initialized.
public final void update(byte[] data) throws SignatureException
Updates the data to be signed or verified with the specified bytes.
- Parameters:
data- the array of bytes to use.
- Throws:
SignatureException- if the engine is not properly initialized.
public final void update(byte[] data, int off, int len) throws SignatureException
Updates the data to be signed or verified with the specified bytes.
- Parameters:
data- an array of bytes to use.off- the offset to start at in the array.len- the number of bytes to use from the array.
- Throws:
SignatureException- if the engine is not properly initialized.
public final void update(ByteBuffer input) throws SignatureException
Update this signature with theBuffer.remaining()bytes of the input buffer.
- Parameters:
input- The input buffer.
- Throws:
SignatureException- If this instance was not properly initialized.
public final boolean verify(byte[] signature) throws SignatureException
Verifies a designated signature.
- Parameters:
signature- the signature bytes to verify.
- Returns:
trueif verified,falseotherwise.
- Throws:
SignatureException- if the engine is not properly initialized or the signature does not check.
public final boolean verify(byte[] signature, int offset, int length) throws SignatureException
Verifies a designated signature.
- Parameters:
signature- the signature bytes to verify.offset- the offset to start at in the array.length- the number of the bytes to use from the array.
- Returns:
trueif verified,falseotherwise.
- Throws:
IllegalArgumentException- if thesignaturebyte array isnull, or theoffsetorlengthis less than0, or the sum of theoffsetandlengthis greater than the length of thesignaturebyte array.SignatureException- if the engine is not properly initialized or the signature does not check.