// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.cms;

import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.DigestCalculatorProvider;
import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.asn1.cms.AuthenticatedData;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERSet;
import java.util.Map;
import java.util.Collections;
import java.io.IOException;
import org.bouncycastle.asn1.BEROctetString;
import java.io.OutputStream;
import org.bouncycastle.util.io.TeeOutputStream;
import java.io.ByteArrayOutputStream;
import org.bouncycastle.operator.DigestCalculator;
import org.bouncycastle.operator.MacCalculator;

public class CMSAuthenticatedDataGenerator extends CMSAuthenticatedGenerator
{
    public CMSAuthenticatedData generate(final CMSTypedData cmsTypedData, final MacCalculator macCalculator) throws CMSException {
        return this.generate(cmsTypedData, macCalculator, null);
    }
    
    public CMSAuthenticatedData generate(final CMSTypedData cmsTypedData, final MacCalculator macCalculator, final DigestCalculator digestCalculator) throws CMSException {
        final ASN1EncodableVector recipentInfos = CMSUtils.getRecipentInfos(macCalculator.getKey(), this.recipientInfoGenerators);
        AuthenticatedData authenticatedData;
        if (digestCalculator != null) {
            BEROctetString berOctetString;
            try {
                final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                final TeeOutputStream teeOutputStream = new TeeOutputStream(digestCalculator.getOutputStream(), byteArrayOutputStream);
                cmsTypedData.write(teeOutputStream);
                teeOutputStream.close();
                berOctetString = new BEROctetString(byteArrayOutputStream.toByteArray());
            }
            catch (final IOException ex) {
                throw new CMSException("unable to perform digest calculation: " + ex.getMessage(), ex);
            }
            final Map<Object, Object> unmodifiableMap = Collections.unmodifiableMap((Map<?, ?>)this.getBaseParameters(cmsTypedData.getContentType(), digestCalculator.getAlgorithmIdentifier(), macCalculator.getAlgorithmIdentifier(), digestCalculator.getDigest()));
            if (this.authGen == null) {
                this.authGen = new DefaultAuthenticatedAttributeTableGenerator();
            }
            final DERSet set = new DERSet(this.authGen.getAttributes(unmodifiableMap).toASN1EncodableVector());
            DEROctetString derOctetString;
            try {
                final OutputStream outputStream = macCalculator.getOutputStream();
                outputStream.write(set.getEncoded("DER"));
                outputStream.close();
                derOctetString = new DEROctetString(macCalculator.getMac());
            }
            catch (final IOException ex2) {
                throw new CMSException("unable to perform MAC calculation: " + ex2.getMessage(), ex2);
            }
            authenticatedData = new AuthenticatedData(this.originatorInfo, new DERSet(recipentInfos), macCalculator.getAlgorithmIdentifier(), digestCalculator.getAlgorithmIdentifier(), new ContentInfo(cmsTypedData.getContentType(), berOctetString), set, derOctetString, CMSUtils.getAttrBERSet(this.unauthGen));
        }
        else {
            BEROctetString berOctetString2;
            DEROctetString derOctetString2;
            try {
                final ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
                final TeeOutputStream teeOutputStream2 = new TeeOutputStream(byteArrayOutputStream2, macCalculator.getOutputStream());
                cmsTypedData.write(teeOutputStream2);
                teeOutputStream2.close();
                berOctetString2 = new BEROctetString(byteArrayOutputStream2.toByteArray());
                derOctetString2 = new DEROctetString(macCalculator.getMac());
            }
            catch (final IOException ex3) {
                throw new CMSException("unable to perform MAC calculation: " + ex3.getMessage(), ex3);
            }
            authenticatedData = new AuthenticatedData(this.originatorInfo, new DERSet(recipentInfos), macCalculator.getAlgorithmIdentifier(), null, new ContentInfo(cmsTypedData.getContentType(), berOctetString2), null, derOctetString2, CMSUtils.getAttrBERSet(this.unauthGen));
        }
        return new CMSAuthenticatedData(new ContentInfo(CMSObjectIdentifiers.authenticatedData, authenticatedData), new DigestCalculatorProvider() {
            @Override
            public DigestCalculator get(final AlgorithmIdentifier algorithmIdentifier) throws OperatorCreationException {
                return digestCalculator;
            }
        });
    }
}
