// Copyright 1997 The Open Group Research Institute.  All rights reserved.

package krb5.lib;

import krb5.lib.asn1.*;

/**
 * PA encrypted TS 
 */
public class PAEncTSEnc {

	/**
	 * Timestampe
	 */
	public KerberosTime pATimeStamp;

	/**
	 * Usec
	 */
	public Integer pAUSec; //optional

	/**
	 * Class constructor
	 *
	 * @param new_pATimeStamp is of type KerberosTime
	 * @param new_pAUSec is of type Integer
	 * @see krb5.lib.KerberosTime
	 */
	public PAEncTSEnc(
		KerberosTime new_pATimeStamp,
		Integer new_pAUSec
		) {
		pATimeStamp = new_pATimeStamp;
		pAUSec = new_pAUSec;
	}

	/**
	 * Class constructor
	 */
	public PAEncTSEnc() {
		KerberosTime now = new KerberosTime(KerberosTime.NOW);
		pATimeStamp = now;
		pAUSec = new Integer(now.getMicroSeconds());
	}

	/**
	 * Class constructor
	 *
	 * @param ref is of type EncodeRef
	 * @exception Asn1Exception an exception
	 * @see krb5.lib.asn1.EncodeRef
	 * @see krb5.lib.Asn1Exception
	 */
	public PAEncTSEnc(EncodeRef ref) throws Asn1Exception {
		EncodeRef subRef = decode.Sequence(ref);
		if (decode.Tag(subRef) == 0) {
			pATimeStamp = new KerberosTime(subRef.startOfData());
			subRef.next();
		}
		else
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
		if (decode.Tag(subRef) == 1) {
			pAUSec = new Integer(decode.Integer(subRef.startOfData()));
			subRef.next();
		}
		if (subRef.isMoreInSequence())
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
	}

	/**
	 * Encodes the object in asn1
	 *
	 * @return byte[] is a result
	 * @exception Asn1Exception an exception
	 * @see krb5.lib.Asn1Exception
	 */
	public byte[] asn1Encode() throws Asn1Exception {
		if (pAUSec != null) {
			byte[][] pa_enc_ts_enc = {
				encode.prependExplicitTag(0, pATimeStamp.asn1Encode()),
				encode.prependExplicitTag(1, encode.Integer(pAUSec.intValue()))
			};
			return encode.Sequence(pa_enc_ts_enc);
		}
		else {
			byte[][] pa_enc_ts_enc = {
				encode.prependExplicitTag(0, pATimeStamp.asn1Encode()),
			};
			return encode.Sequence(pa_enc_ts_enc);
		}
	}

}