001: package org.bouncycastle.asn1;
002:
003: import org.bouncycastle.util.encoders.Hex;
004:
005: import java.io.ByteArrayInputStream;
006: import java.io.ByteArrayOutputStream;
007: import java.io.IOException;
008: import java.io.InputStream;
009: import java.util.Enumeration;
010: import java.util.Vector;
011:
012: public abstract class ASN1OctetString extends ASN1Object implements
013: ASN1OctetStringParser {
014: byte[] string;
015:
016: /**
017: * return an Octet String from a tagged object.
018: *
019: * @param obj the tagged object holding the object we want.
020: * @param explicit true if the object is meant to be explicitly
021: * tagged false otherwise.
022: * @exception IllegalArgumentException if the tagged object cannot
023: * be converted.
024: */
025: public static ASN1OctetString getInstance(ASN1TaggedObject obj,
026: boolean explicit) {
027: return getInstance(obj.getObject());
028: }
029:
030: /**
031: * return an Octet String from the given object.
032: *
033: * @param obj the object we want converted.
034: * @exception IllegalArgumentException if the object cannot be converted.
035: */
036: public static ASN1OctetString getInstance(Object obj) {
037: if (obj == null || obj instanceof ASN1OctetString) {
038: return (ASN1OctetString) obj;
039: }
040:
041: if (obj instanceof ASN1TaggedObject) {
042: return getInstance(((ASN1TaggedObject) obj).getObject());
043: }
044:
045: if (obj instanceof ASN1Sequence) {
046: Vector v = new Vector();
047: Enumeration e = ((ASN1Sequence) obj).getObjects();
048:
049: while (e.hasMoreElements()) {
050: v.addElement(e.nextElement());
051: }
052:
053: return new BERConstructedOctetString(v);
054: }
055:
056: throw new IllegalArgumentException(
057: "illegal object in getInstance: "
058: + obj.getClass().getName());
059: }
060:
061: /**
062: * @param string the octets making up the octet string.
063: */
064: public ASN1OctetString(byte[] string) {
065: this .string = string;
066: }
067:
068: public ASN1OctetString(DEREncodable obj) {
069: try {
070: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
071: DEROutputStream dOut = new DEROutputStream(bOut);
072:
073: dOut.writeObject(obj);
074: dOut.close();
075:
076: this .string = bOut.toByteArray();
077: } catch (IOException e) {
078: throw new IllegalArgumentException(
079: "Error processing object : " + e.toString());
080: }
081: }
082:
083: public InputStream getOctetStream() {
084: return new ByteArrayInputStream(string);
085: }
086:
087: public ASN1OctetStringParser parser() {
088: return this ;
089: }
090:
091: public byte[] getOctets() {
092: return string;
093: }
094:
095: public int hashCode() {
096: byte[] b = this .getOctets();
097: int value = 0;
098:
099: for (int i = 0; i != b.length; i++) {
100: value ^= (b[i] & 0xff) << (i % 4);
101: }
102:
103: return value;
104: }
105:
106: boolean asn1Equals(DERObject o) {
107: if (!(o instanceof ASN1OctetString)) {
108: return false;
109: }
110:
111: ASN1OctetString other = (ASN1OctetString) o;
112:
113: byte[] b1 = other.string;
114: byte[] b2 = this .string;
115:
116: if (b1.length != b2.length) {
117: return false;
118: }
119:
120: for (int i = 0; i != b1.length; i++) {
121: if (b1[i] != b2[i]) {
122: return false;
123: }
124: }
125:
126: return true;
127: }
128:
129: abstract void encode(DEROutputStream out) throws IOException;
130:
131: public String toString() {
132: return "#" + new String(Hex.encode(string));
133: }
134: }
|