Generate Digital Signature with RSA Private Key using message/text
In this approach, a digital signature is created by taking a message and applying a hash function to the fingerprint of the message (which creates a message digest) and then using the sender's private key to encrypt the message digest. The message to be signed and the digital signature is then sent to the recipient to verify it using the public key of the sender.
Get an instance of a Signature object and pass SHA256withRSA algorithm as an argument .
Obtain an instance of the key you want to use for the Sign operation using the getPrivateKey method of the NAEKey class.
(Optional) You can also use the saltLength parameter to generate signature.
Initialize the Signature object with the key.
Convert the text string, “Hello World!” to a byte array and pass it to the update method of the Signature object.
Invoke the sign method of the Signature object.
Code sample
In the following example, a string Hello World! is signed with the RSA key, named rsakey. The algorithm used in this sample is SHA256withRSA. In this example we are demonstrating how to sign data using the saltLength parameter.
Signature sig = Signature.getInstance("SHA256withRSA", "IngrianProvider");
PrivateKey key = NAEKey.getPrivateKey("rsakey");
int saltLength=80;
PSSParameterSpec pssParameterSpec = new PSSParameterSpec(saltLength);
sig.setParameter(pssParameterSpec);
sig.initSign(key);
sig.update("Hello World!".getBytes());
byte[] signature = sig.sign();