1 19 20 package com.maverick.crypto.asn1; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 import java.util.Vector ; 26 27 public abstract class ASN1OctetString 28 extends DERObject 29 { 30 byte[] string; 31 32 41 public static ASN1OctetString getInstance( 42 ASN1TaggedObject obj, 43 boolean explicit) 44 { 45 return getInstance(obj.getObject()); 46 } 47 48 54 public static ASN1OctetString getInstance( 55 Object obj) 56 { 57 if (obj == null || obj instanceof ASN1OctetString) 58 { 59 return (ASN1OctetString)obj; 60 } 61 62 if (obj instanceof ASN1TaggedObject) 63 { 64 return getInstance(((ASN1TaggedObject)obj).getObject()); 65 } 66 67 if (obj instanceof ASN1Sequence) 68 { 69 Vector v = new Vector (); 70 Enumeration e = ((ASN1Sequence)obj).getObjects(); 71 72 while (e.hasMoreElements()) 73 { 74 v.addElement(e.nextElement()); 75 } 76 77 return new BERConstructedOctetString(v); 78 } 79 80 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 81 } 82 83 86 public ASN1OctetString( 87 byte[] string) 88 { 89 this.string = string; 90 } 91 92 public ASN1OctetString( 93 DEREncodable obj) 94 { 95 try 96 { 97 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 98 DEROutputStream dOut = new DEROutputStream(bOut); 99 100 dOut.writeObject(obj); 101 dOut.close(); 102 103 this.string = bOut.toByteArray(); 104 } 105 catch (IOException e) 106 { 107 throw new IllegalArgumentException ("Error processing object : " + e.toString()); 108 } 109 } 110 111 public byte[] getOctets() 112 { 113 return string; 114 } 115 116 public int hashCode() 117 { 118 byte[] b = this.getOctets(); 119 int value = 0; 120 121 for (int i = 0; i != b.length; i++) 122 { 123 value ^= (b[i] & 0xff) << (i % 4); 124 } 125 126 return value; 127 } 128 129 public boolean equals( 130 Object o) 131 { 132 if (o == null || !(o instanceof DEROctetString)) 133 { 134 return false; 135 } 136 137 DEROctetString other = (DEROctetString)o; 138 139 byte[] b1 = other.getOctets(); 140 byte[] b2 = this.getOctets(); 141 142 if (b1.length != b2.length) 143 { 144 return false; 145 } 146 147 for (int i = 0; i != b1.length; i++) 148 { 149 if (b1[i] != b2[i]) 150 { 151 return false; 152 } 153 } 154 155 return true; 156 } 157 158 abstract void encode(DEROutputStream out) 159 throws IOException ; 160 } 161 | Popular Tags |