001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.crypto.asn1;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.util.Enumeration;
021: import java.util.Vector;
022:
023: public abstract class ASN1OctetString extends DERObject {
024: byte[] string;
025:
026: /**
027: * return an Octet String from a tagged object.
028: *
029: * @param obj the tagged object holding the object we want.
030: * @param explicit true if the object is meant to be explicitly
031: * tagged false otherwise.
032: * @exception IllegalArgumentException if the tagged object cannot
033: * be converted.
034: */
035: public static ASN1OctetString getInstance(ASN1TaggedObject obj,
036: boolean explicit) {
037: return getInstance(obj.getObject());
038: }
039:
040: /**
041: * return an Octet String from the given object.
042: *
043: * @param obj the object we want converted.
044: * @exception IllegalArgumentException if the object cannot be converted.
045: */
046: public static ASN1OctetString getInstance(Object obj) {
047: if (obj == null || obj instanceof ASN1OctetString) {
048: return (ASN1OctetString) obj;
049: }
050:
051: if (obj instanceof ASN1TaggedObject) {
052: return getInstance(((ASN1TaggedObject) obj).getObject());
053: }
054:
055: if (obj instanceof ASN1Sequence) {
056: Vector v = new Vector();
057: Enumeration e = ((ASN1Sequence) obj).getObjects();
058:
059: while (e.hasMoreElements()) {
060: v.addElement(e.nextElement());
061: }
062:
063: return new BERConstructedOctetString(v);
064: }
065:
066: throw new IllegalArgumentException(
067: "illegal object in getInstance: "
068: + obj.getClass().getName());
069: }
070:
071: /**
072: * @param string the octets making up the octet string.
073: */
074: public ASN1OctetString(byte[] string) {
075: this .string = string;
076: }
077:
078: public ASN1OctetString(DEREncodable obj) {
079: try {
080: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
081: DEROutputStream dOut = new DEROutputStream(bOut);
082:
083: dOut.writeObject(obj);
084: dOut.close();
085:
086: this .string = bOut.toByteArray();
087: } catch (IOException e) {
088: throw new IllegalArgumentException(
089: "Error processing object : " + e.getMessage(), e);
090: }
091: }
092:
093: public byte[] getOctets() {
094: return string;
095: }
096:
097: public int hashCode() {
098: byte[] b = this .getOctets();
099: int value = 0;
100:
101: for (int i = 0; i != b.length; i++) {
102: value ^= (b[i] & 0xff) << (i % 4);
103: }
104:
105: return value;
106: }
107:
108: public boolean equals(Object o) {
109: if (o == null || !(o instanceof DEROctetString)) {
110: return false;
111: }
112:
113: DEROctetString other = (DEROctetString) o;
114:
115: byte[] b1 = other.getOctets();
116: byte[] b2 = this .getOctets();
117:
118: if (b1.length != b2.length) {
119: return false;
120: }
121:
122: for (int i = 0; i != b1.length; i++) {
123: if (b1[i] != b2[i]) {
124: return false;
125: }
126: }
127:
128: return true;
129: }
130:
131: abstract void encode(DEROutputStream out) throws IOException;
132: }
|