1 19 20 package com.maverick.crypto.asn1; 21 22 import java.io.IOException ; 23 24 public class DERGeneralString 25 extends DERObject implements DERString 26 { 27 private String string; 28 29 public static DERGeneralString getInstance( 30 Object obj) 31 { 32 if (obj == null || obj instanceof DERGeneralString) 33 { 34 return (DERGeneralString) obj; 35 } 36 if (obj instanceof ASN1OctetString) 37 { 38 return new DERGeneralString(((ASN1OctetString) obj).getOctets()); 39 } 40 if (obj instanceof ASN1TaggedObject) 41 { 42 return getInstance(((ASN1TaggedObject) obj).getObject()); 43 } 44 throw new IllegalArgumentException ("illegal object in getInstance: " 45 + obj.getClass().getName()); 46 } 47 48 public static DERGeneralString getInstance( 49 ASN1TaggedObject obj, 50 boolean explicit) 51 { 52 return getInstance(obj.getObject()); 53 } 54 55 public DERGeneralString(byte[] string) 56 { 57 char[] cs = new char[string.length]; 58 for (int i = 0; i != cs.length; i++) { 59 cs[i] = (char) (string[i] & 0xff); 60 } 61 this.string = new String (cs); 62 } 63 64 public DERGeneralString(String string) 65 { 66 this.string = string; 67 } 68 69 public String getString() 70 { 71 return string; 72 } 73 74 public byte[] getOctets() 75 { 76 char[] cs = string.toCharArray(); 77 byte[] bs = new byte[cs.length]; 78 for (int i = 0; i != cs.length; i++) 79 { 80 bs[i] = (byte) cs[i]; 81 } 82 return bs; 83 } 84 85 void encode(DEROutputStream out) 86 throws IOException 87 { 88 out.writeEncoded(GENERAL_STRING, this.getOctets()); 89 } 90 91 public int hashCode() 92 { 93 return this.getString().hashCode(); 94 } 95 96 public boolean equals(Object o) 97 { 98 if (!(o instanceof DERGeneralString)) 99 { 100 return false; 101 } 102 DERGeneralString s = (DERGeneralString) o; 103 return this.getString().equals(s.getString()); 104 } 105 } 106 | Popular Tags |