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

package org.bouncycastle.pkcs.bc;

import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle.operator.GenericKey;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.io.CipherInputStream;
import java.io.InputStream;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.asn1.pkcs.PKCS12PBEParams;
import org.bouncycastle.operator.InputDecryptor;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.operator.InputDecryptorProvider;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.ExtendedDigest;

public class BcPKCS12PBEInputDecryptorProviderBuilder
{
    private ExtendedDigest digest;
    
    public BcPKCS12PBEInputDecryptorProviderBuilder() {
        this(new SHA1Digest());
    }
    
    public BcPKCS12PBEInputDecryptorProviderBuilder(final ExtendedDigest digest) {
        this.digest = digest;
    }
    
    public InputDecryptorProvider build(final char[] array) {
        return new InputDecryptorProvider() {
            @Override
            public InputDecryptor get(final AlgorithmIdentifier algorithmIdentifier) {
                final PaddedBufferedBlockCipher engine = PKCS12PBEUtils.getEngine(algorithmIdentifier.getAlgorithm());
                engine.init(false, PKCS12PBEUtils.createCipherParameters(algorithmIdentifier.getAlgorithm(), BcPKCS12PBEInputDecryptorProviderBuilder.this.digest, engine.getBlockSize(), PKCS12PBEParams.getInstance(algorithmIdentifier.getParameters()), array));
                return new InputDecryptor() {
                    @Override
                    public AlgorithmIdentifier getAlgorithmIdentifier() {
                        return algorithmIdentifier;
                    }
                    
                    @Override
                    public InputStream getInputStream(final InputStream inputStream) {
                        return new CipherInputStream(inputStream, engine);
                    }
                    
                    public GenericKey getKey() {
                        return new GenericKey(algorithmIdentifier, PBEParametersGenerator.PKCS12PasswordToBytes(array));
                    }
                };
            }
        };
    }
}
