1 package org.bouncycastle.asn1.x509;
4 import java.util.Enumeration;
6 import org.bouncycastle.asn1.*;
8 public class AlgorithmIdentifier
9 implements DEREncodable
11 private DERObjectIdentifier objectId;
12 private DEREncodable parameters;
13 private boolean parametersDefined = false;
15 public static AlgorithmIdentifier getInstance(
19 return getInstance(ASN1Sequence.getInstance(obj, explicit));
22 public static AlgorithmIdentifier getInstance(
25 if (obj instanceof AlgorithmIdentifier)
27 return (AlgorithmIdentifier)obj;
30 if (obj instanceof DERObjectIdentifier)
32 return new AlgorithmIdentifier((DERObjectIdentifier)obj);
35 if (obj instanceof String)
37 return new AlgorithmIdentifier((String)obj);
40 if (obj instanceof ASN1Sequence)
42 return new AlgorithmIdentifier((ASN1Sequence)obj);
45 throw new IllegalArgumentException("unknown object in factory");
48 public AlgorithmIdentifier(
49 DERObjectIdentifier objectId)
51 this.objectId = objectId;
54 public AlgorithmIdentifier(
57 this.objectId = new DERObjectIdentifier(objectId);
60 public AlgorithmIdentifier(
61 DERObjectIdentifier objectId,
62 DEREncodable parameters)
64 parametersDefined = true;
65 this.objectId = objectId;
66 this.parameters = parameters;
69 public AlgorithmIdentifier(
72 objectId = (DERObjectIdentifier)seq.getObjectAt(0);
76 parametersDefined = true;
77 parameters = seq.getObjectAt(1);
85 public DERObjectIdentifier getObjectId()
90 public DEREncodable getParameters()
97 * AlgorithmIdentifier ::= SEQUENCE {
98 * algorithm OBJECT IDENTIFIER,
99 * parameters ANY DEFINED BY algorithm OPTIONAL }
102 public DERObject getDERObject()
104 DERConstructedSequence seq = new DERConstructedSequence();
106 seq.addObject(objectId);
108 if (parametersDefined)
110 seq.addObject(parameters);
116 public boolean equals(
119 if ((o == null) || !(o instanceof AlgorithmIdentifier))
124 AlgorithmIdentifier other = (AlgorithmIdentifier)o;
126 if (!this.getObjectId().equals(other.getObjectId()))
131 if (this.getParameters() == null && other.getParameters() == null)
136 if (this.getParameters() == null || other.getParameters() == null)
141 ByteArrayOutputStream b1Out = new ByteArrayOutputStream();
142 ByteArrayOutputStream b2Out = new ByteArrayOutputStream();
143 DEROutputStream d1Out = new DEROutputStream(b1Out);
144 DEROutputStream d2Out = new DEROutputStream(b2Out);
148 d1Out.writeObject(this.getParameters());
149 d2Out.writeObject(other.getParameters());
151 byte[] b1 = b1Out.toByteArray();
152 byte[] b2 = b2Out.toByteArray();
154 if (b1.length != b2.length)
159 for (int i = 0; i != b1.length; i++)