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

package org.bouncycastle.pkcs.bc;

import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.MacCalculator;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import java.io.IOException;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PBKDF2Params;
import org.bouncycastle.asn1.pkcs.PBMAC1Params;
import org.bouncycastle.pkcs.PKCS12MacCalculatorBuilder;

public class BcPKCS12PBMac1CalculatorBuilder implements PKCS12MacCalculatorBuilder
{
    private final PBMAC1Params pbmac1Params;
    private PBKDF2Params pbkdf2Params;
    
    public BcPKCS12PBMac1CalculatorBuilder(final PBMAC1Params pbmac1Params) throws IOException {
        this.pbkdf2Params = null;
        this.pbmac1Params = pbmac1Params;
        if (!PKCSObjectIdentifiers.id_PBKDF2.equals(pbmac1Params.getKeyDerivationFunc().getAlgorithm())) {
            throw new IllegalArgumentException("unrecognised PBKDF");
        }
        this.pbkdf2Params = PBKDF2Params.getInstance(pbmac1Params.getKeyDerivationFunc().getParameters());
        if (this.pbkdf2Params.getKeyLength() == null) {
            throw new IOException("Key length must be present when using PBMAC1.");
        }
    }
    
    @Override
    public AlgorithmIdentifier getDigestAlgorithmIdentifier() {
        return new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBMAC1, this.pbmac1Params);
    }
    
    @Override
    public MacCalculator build(final char[] array) throws OperatorCreationException {
        return PKCS12PBEUtils.createPBMac1Calculator(this.pbmac1Params, this.pbkdf2Params, array);
    }
}
