Java Signature sign() method with Examples
Last Updated :
11 May, 2021
sign( byte[] dataBuffer, int offset, int length )
The sign() method of java.security.Provider class is used to finish the signature operation and stores the resulting signature bytes in the provided buffer dataBuffer, starting at offset. The format of the signature depends on the underlying signature scheme.
This signature object is reset to its initial state (the state it was in after a call to one of the initSign methods) and can be reused to generate further signatures with the same private key.
Syntax:
public final int sign( byte[] data, int offset, int length )
Parameters: This method takes the following argument as a parameter
dataBuffer– buffer for the signature result as byte[] array.
offset– offset into dataBuffer where the signature is stored.
length– number of bytes within dataBuffer allotted for the signature.
Return Value: This method returns the number of bytes placed into dataBuffer.
Exception: This method throws SignatureException if this signature object is not initialized properly or if this signature algorithm is unable to process the input data provided.
Below are the examples to illustrate the sign() method:
Note: The following program will not run in online IDE
Example 1:
Java
import java.security.*;
import java.util.*;
import sun.misc.BASE64Encoder;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
KeyPair keyPair = getKeyPair();
byte [] outbuff = new byte [ 1000 ];
byte [] data = "test" .getBytes( "UTF8" );
Signature sr
= Signature.getInstance( "SHA1WithRSA" );
sr.initSign(keyPair.getPrivate());
sr.update(data);
int bytes = sr.sign(outbuff, 0 , 550 );
System.out.println( "Signature:" + bytes);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (SignatureException e) {
System.out.println( "Exception thrown : " + e);
}
}
private static KeyPair getKeyPair()
throws NoSuchAlgorithmException
{
KeyPairGenerator kpg
= KeyPairGenerator.getInstance( "RSA" );
kpg.initialize( 1024 );
return kpg.genKeyPair();
}
}
|
Output:
Signature:128
Example 2:
Java
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
byte [] outbuff = new byte [ 1000 ];
Signature sr
= Signature.getInstance( "SHA1WithRSA" );
;
int bytes = sr.sign(outbuff, 0 , 550 );
System.out.println( "Signature:" + bytes);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (SignatureException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Exception thrown : java.security.SignatureException: object not initialized for signing
sign()
The sign() method of java.security.Provider class is used to return the signature bytes of all the data updated. The format of the signature depends on the underlying signature scheme.
A call to this method resets this signature object to the state it was in when previously initialized for signing via a call to initSign(PrivateKey). That is, the object is reset and available to generate another signature from the same signer, if desired, via new calls to update and sign.
Return Value: This method returns the signature bytes of the signing operation’s result.
Exception: This method throws SignatureException if this signature object is not initialized properly or if this signature algorithm is unable to process the input data provided.
Below are the examples to illustrate the sign() method:
Note: The following program will not run in online IDE
Example 1:
Java
import java.security.*;
import java.util.*;
import sun.misc.BASE64Encoder;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
KeyPair keyPair = getKeyPair();
byte [] data = "test" .getBytes( "UTF8" );
Signature sr
= Signature.getInstance( "SHA1WithRSA" );
sr.initSign(keyPair.getPrivate());
sr.update(data);
byte [] bytes = sr.sign();
System.out.println( "Signature:"
+ Arrays.toString(bytes));
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (SignatureException e) {
System.out.println( "Exception thrown : " + e);
}
}
private static KeyPair getKeyPair()
throws NoSuchAlgorithmException
{
KeyPairGenerator kpg
= KeyPairGenerator.getInstance( "RSA" );
kpg.initialize( 1024 );
return kpg.genKeyPair();
}
}
|
Output:
Signature : [96, 101, 38, 76, ... -59]
Example 2:
Java
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
byte [] outbuff = new byte [ 1000 ];
Signature sr
= Signature.getInstance( "SHA1WithRSA" );
;
System.out.println(
"Trying to get"
+ " the signature byte before initializing" );
byte [] bytes = sr.sign();
System.out.println( "Signature:" + bytes);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (SignatureException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Trying to get the signature byte before initializing
Exception thrown : java.security.SignatureException: object not initialized for signing
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...