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:
021: /**
022: * DER UniversalString object.
023: */
024: public class DERUniversalString extends DERObject implements DERString {
025: private static final char[] table = { '0', '1', '2', '3', '4', '5',
026: '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
027: private byte[] string;
028:
029: /**
030: * return a Universal String from the passed in object.
031: *
032: * @exception IllegalArgumentException if the object cannot be converted.
033: */
034: public static DERUniversalString getInstance(Object obj) {
035: if (obj == null || obj instanceof DERUniversalString) {
036: return (DERUniversalString) obj;
037: }
038:
039: if (obj instanceof ASN1OctetString) {
040: return new DERUniversalString(((ASN1OctetString) obj)
041: .getOctets());
042: }
043:
044: throw new IllegalArgumentException(
045: "illegal object in getInstance: "
046: + obj.getClass().getName());
047: }
048:
049: /**
050: * return a Universal String from a tagged object.
051: *
052: * @param obj the tagged object holding the object we want
053: * @param explicit true if the object is meant to be explicitly
054: * tagged false otherwise.
055: * @exception IllegalArgumentException if the tagged object cannot
056: * be converted.
057: */
058: public static DERUniversalString getInstance(ASN1TaggedObject obj,
059: boolean explicit) {
060: return getInstance(obj.getObject());
061: }
062:
063: /**
064: * basic constructor - byte encoded string.
065: */
066: public DERUniversalString(byte[] string) {
067: this .string = string;
068: }
069:
070: public String getString() {
071: StringBuffer buf = new StringBuffer("#");
072: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
073: ASN1OutputStream aOut = new ASN1OutputStream(bOut);
074:
075: try {
076: aOut.writeObject(this );
077: } catch (IOException e) {
078: throw new RuntimeException(
079: "internal error encoding BitString", e);
080: }
081:
082: byte[] string = bOut.toByteArray();
083:
084: for (int i = 0; i != string.length; i++) {
085: buf.append(table[(string[i] >>> 4) % 0xf]);
086: buf.append(table[string[i] & 0xf]);
087: }
088:
089: return buf.toString();
090: }
091:
092: public byte[] getOctets() {
093: return string;
094: }
095:
096: void encode(DEROutputStream out) throws IOException {
097: out.writeEncoded(UNIVERSAL_STRING, this .getOctets());
098: }
099:
100: public boolean equals(Object o) {
101: if ((o == null) || !(o instanceof DERUniversalString)) {
102: return false;
103: }
104:
105: return this .getString().equals(
106: ((DERUniversalString) o).getString());
107: }
108:
109: public int hashCode() {
110: return this.getString().hashCode();
111: }
112: }
|