8f044546241842dedf02f02b084a59fcfaa05657
[org.ibex.core.git] / src / org / bouncycastle / asn1 / DERObjectIdentifier.java
1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4
5 public class DERObjectIdentifier
6     extends DERObject
7 {
8     String      identifier;
9
10     public DERObjectIdentifier(
11         String  identifier)
12     {
13         this.identifier = identifier;
14     }
15
16     public String getId()
17     {
18         return identifier;
19     }
20
21     void encode(
22         DEROutputStream out)
23         throws IOException
24     {
25         OIDTokenizer            tok = new OIDTokenizer(identifier);
26         ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
27         DEROutputStream         dOut = new DEROutputStream(bOut);
28
29                                     // space for 5 7 bit numbers in an int
30         byte[]                  iBuf = new byte[5];    
31         
32         // FIXED by Adam Megacz -- GCJ doesn't handle evaluation order properly
33         String t1 = tok.nextToken();
34         String t2 = tok.nextToken();
35         dOut.write(Integer.parseInt(t1) * 40 + Integer.parseInt(t2));
36
37         while (tok.hasMoreTokens())
38         {
39             //
40             // translate into base 128
41             //
42             int value = Integer.parseInt(tok.nextToken());
43             int count = iBuf.length - 1;
44             
45             iBuf[count--] = (byte)(value % 128);
46             value /= 128;
47
48             while (value != 0)
49             {
50                 iBuf[count--] = (byte)((value % 128) | 0x80);
51                 value /= 128;
52             }
53             dOut.write(iBuf, count + 1, iBuf.length - (count + 1));
54         }
55
56         dOut.close();
57
58         byte[]  bytes = bOut.toByteArray();
59
60         out.writeEncoded(OBJECT_IDENTIFIER, bytes);
61     }
62
63     public int hashCode()
64     {
65         return identifier.hashCode();
66     }
67
68     public boolean equals(
69         Object  o)
70     {
71         if ((o == null) || !(o instanceof DERObjectIdentifier))
72         {
73             return false;
74         }
75
76         return identifier.equals(((DERObjectIdentifier)o).identifier);
77     }
78 }