1 package org.bouncycastle.asn1;
5 public class DERObjectIdentifier
11 * return an OID from the passed in object
13 * @exception IllegalArgumentException if the object cannot be converted.
15 public static DERObjectIdentifier getInstance(
18 if (obj == null || obj instanceof DERObjectIdentifier)
20 return (DERObjectIdentifier)obj;
23 if (obj instanceof ASN1OctetString)
25 return new DERObjectIdentifier(((ASN1OctetString)obj).getOctets());
28 if (obj instanceof ASN1TaggedObject)
30 return getInstance(((ASN1TaggedObject)obj).getObject());
33 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
37 * return an Object Identifier from a tagged object.
39 * @param obj the tagged object holding the object we want
40 * @param explicit true if the object is meant to be explicitly
41 * tagged false otherwise.
42 * @exception IllegalArgumentException if the tagged object cannot
45 public static DERObjectIdentifier getInstance(
49 return getInstance(obj.getObject());
56 int head = bytes[0] & 0xff;
57 StringBuffer objId = new StringBuffer();
61 for (int i = 0; i != bytes.length; i++)
63 int b = bytes[i] & 0xff;
65 value = value * 128 + (b & 0x7f);
66 if ((b & 0x80) == 0) // end of number reached
87 objId.append(Integer.toString(value));
92 this.identifier = objId.toString();
95 public DERObjectIdentifier(
98 this.identifier = identifier;
101 public String getId()
106 private void writeField(
111 if (fieldValue >= (1 << 7))
113 if (fieldValue >= (1 << 14))
115 if (fieldValue >= (1 << 21))
117 if (fieldValue >= (1 << 28))
119 out.write((fieldValue >> 28) | 0x80);
121 out.write((fieldValue >> 21) | 0x80);
123 out.write((fieldValue >> 14) | 0x80);
125 out.write((fieldValue >> 7) | 0x80);
127 out.write(fieldValue & 0x7f);
134 OIDTokenizer tok = new OIDTokenizer(identifier);
135 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
136 DEROutputStream dOut = new DEROutputStream(bOut);
139 Integer.parseInt(tok.nextToken()) * 40
140 + Integer.parseInt(tok.nextToken()));
142 while (tok.hasMoreTokens())
144 writeField(bOut, Integer.parseInt(tok.nextToken()));
149 byte[] bytes = bOut.toByteArray();
151 out.writeEncoded(OBJECT_IDENTIFIER, bytes);
154 public int hashCode()
156 return identifier.hashCode();
159 public boolean equals(
162 if ((o == null) || !(o instanceof DERObjectIdentifier))
167 return identifier.equals(((DERObjectIdentifier)o).identifier);