sign( byte[] dataBuffer, int offset, int length )
java.security.Provider类的sign()方法用于完成签名操作,并将生成的签名字节存储在提供的缓冲区dataBuffer中(从offset开始)。签名的格式取决于基础签名方案。
该签名对象被重置为其初始状态(在调用initSign方法之一之后所处的状态),并且可以被重用以使用同一私钥生成进一步的签名。
用法:
public final int sign( byte[] data, int offset, int length )
参数:此方法将以下参数作为参数
dataBuffer-签名结果的缓冲区,为byte []数组。
offset-偏移到存储签名的dataBuffer中。
length-分配给签名的dataBuffer中的字节数。
返回值:此方法返回放入dataBuffer的字节数。
异常:如果此签名对象未正确初始化,或者此签名算法无法处理提供的输入数据,则此方法将引发SignatureException。
下面是说明sign()方法的示例:
注意:以下程序将无法在在线IDE中运行
范例1:
// Java program to demonstrate
// sign() method
import java.security.*;
import java.util.*;
import sun.misc.BASE64Encoder;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// calling getKeyPair() method and assining in keypair
KeyPair keyPair = getKeyPair();
// creating byte array object
byte[] outbuff = new byte[1000];
// data to be updated
byte[] data = "test".getBytes("UTF8");
// creating the object of Signature
Signature sr = Signature.getInstance("SHA1WithRSA");
// initializing the signature object with key pair
// for signing
sr.initSign(keyPair.getPrivate());
// updating the data
sr.update(data);
// getting the number of bytes
// placed in outbuffer
// by using method sign()
int bytes = sr.sign(outbuff, 0, 550);
// pritning the number of byte
System.out.println("Signature:" + bytes);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown:" + e);
}
catch (SignatureException e) {
System.out.println("Exception thrown:" + e);
}
}
// defining getKeyPair method
private static KeyPair getKeyPair() throws NoSuchAlgorithmException
{
// creating the object of KeyPairGenerator
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
// initializing with 1024
kpg.initialize(1024);
// returning the key pairs
return kpg.genKeyPair();
}
}
输出:
Signature:128
范例2:
// Java program to demonstrate
// sign() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating byte array object
byte[] outbuff = new byte[1000];
// creating the object of Signature
Signature sr = Signature.getInstance("SHA1WithRSA");
;
// getting the number of bytes
// placed in outbuffer
// by using method sign()
int bytes = sr.sign(outbuff, 0, 550);
// pritning the number of byte
System.out.println("Signature:" + bytes);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown:" + e);
}
catch (SignatureException e) {
System.out.println("Exception thrown:" + e);
}
}
}
输出:
Exception thrown:java.security.SignatureException:object not initialized for signing
sign()
java.security.Provider类的sign()方法用于返回所有更新数据的签名字节。签名的格式取决于基础签名方案。
对此方法的调用会将此签名对象重置为先前通过调用initSign(PrivateKey)进行初始化以进行签名时的状态。也就是说,如果需要,可以通过更新和签名的新调用来重置对象并使其从同一签名者生成另一个签名。
返回值:此方法返回签名操作结果的签名字节。
异常:如果此签名对象未正确初始化,或者此签名算法无法处理提供的输入数据,则此方法将引发SignatureException。
下面是说明sign()方法的示例:
注意:以下程序将无法在在线IDE中运行
范例1:
// Java program to demonstrate
// sign() method
import java.security.*;
import java.util.*;
import sun.misc.BASE64Encoder;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// calling getKeyPair() method and assining in keypair
KeyPair keyPair = getKeyPair();
// data to be updated
byte[] data = "test".getBytes("UTF8");
// creating the object of Signature
Signature sr = Signature.getInstance("SHA1WithRSA");
// initializing the signature object with key pair
// for signing
sr.initSign(keyPair.getPrivate());
// updating the data
sr.update(data);
// getting the signature byte
// of an signing operation
// by using method sign()
byte[] bytes = sr.sign();
// pritning the number of byte
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);
}
}
// defining getKeyPair method
private static KeyPair getKeyPair() throws NoSuchAlgorithmException
{
// creating the object of KeyPairGenerator
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
// initializing with 1024
kpg.initialize(1024);
// returning the key pairs
return kpg.genKeyPair();
}
}
输出:
Signature:[96, 101, 38, 76, ... -59]
范例2:
// Java program to demonstrate
// sign() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating byte array object
byte[] outbuff = new byte[1000];
// creating the object of Signature
Signature sr = Signature.getInstance("SHA1WithRSA");
;
// getting the number of bytes
// placed in outbuffer
// by using method sign()
System.out.println("Trying to get"
+ " the signature byte before initializing");
byte[] bytes = sr.sign();
// pritning the number of byte
System.out.println("Signature:" + bytes);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown:" + e);
}
catch (SignatureException e) {
System.out.println("Exception thrown:" + e);
}
}
}
输出:
Trying to get the signature byte before initializing Exception thrown:java.security.SignatureException:object not initialized for signing
相关用法
- Java Signature getInstance()用法及代码示例
- Java Signature initSign()用法及代码示例
- Java Signature getAlgorithm()用法及代码示例
- Java Signature getProvider()用法及代码示例
- Java Signature toString()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java lang.Long.builtcount()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Java Signature sign() method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。