當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Signature sign()用法及代碼示例


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


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Java Signature sign() method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。