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