1package org.bouncycastle.jcajce.util; 2 3import java.io.IOException; 4import java.security.AlgorithmParameters; 5 6import org.bouncycastle.asn1.ASN1Encodable; 7import org.bouncycastle.asn1.ASN1Primitive; 8 9/** 10 * General JCA/JCE utility methods. 11 */ 12public class AlgorithmParametersUtils 13{ 14 15 16 private AlgorithmParametersUtils() 17 { 18 19 } 20 21 /** 22 * Extract an ASN.1 encodable from an AlgorithmParameters object. 23 * 24 * @param params the object to get the encoding used to create the return value. 25 * @return an ASN.1 object representing the primitives making up the params parameter. 26 * @throws IOException if an encoding cannot be extracted. 27 */ 28 public static ASN1Encodable extractParameters(AlgorithmParameters params) 29 throws IOException 30 { 31 // we try ASN.1 explicitly first just in case and then role back to the default. 32 ASN1Encodable asn1Params; 33 try 34 { 35 asn1Params = ASN1Primitive.fromByteArray(params.getEncoded("ASN.1")); 36 } 37 catch (Exception ex) 38 { 39 asn1Params = ASN1Primitive.fromByteArray(params.getEncoded()); 40 } 41 42 return asn1Params; 43 } 44 45 /** 46 * Load an AlgorithmParameters object with the passed in ASN.1 encodable - if possible. 47 * 48 * @param params the AlgorithmParameters object to be initialised. 49 * @param sParams the ASN.1 encodable to initialise params with. 50 * @throws IOException if the parameters cannot be initialised. 51 */ 52 public static void loadParameters(AlgorithmParameters params, ASN1Encodable sParams) 53 throws IOException 54 { 55 // we try ASN.1 explicitly first just in case and then role back to the default. 56 try 57 { 58 params.init(sParams.toASN1Primitive().getEncoded(), "ASN.1"); 59 } 60 catch (Exception ex) 61 { 62 params.init(sParams.toASN1Primitive().getEncoded()); 63 } 64 } 65} 66