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


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


getInstance(String algorithm)

java.security.Provider類的getInstance()方法用於返回實現指定簽名算法的Signature對象。

此方法從最喜歡的提供者開始遍曆已注冊的安全提供者列表。返回一個新的Signature對象,該對象封裝了第一個支持指定算法的Provider的SignatureSpi實現。

用法:


public static Signature getInstance(String algorithm)
    throws NoSuchAlgorithmException

參數:此方法將“算法”的標準名稱作為參數。

返回值:此方法返回新的Signature對象。

異常:如果沒有提供者支持指定算法的Signature實現,則此方法將引發NoSuchAlgorithmException。

下麵是說明getInstance()方法的示例:

範例1:

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // creating the object of Signature and getting instance 
            // By usng getInstance() method 
            Signature sr = Signature.getInstance("SHA1WithRSA"); 
  
            // getting the status of signature object 
            String str = sr.toString(); 
  
            // printing the status 
            System.out.println("Status:" + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
        catch (ProviderException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
    } 
}
輸出:
Status:Signature object:SHA1WithRSA

範例2:顯示NoSuchAlgorithmException

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // creating the object of Signature and getting instance 
            // By usng getInstance() method 
            System.out.println("Trying to get the instance of unknown instance"); 
            Signature sr = Signature.getInstance("TAJMAHAL"); 
  
            // getting the status of signature object 
            String str = sr.toString(); 
  
            // printing the status 
            System.out.println("Status:" + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
        catch (ProviderException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
    } 
}
輸出:
Trying to get the instance of unknown instance
Exception thrown:java.security.NoSuchAlgorithmException:TAJMAHAL Signature not available

Signature getInstance(String algorithm, Provider provider)

java.security.Provider類的getInstance()方法用於返回實現指定簽名算法的Signature對象。

返回一個封裝了指定Provider對象中的SignatureSpi實現的新Signature對象。請注意,指定的提供程序對象不必在提供程序列表中注冊。


用法:

public static Signature 
    getInstance(String algorithm, Provider provider)
        throws NoSuchAlgorithmException

參數:此方法將以下參數作為參數:

  • algorithm-所請求算法的名稱。
  • provider-提供者

返回值:此方法返回新的Signature對象。

異常:此方法引發以下異常:

  • NoSuchAlgorithmException-如果指定的Provider對象沒有提供指定算法的SignatureSpi實現。
  • IllegalArgumentException-如果提供者為null。

下麵是說明getInstance()方法的示例:

範例1:

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // creating the object of Signature and getting instance 
            // By usng getInstance() method 
            Signature sr = Signature.getInstance("SHA1WithRSA"); 
  
            // creating Provider object 
            Provider pd = sr.getProvider(); 
  
            // getting algorithm name 
            // by using     getAlgorithm() mathod 
            String algo = sr.getAlgorithm(); 
  
            // creating the object of Signature and getting instance 
            // By usng getInstance() method 
            Signature sr1 = Signature.getInstance(algo, pd); 
  
            // getting the status of signature object 
            String str = sr1.toString(); 
  
            // printing the status 
            System.out.println("Status:" + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
        catch (ProviderException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
    } 
}
輸出:
Status:Signature object:SHA1WithRSA

範例2:顯示NoSuchAlgorithmException

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // creating the object of Signature and getting instance 
            // By usng getInstance() method 
            Signature sr = Signature.getInstance("SHA1WithRSA"); 
  
            // creating Provider object 
            Provider pd = sr.getProvider(); 
  
            // getting algorithm name 
            // by using     getAlgorithm() mathod 
            String algo = sr.getAlgorithm(); 
  
            // creating the object of Signature and getting instance 
            // By usng getInstance() method 
            Signature sr1 = Signature.getInstance("TAJMAHAL", pd); 
  
            // getting the status of signature object 
            String str = sr1.toString(); 
  
            // printing the status 
            System.out.println("Status:" + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
        catch (ProviderException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
    } 
}
輸出:
Exception thrown:java.security.NoSuchAlgorithmException:no such algorithm:TAJMAHAL for provider SunRsaSign

範例3:顯示IllegalArgumentException

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // creating the object of Signature and getting instance 
            // By usng getInstance() method 
            Signature sr = Signature.getInstance("SHA1WithRSA"); 
  
            // creating Provider object 
            Provider pd = null; 
  
            // getting algorithm name 
            // by using     getAlgorithm() mathod 
            String algo = sr.getAlgorithm(); 
  
            // creating the object of Signature and getting instance 
            // By usng getInstance() method 
            Signature sr1 = Signature.getInstance(algo, pd); 
  
            // getting the status of signature object 
            String str = sr1.toString(); 
  
            // printing the status 
            System.out.println("Status:" + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
        catch (ProviderException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
    } 
}
輸出:
Exception thrown:java.lang.IllegalArgumentException:missing provider


相關用法


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