resolve darcs stupidity
[org.ibex.core.git] / src / org / bouncycastle / asn1 / x509 / AlgorithmIdentifier.java
1 package org.bouncycastle.asn1.x509;
2
3 import java.io.*;
4
5 import org.bouncycastle.asn1.*;
6
7 public class AlgorithmIdentifier
8     implements DEREncodable
9 {
10     private DERObjectIdentifier objectId;
11     private DEREncodable        parameters;
12     private boolean             parametersDefined = false;
13         
14     public static AlgorithmIdentifier getInstance(
15         ASN1TaggedObject obj,
16         boolean          explicit)
17     {
18         return getInstance(ASN1Sequence.getInstance(obj, explicit));
19     }
20     
21     public static AlgorithmIdentifier getInstance(
22         Object  obj)
23     {
24         if (obj instanceof AlgorithmIdentifier)
25         {
26             return (AlgorithmIdentifier)obj;
27         }
28         
29         if (obj instanceof DERObjectIdentifier)
30         {
31             return new AlgorithmIdentifier((DERObjectIdentifier)obj);
32         }
33
34         if (obj instanceof String)
35         {
36             return new AlgorithmIdentifier((String)obj);
37         }
38
39         if (obj instanceof ASN1Sequence)
40         {
41             return new AlgorithmIdentifier((ASN1Sequence)obj);
42         }
43
44         throw new IllegalArgumentException("unknown object in factory");
45     }
46
47     public AlgorithmIdentifier(
48         DERObjectIdentifier     objectId)
49     {
50         this.objectId = objectId;
51     }
52
53     public AlgorithmIdentifier(
54         String     objectId)
55     {
56         this.objectId = new DERObjectIdentifier(objectId);
57     }
58
59     public AlgorithmIdentifier(
60         DERObjectIdentifier     objectId,
61         DEREncodable            parameters)
62     {
63         parametersDefined = true;
64         this.objectId = objectId;
65         this.parameters = parameters;
66     }
67
68     public AlgorithmIdentifier(
69         ASN1Sequence   seq)
70     {
71         objectId = (DERObjectIdentifier)seq.getObjectAt(0);
72
73         if (seq.size() == 2)
74         {
75             parametersDefined = true;
76             parameters = seq.getObjectAt(1);
77         }
78         else
79         {
80             parameters = null;
81         }
82     }
83
84     public DERObjectIdentifier getObjectId()
85     {
86         return objectId;
87     }
88
89     public DEREncodable getParameters()
90     {
91         return parameters;
92     }
93
94     /**
95      * <pre>
96      *      AlgorithmIdentifier ::= SEQUENCE {
97      *                            algorithm OBJECT IDENTIFIER,
98      *                            parameters ANY DEFINED BY algorithm OPTIONAL }
99      * </pre>
100      */
101     public DERObject getDERObject()
102     {
103         DERConstructedSequence  seq = new DERConstructedSequence();
104
105         seq.addObject(objectId);
106
107         if (parametersDefined)
108         {
109             seq.addObject(parameters);
110         }
111
112         return seq;
113     }
114
115     public boolean equals(
116         Object  o)
117     {
118         if ((o == null) || !(o instanceof AlgorithmIdentifier))
119         {
120             return false;
121         }
122
123         AlgorithmIdentifier other = (AlgorithmIdentifier)o;
124
125         if (!this.getObjectId().equals(other.getObjectId()))
126         {
127             return false;
128         }
129
130         if (this.getParameters() == null && other.getParameters() == null)
131         {
132             return true;
133         }
134
135         if (this.getParameters() == null || other.getParameters() == null)
136         {
137             return false;
138         }
139
140         ByteArrayOutputStream   b1Out = new ByteArrayOutputStream();
141         ByteArrayOutputStream   b2Out = new ByteArrayOutputStream();
142         DEROutputStream         d1Out = new DEROutputStream(b1Out);
143         DEROutputStream         d2Out = new DEROutputStream(b2Out);
144
145         try
146         {
147             d1Out.writeObject(this.getParameters());
148             d2Out.writeObject(other.getParameters());
149
150             byte[]  b1 = b1Out.toByteArray();
151             byte[]  b2 = b2Out.toByteArray();
152
153             if (b1.length != b2.length)
154             {
155                 return false;
156             }
157
158             for (int i = 0; i != b1.length; i++)
159             {
160                 if (b1[i] != b2[i])
161                 {
162                     return false;
163                 }
164             }
165         }
166         catch (Exception e)
167         {
168             return false;
169         }
170
171         return true;
172     }
173 }