Search in sources :

Example 41 with ASN1GeneralizedTime

use of com.unboundid.asn1.ASN1GeneralizedTime in project attestation by TokenScript.

the class ChequeDecoder method decode.

@Override
public Cheque decode(byte[] encoding) throws IOException {
    ASN1InputStream input = null;
    try {
        input = new ASN1InputStream(encoding);
        ASN1Sequence asn1 = ASN1Sequence.getInstance(input.readObject());
        ASN1Sequence cheque = ASN1Sequence.getInstance(asn1.getObjectAt(0));
        long amount = (ASN1Integer.getInstance(cheque.getObjectAt(0))).getValue().longValueExact();
        ASN1Sequence validity = ASN1Sequence.getInstance(cheque.getObjectAt(1));
        ASN1GeneralizedTime notValidBeforeEnc = ASN1GeneralizedTime.getInstance(validity.getObjectAt(0));
        ASN1GeneralizedTime notValidAfterEnc = ASN1GeneralizedTime.getInstance(validity.getObjectAt(1));
        long notValidBefore, notValidAfter;
        try {
            notValidBefore = notValidBeforeEnc.getDate().getTime();
            notValidAfter = notValidAfterEnc.getDate().getTime();
        } catch (ParseException e) {
            throw ExceptionUtil.throwException(logger, new IOException("Validity is not encoded properly"));
        }
        byte[] commitment = (ASN1OctetString.getInstance(cheque.getObjectAt(2))).getOctets();
        AsymmetricKeyParameter publicKey = SignatureUtility.restoreDefaultKey(DERBitString.getInstance(asn1.getObjectAt(1)).getEncoded());
        // Verify signature
        byte[] signature = DERBitString.getInstance(asn1.getObjectAt(2)).getBytes();
        return new Cheque(commitment, amount, notValidBefore, notValidAfter, signature, publicKey);
    } finally {
        input.close();
    }
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) AsymmetricKeyParameter(org.bouncycastle.crypto.params.AsymmetricKeyParameter) ASN1GeneralizedTime(org.bouncycastle.asn1.ASN1GeneralizedTime) ParseException(java.text.ParseException) IOException(java.io.IOException)

Aggregations

ASN1GeneralizedTime (org.bouncycastle.asn1.ASN1GeneralizedTime)24 ASN1GeneralizedTime (com.unboundid.asn1.ASN1GeneralizedTime)10 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)10 IOException (java.io.IOException)10 Date (java.util.Date)10 ASN1BigInteger (com.unboundid.asn1.ASN1BigInteger)9 ASN1BitString (com.unboundid.asn1.ASN1BitString)9 ASN1Element (com.unboundid.asn1.ASN1Element)9 ASN1Integer (com.unboundid.asn1.ASN1Integer)9 ASN1Null (com.unboundid.asn1.ASN1Null)9 ASN1ObjectIdentifier (com.unboundid.asn1.ASN1ObjectIdentifier)9 DN (com.unboundid.ldap.sdk.DN)9 OID (com.unboundid.util.OID)9 Test (org.testng.annotations.Test)9 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)7 DEROctetString (org.bouncycastle.asn1.DEROctetString)7 ASN1GeneralizedTime (com.github.zhenwei.core.asn1.ASN1GeneralizedTime)6 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)6 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)6 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)5