2002/03/21 01:19:32
[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         dOut.write(
33                     Integer.parseInt(tok.nextToken()) * 40
34                     + Integer.parseInt(tok.nextToken()));
35
36         while (tok.hasMoreTokens())
37         {
38             //
39             // translate into base 128
40             //
41             int value = Integer.parseInt(tok.nextToken());
42             int count = iBuf.length - 1;
43             
44             iBuf[count--] = (byte)(value % 128);
45             value /= 128;
46
47             while (value != 0)
48             {
49                 iBuf[count--] = (byte)((value % 128) | 0x80);
50                 value /= 128;
51             }
52             dOut.write(iBuf, count + 1, iBuf.length - (count + 1));
53         }
54
55         dOut.close();
56
57         byte[]  bytes = bOut.toByteArray();
58
59         out.writeEncoded(OBJECT_IDENTIFIER, bytes);
60     }
61
62     public int hashCode()
63     {
64         return identifier.hashCode();
65     }
66
67     public boolean equals(
68         Object  o)
69     {
70         if ((o == null) || !(o instanceof DERObjectIdentifier))
71         {
72             return false;
73         }
74
75         return identifier.equals(((DERObjectIdentifier)o).identifier);
76     }
77 }