1 19 20 package com.maverick.crypto.asn1; 21 22 import java.io.IOException ; 23 import java.text.ParseException ; 24 import java.text.SimpleDateFormat ; 25 import java.util.Date ; 26 import java.util.SimpleTimeZone ; 27 28 31 public class DERGeneralizedTime 32 extends DERObject 33 { 34 String time; 35 36 41 public static DERGeneralizedTime getInstance( 42 Object obj) 43 { 44 if (obj == null || obj instanceof DERGeneralizedTime) 45 { 46 return (DERGeneralizedTime)obj; 47 } 48 49 if (obj instanceof ASN1OctetString) 50 { 51 return new DERGeneralizedTime(((ASN1OctetString)obj).getOctets()); 52 } 53 54 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 55 } 56 57 66 public static DERGeneralizedTime getInstance( 67 ASN1TaggedObject obj, 68 boolean explicit) 69 { 70 return getInstance(obj.getObject()); 71 } 72 73 81 public DERGeneralizedTime( 82 String time) 83 { 84 this.time = time; 85 } 86 87 90 public DERGeneralizedTime( 91 Date time) 92 { 93 SimpleDateFormat dateF = new SimpleDateFormat ("yyyyMMddHHmmss'Z'"); 94 95 dateF.setTimeZone(new SimpleTimeZone (0,"Z")); 96 97 this.time = dateF.format(time); 98 } 99 100 DERGeneralizedTime( 101 byte[] bytes) 102 { 103 char[] dateC = new char[bytes.length]; 107 108 for (int i = 0; i != dateC.length; i++) 109 { 110 dateC[i] = (char)(bytes[i] & 0xff); 111 } 112 113 this.time = new String (dateC); 114 } 115 116 128 public String getTime() 129 { 130 if (time.charAt(time.length() - 1) == 'Z') 134 { 135 return time.substring(0, time.length() - 1) + "GMT+00:00"; 136 } 137 else 138 { 139 int signPos = time.length() - 5; 140 char sign = time.charAt(signPos); 141 if (sign == '-' || sign == '+') 142 { 143 return time.substring(0, signPos) 144 + "GMT" 145 + time.substring(signPos, signPos + 3) 146 + ":" 147 + time.substring(signPos + 3); 148 } 149 else 150 { 151 signPos = time.length() - 3; 152 sign = time.charAt(signPos); 153 if (sign == '-' || sign == '+') 154 { 155 return time.substring(0, signPos) 156 + "GMT" 157 + time.substring(signPos) 158 + ":00"; 159 } 160 } 161 } 162 163 return time; 164 } 165 166 public Date getDate() 167 throws ParseException 168 { 169 SimpleDateFormat dateF = new SimpleDateFormat ("yyyyMMddHHmmss'Z'"); 170 171 dateF.setTimeZone(new SimpleTimeZone (0, "Z")); 172 173 return dateF.parse(time); 174 } 175 176 private byte[] getOctets() 177 { 178 char[] cs = time.toCharArray(); 179 byte[] bs = new byte[cs.length]; 180 181 for (int i = 0; i != cs.length; i++) 182 { 183 bs[i] = (byte)cs[i]; 184 } 185 186 return bs; 187 } 188 189 190 void encode( 191 DEROutputStream out) 192 throws IOException 193 { 194 out.writeEncoded(GENERALIZED_TIME, this.getOctets()); 195 } 196 197 public boolean equals( 198 Object o) 199 { 200 if ((o == null) || !(o instanceof DERGeneralizedTime)) 201 { 202 return false; 203 } 204 205 return time.equals(((DERGeneralizedTime)o).time); 206 } 207 } 208 | Popular Tags |