Class Signature
- java.lang.Object
-
- java.security.SignatureSpi
-
- java.security.Signature
-
public abstract class Signature extends SignatureSpi
Signatureis an engine class which is capable of creating and verifying digital signatures, using different algorithms that have been registered with theSecurityclass.- See Also:
SignatureSpi
-
-
Field Summary
Fields Modifier and Type Field Description protected static intSIGNConstant that indicates that thisSignatureinstance has been initialized for signing.protected intstateRepresents the current state of thisSignature.protected static intUNINITIALIZEDConstant that indicates that thisSignatureinstance has not yet been initialized.protected static intVERIFYConstant that indicates that thisSignatureinstance has been initialized for verification.-
Fields inherited from class java.security.SignatureSpi
appRandom
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description StringgetAlgorithm()Returns the name of the algorithm of thisSignature.SignatureSpigetCurrentSpi()Returns theSignatureSpibacking thisSignatureornullif noSignatureSpiis backing thisSignature.static SignaturegetInstance(String algorithm)Returns a new instance ofSignaturethat utilizes the specified algorithm.static SignaturegetInstance(String algorithm, String provider)Returns a new instance ofSignaturethat utilizes the specified algorithm from the specified provider.static SignaturegetInstance(String algorithm, Provider provider)Returns a new instance ofSignaturethat utilizes the specified algorithm from the specified provider.ObjectgetParameter(String param)Deprecated.There is no generally accepted parameter naming convention.AlgorithmParametersgetParameters()Returns theAlgorithmParametersof thisSignatureinstance.ProvidergetProvider()Returns the provider associated with thisSignature.voidinitSign(PrivateKey privateKey)Initializes thisSignatureinstance for signing, using the private key of the identity whose signature is going to be generated.voidinitSign(PrivateKey privateKey, SecureRandom random)Initializes thisSignatureinstance for signing, using the private key of the identity whose signature is going to be generated and the specified source of randomness.voidinitVerify(Certificate certificate)Initializes thisSignatureinstance for signature verification, using the certificate of the identity whose signature is going to be verified.voidinitVerify(PublicKey publicKey)Initializes thisSignatureinstance for signature verification, using the public key of the identity whose signature is going to be verified.voidsetParameter(String param, Object value)Deprecated.UsesetParameter(AlgorithmParameterSpec)instead.voidsetParameter(AlgorithmParameterSpec params)Sets the specifiedAlgorithmParameterSpec.byte[]sign()Generates and returns the signature of all updated data.intsign(byte[] outbuf, int offset, int len)Generates and stores the signature of all updated data in the providedbyte[]at the specified position with the specified length.StringtoString()Returns a string containing a concise, human-readable description of thisSignatureincluding its algorithm and its state.voidupdate(byte b)Updates the data to be verified or to be signed, using the specifiedbyte.voidupdate(byte[] data)Updates the data to be verified or to be signed, using the specifiedbyte[].voidupdate(byte[] data, int off, int len)Updates the data to be verified or to be signed, using the givenbyte[], starting form the specified index for the specified length.voidupdate(ByteBuffer data)Updates the data to be verified or to be signed, using the specifiedByteBuffer.booleanverify(byte[] signature)Indicates whether the givensignaturecan be verified using the public key or a certificate of the signer.booleanverify(byte[] signature, int offset, int length)Indicates whether the givensignaturestarting at indexoffsetwithlengthbytes can be verified using the public key or a certificate of the signer.-
Methods inherited from class java.security.SignatureSpi
clone, engineGetParameter, engineGetParameters, engineInitSign, engineInitSign, engineInitVerify, engineSetParameter, engineSetParameter, engineSign, engineSign, engineUpdate, engineUpdate, engineUpdate, engineVerify, engineVerify
-
-
-
-
Field Detail
-
UNINITIALIZED
protected static final int UNINITIALIZED
Constant that indicates that thisSignatureinstance has not yet been initialized.- See Also:
- Constant Field Values
-
SIGN
protected static final int SIGN
Constant that indicates that thisSignatureinstance has been initialized for signing.- See Also:
- Constant Field Values
-
VERIFY
protected static final int VERIFY
Constant that indicates that thisSignatureinstance has been initialized for verification.- See Also:
- Constant Field Values
-
state
protected int state
Represents the current state of thisSignature. The three possible states areUNINITIALIZED,SIGNorVERIFY.
-
-
Constructor Detail
-
Signature
protected Signature(String algorithm)
Constructs a new instance ofSignaturewith the name of the algorithm to use.- Parameters:
algorithm- the name of algorithm to use.
-
-
Method Detail
-
getInstance
public static Signature getInstance(String algorithm) throws NoSuchAlgorithmException
Returns a new instance ofSignaturethat utilizes the specified algorithm.- Parameters:
algorithm- the name of the algorithm to use.- Returns:
- a new instance of
Signaturethat utilizes the specified algorithm. - Throws:
NoSuchAlgorithmException- if the specified algorithm is not available.NullPointerException- ifalgorithmisnull.
-
getInstance
public static Signature getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
Returns a new instance ofSignaturethat utilizes the specified algorithm from the specified provider.- Parameters:
algorithm- the name of the algorithm to use.provider- the name of the provider.- Returns:
- a new instance of
Signaturethat utilizes the specified algorithm from the specified provider. - Throws:
NoSuchAlgorithmException- if the specified algorithm is not available.NoSuchProviderException- if the specified provider is not available.NullPointerException- ifalgorithmisnull.IllegalArgumentException- ifprovider == null || provider.isEmpty()
-
getInstance
public static Signature getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
Returns a new instance ofSignaturethat utilizes the specified algorithm from the specified provider. Theprovidersupplied does not have to be registered.- Parameters:
algorithm- the name of the algorithm to use.provider- the security provider.- Returns:
- a new instance of
Signaturethat utilizes the specified algorithm from the specified provider. - Throws:
NoSuchAlgorithmException- if the specified algorithm is not available.NullPointerException- ifalgorithmisnull.IllegalArgumentException- ifprovider == null
-
getProvider
public final Provider getProvider()
Returns the provider associated with thisSignature.- Returns:
- the provider associated with this
Signature.
-
getCurrentSpi
public SignatureSpi getCurrentSpi()
Returns theSignatureSpibacking thisSignatureornullif noSignatureSpiis backing thisSignature.
-
getAlgorithm
public final String getAlgorithm()
Returns the name of the algorithm of thisSignature.- Returns:
- the name of the algorithm of this
Signature.
-
initVerify
public final void initVerify(PublicKey publicKey) throws InvalidKeyException
Initializes thisSignatureinstance for signature verification, using the public key of the identity whose signature is going to be verified.- Parameters:
publicKey- the public key.- Throws:
InvalidKeyException- ifpublicKeyis not valid.
-
initVerify
public final void initVerify(Certificate certificate) throws InvalidKeyException
Initializes thisSignatureinstance for signature verification, using the certificate of the identity whose signature is going to be verified.If the given certificate is an instance of
X509Certificateand has a key usage parameter that indicates, that this certificate is not to be used for signing, anInvalidKeyExceptionis thrown.- Parameters:
certificate- the certificate used to verify a signature.- Throws:
InvalidKeyException- if the publicKey in the certificate is not valid or not to be used for signing.
-
initSign
public final void initSign(PrivateKey privateKey) throws InvalidKeyException
Initializes thisSignatureinstance for signing, using the private key of the identity whose signature is going to be generated.- Parameters:
privateKey- the private key.- Throws:
InvalidKeyException- ifprivateKeyis not valid.
-
initSign
public final void initSign(PrivateKey privateKey, SecureRandom random) throws InvalidKeyException
Initializes thisSignatureinstance for signing, using the private key of the identity whose signature is going to be generated and the specified source of randomness.- Parameters:
privateKey- the private key.random- theSecureRandomto use.- Throws:
InvalidKeyException- ifprivateKeyis not valid.
-
sign
public final byte[] sign() throws SignatureExceptionGenerates and returns the signature of all updated data.This
Signatureinstance is reset to the state of its last initialization for signing and thus can be used for another signature from the same identity.- Returns:
- the signature of all updated data.
- Throws:
SignatureException- if thisSignatureinstance is not initialized properly.
-
sign
public final int sign(byte[] outbuf, int offset, int len) throws SignatureExceptionGenerates and stores the signature of all updated data in the providedbyte[]at the specified position with the specified length.This
Signatureinstance is reset to the state of its last initialization for signing and thus can be used for another signature from the same identity.- Parameters:
outbuf- the buffer to store the signature.offset- the index of the first byte inoutbufto store.len- the number of bytes allocated for the signature.- Returns:
- the number of bytes stored in
outbuf. - Throws:
SignatureException- if thisSignatureinstance is not initialized properly.IllegalArgumentException- ifoffsetorlenare not valid in respect tooutbuf.
-
verify
public final boolean verify(byte[] signature) throws SignatureExceptionIndicates whether the givensignaturecan be verified using the public key or a certificate of the signer.This
Signatureinstance is reset to the state of its last initialization for verifying and thus can be used to verify another signature of the same signer.- Parameters:
signature- the signature to verify.- Returns:
trueif the signature was verified,falseotherwise.- Throws:
SignatureException- if thisSignatureinstance is not initialized properly.
-
verify
public final boolean verify(byte[] signature, int offset, int length) throws SignatureExceptionIndicates whether the givensignaturestarting at indexoffsetwithlengthbytes can be verified using the public key or a certificate of the signer.This
Signatureinstance is reset to the state of its last initialization for verifying and thus can be used to verify another signature of the same signer.- Parameters:
signature- thebyte[]containing the signature to verify.offset- the start index insignatureof the signature.length- the number of bytes allocated for the signature.- Returns:
trueif the signature was verified,falseotherwise.- Throws:
SignatureException- if thisSignatureinstance is not initialized properly.IllegalArgumentException- ifoffsetorlengthare not valid in respect tosignature.
-
update
public final void update(byte b) throws SignatureExceptionUpdates the data to be verified or to be signed, using the specifiedbyte.- Parameters:
b- the byte to update with.- Throws:
SignatureException- if thisSignatureinstance is not initialized properly.
-
update
public final void update(byte[] data) throws SignatureExceptionUpdates the data to be verified or to be signed, using the specifiedbyte[].- Parameters:
data- the byte array to update with.- Throws:
SignatureException- if thisSignatureinstance is not initialized properly.
-
update
public final void update(byte[] data, int off, int len) throws SignatureExceptionUpdates the data to be verified or to be signed, using the givenbyte[], starting form the specified index for the specified length.- Parameters:
data- the byte array to update with.off- the start index indataof the data.len- the number of bytes to use.- Throws:
SignatureException- if thisSignatureinstance is not initialized properly.
-
update
public final void update(ByteBuffer data) throws SignatureException
Updates the data to be verified or to be signed, using the specifiedByteBuffer.- Parameters:
data- theByteBufferto update with.- Throws:
SignatureException- if thisSignatureinstance is not initialized properly.
-
toString
public String toString()
Returns a string containing a concise, human-readable description of thisSignatureincluding its algorithm and its state.
-
setParameter
@Deprecated public final void setParameter(String param, Object value) throws InvalidParameterException
Deprecated.UsesetParameter(AlgorithmParameterSpec)instead.Sets the specified parameter to the given value.- Parameters:
param- the name of the parameter.value- the parameter value.- Throws:
InvalidParameterException- if the parameter is invalid, already set or is not allowed to be changed.
-
setParameter
public final void setParameter(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException
Sets the specifiedAlgorithmParameterSpec.- Parameters:
params- the parameter to set.- Throws:
InvalidAlgorithmParameterException- if the parameter is invalid, already set or is not allowed to be changed.
-
getParameters
public final AlgorithmParameters getParameters()
Returns theAlgorithmParametersof thisSignatureinstance.- Returns:
- the
AlgorithmParametersof thisSignatureinstance, maybenull.
-
getParameter
@Deprecated public final Object getParameter(String param) throws InvalidParameterException
Deprecated.There is no generally accepted parameter naming convention.Returns the value of the parameter with the specified name.- Parameters:
param- the name of the requested parameter value- Returns:
- the value of the parameter with the specified name, maybe
null. - Throws:
InvalidParameterException- ifparamis not a valid parameter for thisSignatureor an other error occurs.
-
-