public class RawRSAPkcs1v15Signature extends RSASignature
In contrast to the RawRSASignature engine class
where the application has to do both hash calculation and DigestInfo wrapping, this class
only expects that the hash value is supplied to one of its update methods and does the
DigestInfo wrapping/unwrapping itself. Since building the DigestInfo requires the knowledge
of the hash algorithm in use, the calling application has to supply the corresponding
algorithm id as RSASSAPkcs1v15ParameterSpec:
AlgorithmID hashAlgorithm = AlgorithmID.sha1; RSASSAPkcs1v15ParameterSpec params = new RSASSAPkcs1v15ParameterSpec(hashAlgorithm);
Generally an application wishing to sign some message or to verify some signature, generally
has to perform four steps (in the following example, HASH has to be
replaced by the name of the desired hash algorithm):
getInstance method, e.g.:
Signature rawRsaSignatureEngine = Signature.getInstance("RSAPkcs15", "IAIK");
rawRsaSignatureEngine.initSign(rsaPrivateKey);
rawRsaSignatureEngine.initVerify(rsaPublicKey);
AlgorithmID hashAlgorithm = AlgorithmID.HASH; // e.g. AlgorithmID.sha1 RSASSAPkcs1v15ParameterSpec params = new RSASSAPkcs1v15ParameterSpec(hashAlgorithm); rawRsaSignatureEngine.setParameter(null, params);
sign method returning the signature as byte array.
Otherwise, if the Signature object has been initialized for verifying, first the
prepared hash to be verified is supplied to the Signature object, and subsequently the
signature is verified by calling the verify method, supplied with
the byte array holding the corresponding signature value:
rawRsaSignatureEngine.update(preparedHash); byte[] signature = rawRsaSignatureEngine.sign();
rawRsaSignatureEngine.update(preparedHash);
System.out.println("Signature " + (rawRsaSignatureEngine.verify(signature) ? "correct!" : "incorrect!"));
update method; no check is
performed if the supplied hash value corresponds to the hash algorithm
in use (e.g. has the correct length).Signature| Modifier and Type | Field and Description |
|---|---|
protected java.security.MessageDigest |
hash
The MessageDigest engine used to hash the data; supplied with an instance of the desired MessageDigest
algorithm by any extending subclass.
|
| Constructor and Description |
|---|
RawRSAPkcs1v15Signature()
Default constructor.
|
| Modifier and Type | Method and Description |
|---|---|
protected java.lang.Object |
engineGetParameter(java.lang.String param)
Returns the hash algorithm parameter used by this PKCS#1.5 signature engine.
|
protected java.security.AlgorithmParameters |
engineGetParameters()
Returns the hash algorithm parameter used by this PKCS#1.5 signature engine.
|
protected void |
engineInitSign(java.security.PrivateKey pk)
SPI: Initializes this Signature object with the given
RSA private key for going to sign some data.
|
protected void |
engineInitSign(java.security.PrivateKey pk,
java.security.SecureRandom random)
SPI: Initializes this Signature object with the given
RSA private key for going to sign some data.
|
protected void |
engineInitVerify(java.security.PublicKey pk)
SPI: Initializes this Signature object with the given
RSA public key for performing a signature verification.
|
protected void |
engineSetParameter(java.security.spec.AlgorithmParameterSpec params)
Sets the hash algorithm parameter to be used by this PKCS#1.5 signature engine.
|
protected void |
engineSetParameter(java.lang.String param,
java.lang.Object value)
Sets the hash algorithm parameter to be used by this PKCS#1.5 signature engine.
|
protected void |
engineUpdate(byte b)
SPI: Updates the data to be signed or verified
with the specified byte.
|
protected void |
engineUpdate(byte[] b,
int off,
int len)
SPI: Updates the data to be signed or verified with the
specified number of bytes, beginning at the specified offset within the given byte array.
|
engineSign, engineVerifyprotected java.security.MessageDigest hash
public RawRSAPkcs1v15Signature()
Applications use
Signature.getInstance("RSAPkcs15");
for creating a Signature object.protected void engineSetParameter(java.lang.String param,
java.lang.Object value)
throws java.security.InvalidParameterException
RawRSASignature engine class
where the application has to do both hash calculation and DigestInfo wrapping, this class
only expects that the hash value is supplied to one of its update methods and does the
DigestInfo wrapping/unwrapping itself. Since building the DigestInfo requires the knowledge
of the hash algorithm in use, the calling application has to supply the corresponding
algorithm id as RSASSAPkcs1v15ParameterSpec:
AlgorithmID hashAlgorithm = AlgorithmID.sha1; RSASSAPkcs1v15ParameterSpec params = new RSASSAPkcs1v15ParameterSpec(hashAlgorithm); rawRsaSignatureEngine.setParameter(null, params);
param - ignoredvalue - the hash algorithm supplied as RSASSAPkcs1v15ParameterSpecjava.security.InvalidParameterException - if the hash algorithm is not supplied as
RSASSAPkcs1v15ParameterSpecprotected void engineSetParameter(java.security.spec.AlgorithmParameterSpec params)
throws java.security.InvalidAlgorithmParameterException
RawRSASignature engine class
where the application has to do both hash calculation and DigestInfo wrapping, this class
only expects that the hash value is supplied to one of its update methods and does the
DigestInfo wrapping/unwrapping itself. Since building the DigestInfo requires the knowledge
of the hash algorithm in use, the calling application has to supply the corresponding
algorithm id as RSASSAPkcs1v15ParameterSpec:
AlgorithmID hashAlgorithm = AlgorithmID.sha1; RSASSAPkcs1v15ParameterSpec params = new RSASSAPkcs1v15ParameterSpec(hashAlgorithm); rawRsaSignatureEngine.setParameter(null, params);
params - the hash algorithm supplied as RSASSAPkcs1v15ParameterSpecjava.security.InvalidParameterException - if the hash algorithm is not supplied as
RSASSAPkcs1v15ParameterSpecjava.security.InvalidAlgorithmParameterExceptionprotected java.lang.Object engineGetParameter(java.lang.String param)
throws java.security.InvalidParameterException
param - ignoredjava.security.InvalidParameterException - should not occurprotected java.security.AlgorithmParameters engineGetParameters()
engineGetParameters in class java.security.SignatureSpiprotected void engineInitVerify(java.security.PublicKey pk)
throws java.security.InvalidKeyException
engineInitVerify in class java.security.SignatureSpipk - the RSA public key belonging to the RSA private key that has been used for signing.java.security.InvalidKeyException - if a key encoding error occursprotected void engineInitSign(java.security.PrivateKey pk)
throws java.security.InvalidKeyException
engineInitSign in class java.security.SignatureSpipk - the RSA private key to be used for signing.java.security.InvalidKeyException - if a key encoding error occursprotected void engineInitSign(java.security.PrivateKey pk,
java.security.SecureRandom random)
throws java.security.InvalidKeyException
If a SecureRandom never has been supplied by the application, the signature engine will use a default SecureRandom, if required.
engineInitSign in class java.security.SignatureSpipk - the RSA private key to be used for signing.random - the SecureRandom if random numbers are required by the signature engine (e.g. PSS)java.security.InvalidKeyException - if a key encoding error occursprotected void engineUpdate(byte b)
engineUpdate in class java.security.SignatureSpib - the byte to be used for updating.protected void engineUpdate(byte[] b,
int off,
int len)
engineUpdate in class java.security.SignatureSpib - the byte array holding the data to be used for this update operation.off - the offset, indicating the start position within the given byte array.len - the number of bytes to be obtained from the given byte array, starting at the given position.