initial checkin
[org.ibex.nanogoat.git] / src / org / bouncycastle / asn1 / ASN1OctetString.java
1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4 import java.util.*;
5
6 public abstract class ASN1OctetString
7     extends DERObject
8 {
9     byte[]  string;
10
11     /**
12      * return an Octet String from a tagged object.
13      *
14      * @param obj the tagged object holding the object we want.
15      * @param explicit true if the object is meant to be explicitly
16      *              tagged false otherwise.
17      * @exception IllegalArgumentException if the tagged object cannot
18      *              be converted.
19      */
20     public static ASN1OctetString getInstance(
21         ASN1TaggedObject    obj,
22         boolean             explicit)
23     {
24         return getInstance(obj.getObject());
25     }
26         
27     /**
28      * return an Octet String from the given object.
29      *
30      * @param obj the object we want converted.
31      * @exception IllegalArgumentException if the object cannot be converted.
32      */
33     public static ASN1OctetString getInstance(
34         Object  obj)
35     {
36         if (obj == null || obj instanceof ASN1OctetString)
37         {
38             return (ASN1OctetString)obj;
39         }
40
41         if (obj instanceof ASN1TaggedObject)
42         {
43             return getInstance(((ASN1TaggedObject)obj).getObject());
44         }
45
46         if (obj instanceof ASN1Sequence)
47         {
48             Vector      v = new Vector();
49             Enumeration e = ((ASN1Sequence)obj).getObjects();
50
51             while (e.hasMoreElements())
52             {
53                 v.addElement(e.nextElement());
54             }
55
56             return new BERConstructedOctetString(v);
57         }
58
59         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
60     }
61
62     /**
63      * @param string the octets making up the octet string.
64      */
65     public ASN1OctetString(
66         byte[]  string)
67     {
68         this.string = string;
69     }
70
71     public ASN1OctetString(
72         DEREncodable obj)
73     {
74         try
75         {
76             ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
77             DEROutputStream         dOut = new DEROutputStream(bOut);
78
79             dOut.writeObject(obj);
80             dOut.close();
81
82             this.string = bOut.toByteArray();
83         }
84         catch (IOException e)
85         {
86             throw new IllegalArgumentException("Error processing object : " + e.toString());
87         }
88     }
89
90     public byte[] getOctets()
91     {
92         return string;
93     }
94
95     public int hashCode()
96     {
97         byte[]  b = this.getOctets();
98         int     value = 0;
99
100         for (int i = 0; i != b.length; i++)
101         {
102             value ^= (b[i] & 0xff) << (i % 4);
103         }
104
105         return value;
106     }
107
108     public boolean equals(
109         Object  o)
110     {
111         if (o == null || !(o instanceof DEROctetString))
112         {
113             return false;
114         }
115
116         DEROctetString  other = (DEROctetString)o;
117
118         byte[] b1 = other.getOctets();
119         byte[] b2 = this.getOctets();
120
121         if (b1.length != b2.length)
122         {
123             return false;
124         }
125
126         for (int i = 0; i != b1.length; i++)
127         {
128             if (b1[i] != b2[i])
129             {
130                 return false;
131             }
132         }
133
134         return true;
135     }
136
137     abstract void encode(DEROutputStream out)
138         throws IOException;
139 }