1 package org.bouncycastle.asn1.x509;
5 import org.bouncycastle.asn1.*;
7 public class AlgorithmIdentifier
8 implements DEREncodable
10 private DERObjectIdentifier objectId;
11 private DEREncodable parameters;
12 private boolean parametersDefined = false;
14 public static AlgorithmIdentifier getInstance(
18 return getInstance(ASN1Sequence.getInstance(obj, explicit));
21 public static AlgorithmIdentifier getInstance(
24 if (obj instanceof AlgorithmIdentifier)
26 return (AlgorithmIdentifier)obj;
29 if (obj instanceof DERObjectIdentifier)
31 return new AlgorithmIdentifier((DERObjectIdentifier)obj);
34 if (obj instanceof String)
36 return new AlgorithmIdentifier((String)obj);
39 if (obj instanceof ASN1Sequence)
41 return new AlgorithmIdentifier((ASN1Sequence)obj);
44 throw new IllegalArgumentException("unknown object in factory");
47 public AlgorithmIdentifier(
48 DERObjectIdentifier objectId)
50 this.objectId = objectId;
53 public AlgorithmIdentifier(
56 this.objectId = new DERObjectIdentifier(objectId);
59 public AlgorithmIdentifier(
60 DERObjectIdentifier objectId,
61 DEREncodable parameters)
63 parametersDefined = true;
64 this.objectId = objectId;
65 this.parameters = parameters;
68 public AlgorithmIdentifier(
71 objectId = (DERObjectIdentifier)seq.getObjectAt(0);
75 parametersDefined = true;
76 parameters = seq.getObjectAt(1);
84 public DERObjectIdentifier getObjectId()
89 public DEREncodable getParameters()
96 * AlgorithmIdentifier ::= SEQUENCE {
97 * algorithm OBJECT IDENTIFIER,
98 * parameters ANY DEFINED BY algorithm OPTIONAL }
101 public DERObject getDERObject()
103 DERConstructedSequence seq = new DERConstructedSequence();
105 seq.addObject(objectId);
107 if (parametersDefined)
109 seq.addObject(parameters);
115 public boolean equals(
118 if ((o == null) || !(o instanceof AlgorithmIdentifier))
123 AlgorithmIdentifier other = (AlgorithmIdentifier)o;
125 if (!this.getObjectId().equals(other.getObjectId()))
130 if (this.getParameters() == null && other.getParameters() == null)
135 if (this.getParameters() == null || other.getParameters() == null)
140 ByteArrayOutputStream b1Out = new ByteArrayOutputStream();
141 ByteArrayOutputStream b2Out = new ByteArrayOutputStream();
142 DEROutputStream d1Out = new DEROutputStream(b1Out);
143 DEROutputStream d2Out = new DEROutputStream(b2Out);
147 d1Out.writeObject(this.getParameters());
148 d2Out.writeObject(other.getParameters());
150 byte[] b1 = b1Out.toByteArray();
151 byte[] b2 = b2Out.toByteArray();
153 if (b1.length != b2.length)
158 for (int i = 0; i != b1.length; i++)