当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。