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

package org.bouncycastle.cert.crmf.bc;

import org.bouncycastle.operator.GenericKey;
import org.bouncycastle.crypto.util.CipherFactory;
import java.io.OutputStream;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.cert.crmf.CRMFException;
import org.bouncycastle.operator.OutputEncryptor;
import java.security.SecureRandom;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;

public class BcCRMFEncryptorBuilder
{
    private final ASN1ObjectIdentifier encryptionOID;
    private final int keySize;
    private CRMFHelper helper;
    private SecureRandom random;
    
    public BcCRMFEncryptorBuilder(final ASN1ObjectIdentifier asn1ObjectIdentifier) {
        this(asn1ObjectIdentifier, -1);
    }
    
    public BcCRMFEncryptorBuilder(final ASN1ObjectIdentifier encryptionOID, final int keySize) {
        this.helper = new CRMFHelper();
        this.encryptionOID = encryptionOID;
        this.keySize = keySize;
    }
    
    public BcCRMFEncryptorBuilder setSecureRandom(final SecureRandom random) {
        this.random = random;
        return this;
    }
    
    public OutputEncryptor build() throws CRMFException {
        return new CRMFOutputEncryptor(this.encryptionOID, this.keySize, this.random);
    }
    
    private class CRMFOutputEncryptor implements OutputEncryptor
    {
        private KeyParameter encKey;
        private AlgorithmIdentifier algorithmIdentifier;
        private Object cipher;
        
        CRMFOutputEncryptor(final ASN1ObjectIdentifier asn1ObjectIdentifier, final int n, SecureRandom secureRandom) throws CRMFException {
            secureRandom = CryptoServicesRegistrar.getSecureRandom(secureRandom);
            this.encKey = new KeyParameter(BcCRMFEncryptorBuilder.this.helper.createKeyGenerator(asn1ObjectIdentifier, secureRandom).generateKey());
            this.algorithmIdentifier = BcCRMFEncryptorBuilder.this.helper.generateEncryptionAlgID(asn1ObjectIdentifier, this.encKey, secureRandom);
            BcCRMFEncryptorBuilder.this.helper;
            this.cipher = CRMFHelper.createContentCipher(true, this.encKey, this.algorithmIdentifier);
        }
        
        @Override
        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return this.algorithmIdentifier;
        }
        
        @Override
        public OutputStream getOutputStream(final OutputStream outputStream) {
            return CipherFactory.createOutputStream(outputStream, this.cipher);
        }
        
        @Override
        public GenericKey getKey() {
            return new GenericKey(this.algorithmIdentifier, this.encKey.getKey());
        }
    }
}
