2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / DEROctetString.java
diff --git a/src/org/bouncycastle/asn1/DEROctetString.java b/src/org/bouncycastle/asn1/DEROctetString.java
new file mode 100644 (file)
index 0000000..fd9f516
--- /dev/null
@@ -0,0 +1,97 @@
+package org.bouncycastle.asn1;
+
+import java.io.*;
+
+public class DEROctetString
+    extends DERObject
+{
+    byte[]  string;
+
+    /**
+     * @param string the octets making up the octet string.
+     */
+    public DEROctetString(
+        byte[]  string)
+    {
+        this.string = string;
+    }
+
+    public DEROctetString(
+        DERObject  obj)
+    {
+        try
+        {
+            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
+            DEROutputStream         dOut = new DEROutputStream(bOut);
+
+            dOut.writeObject(obj);
+            dOut.close();
+
+            this.string = bOut.toByteArray();
+        }
+        catch (IOException e)
+        {
+            throw new IllegalArgumentException("Error processing object : " + e.toString());
+        }
+    }
+
+    public DEROctetString(
+        DEREncodable  obj)
+    {
+        this(obj.getDERObject());
+    }
+
+    public byte[] getOctets()
+    {
+        return string;
+    }
+
+    void encode(
+        DEROutputStream out)
+        throws IOException
+    {
+        out.writeEncoded(OCTET_STRING, string);
+    }
+
+    public int hashCode()
+    {
+        byte[]  b = this.getOctets();
+        int     value = 0;
+
+        for (int i = 0; i != b.length; i++)
+        {
+            value ^= (b[i] & 0xff) << (i % 4);
+        }
+
+        return value;
+    }
+
+    public boolean equals(
+        Object  o)
+    {
+        if (o == null || !(o instanceof DEROctetString))
+        {
+            return false;
+        }
+
+        DEROctetString  other = (DEROctetString)o;
+
+        if (other.getOctets().length != this.getOctets().length)
+        {
+            return false;
+        }
+
+        byte[]  b1 = other.getOctets();
+        byte[]  b2 = this.getOctets();
+
+        for (int i = 0; i != b1.length; i++)
+        {
+            if (b1[i] != b2[i])
+            {
+                return false;
+            }
+        }
+
+        return true;
+    }
+}