All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.asn1.DERGeneralizedTime Maven / Gradle / Ivy

Go to download

The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 to JDK 1.8.

There is a newer version: 1.79
Show newest version
package org.bouncycastle.asn1;

import java.io.IOException;
import java.util.Date;

import org.bouncycastle.util.Strings;

/**
 * DER Generalized time object.
 * 

11: Restrictions on BER employed by both CER and DER

*

11.7 GeneralizedTime

*

* 11.7.1 The encoding shall terminate with a "Z", * as described in the ITU-T Rec. X.680 | ISO/IEC 8824-1 clause on * GeneralizedTime. *

* 11.7.2 The seconds element shall always be present. *

*

* 11.7.3 The fractional-seconds elements, if present, * shall omit all trailing zeros; if the elements correspond to 0, * they shall be wholly omitted, and the decimal point element also * shall be omitted. */ public class DERGeneralizedTime extends ASN1GeneralizedTime { public DERGeneralizedTime(byte[] time) { super(time); } public DERGeneralizedTime(Date time) { super(time); } public DERGeneralizedTime(String time) { super(time); } private byte[] getDERTime() { if (time[time.length - 1] == 'Z') { if (!hasMinutes()) { byte[] derTime = new byte[time.length + 4]; System.arraycopy(time, 0, derTime, 0, time.length - 1); System.arraycopy(Strings.toByteArray("0000Z"), 0, derTime, time.length - 1, 5); return derTime; } else if (!hasSeconds()) { byte[] derTime = new byte[time.length + 2]; System.arraycopy(time, 0, derTime, 0, time.length - 1); System.arraycopy(Strings.toByteArray("00Z"), 0, derTime, time.length - 1, 3); return derTime; } else if (hasFractionalSeconds()) { int ind = time.length - 2; while (ind > 0 && time[ind] == '0') { ind--; } if (time[ind] == '.') { byte[] derTime = new byte[ind + 1]; System.arraycopy(time, 0, derTime, 0, ind); derTime[ind] = (byte)'Z'; return derTime; } else { byte[] derTime = new byte[ind + 2]; System.arraycopy(time, 0, derTime, 0, ind + 1); derTime[ind + 1] = (byte)'Z'; return derTime; } } else { return time; } } else { return time; // TODO: is there a better way? } } int encodedLength() { int length = getDERTime().length; return 1 + StreamUtil.calculateBodyLength(length) + length; } void encode(ASN1OutputStream out, boolean withTag) throws IOException { out.writeEncoded(withTag, BERTags.GENERALIZED_TIME, getDERTime()); } ASN1Primitive toDERObject() { return this; } ASN1Primitive toDLObject() { return this; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy