2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / DEROctetString.java
1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4
5 public class DEROctetString
6     extends DERObject
7 {
8     byte[]  string;
9
10     /**
11      * @param string the octets making up the octet string.
12      */
13     public DEROctetString(
14         byte[]  string)
15     {
16         this.string = string;
17     }
18
19     public DEROctetString(
20         DERObject  obj)
21     {
22         try
23         {
24             ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
25             DEROutputStream         dOut = new DEROutputStream(bOut);
26
27             dOut.writeObject(obj);
28             dOut.close();
29
30             this.string = bOut.toByteArray();
31         }
32         catch (IOException e)
33         {
34             throw new IllegalArgumentException("Error processing object : " + e.toString());
35         }
36     }
37
38     public DEROctetString(
39         DEREncodable  obj)
40     {
41         this(obj.getDERObject());
42     }
43
44     public byte[] getOctets()
45     {
46         return string;
47     }
48
49     void encode(
50         DEROutputStream out)
51         throws IOException
52     {
53         out.writeEncoded(OCTET_STRING, string);
54     }
55
56     public int hashCode()
57     {
58         byte[]  b = this.getOctets();
59         int     value = 0;
60
61         for (int i = 0; i != b.length; i++)
62         {
63             value ^= (b[i] & 0xff) << (i % 4);
64         }
65
66         return value;
67     }
68
69     public boolean equals(
70         Object  o)
71     {
72         if (o == null || !(o instanceof DEROctetString))
73         {
74             return false;
75         }
76
77         DEROctetString  other = (DEROctetString)o;
78
79         if (other.getOctets().length != this.getOctets().length)
80         {
81             return false;
82         }
83
84         byte[]  b1 = other.getOctets();
85         byte[]  b2 = this.getOctets();
86
87         for (int i = 0; i != b1.length; i++)
88         {
89             if (b1[i] != b2[i])
90             {
91                 return false;
92             }
93         }
94
95         return true;
96     }
97 }