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

package org.bouncycastle.pkcs.jcajce;

import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.GenericKey;
import org.bouncycastle.jcajce.io.MacOutputStream;
import java.io.OutputStream;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import javax.crypto.SecretKey;
import javax.crypto.Mac;
import java.security.Key;
import java.security.spec.KeySpec;
import org.bouncycastle.jcajce.spec.PBKDF2KeySpec;
import org.bouncycastle.util.BigIntegers;
import org.bouncycastle.operator.MacCalculator;
import org.bouncycastle.jcajce.util.NamedJcaJceHelper;
import org.bouncycastle.jcajce.util.ProviderJcaJceHelper;
import java.security.Provider;
import org.bouncycastle.asn1.pkcs.PBMAC1Params;
import org.bouncycastle.jcajce.util.DefaultJcaJceHelper;
import org.bouncycastle.operator.MacAlgorithmIdentifierFinder;
import org.bouncycastle.asn1.pkcs.PBKDF2Params;
import java.security.SecureRandom;
import org.bouncycastle.jcajce.util.JcaJceHelper;
import org.bouncycastle.operator.DefaultMacAlgorithmIdentifierFinder;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;

public class JcePBMac1CalculatorBuilder
{
    public static final AlgorithmIdentifier PRF_SHA224;
    public static final AlgorithmIdentifier PRF_SHA256;
    public static final AlgorithmIdentifier PRF_SHA384;
    public static final AlgorithmIdentifier PRF_SHA512;
    public static final AlgorithmIdentifier PRF_SHA3_224;
    public static final AlgorithmIdentifier PRF_SHA3_256;
    public static final AlgorithmIdentifier PRF_SHA3_384;
    public static final AlgorithmIdentifier PRF_SHA3_512;
    private static final DefaultMacAlgorithmIdentifierFinder defaultFinder;
    private JcaJceHelper helper;
    private AlgorithmIdentifier macAlgorithm;
    private SecureRandom random;
    private int saltLength;
    private int iterationCount;
    private int keySize;
    private PBKDF2Params pbeParams;
    private AlgorithmIdentifier prf;
    private byte[] salt;
    
    public JcePBMac1CalculatorBuilder(final String s, final int n) {
        this(s, n, JcePBMac1CalculatorBuilder.defaultFinder);
    }
    
    public JcePBMac1CalculatorBuilder(final String s, final int keySize, final MacAlgorithmIdentifierFinder macAlgorithmIdentifierFinder) {
        this.helper = new DefaultJcaJceHelper();
        this.saltLength = -1;
        this.iterationCount = 8192;
        this.pbeParams = null;
        this.prf = JcePBMac1CalculatorBuilder.PRF_SHA256;
        this.salt = null;
        this.macAlgorithm = macAlgorithmIdentifierFinder.find(s);
        this.keySize = keySize;
    }
    
    public JcePBMac1CalculatorBuilder(final PBMAC1Params pbmac1Params) {
        this.helper = new DefaultJcaJceHelper();
        this.saltLength = -1;
        this.iterationCount = 8192;
        this.pbeParams = null;
        this.prf = JcePBMac1CalculatorBuilder.PRF_SHA256;
        this.salt = null;
        this.macAlgorithm = pbmac1Params.getMessageAuthScheme();
        this.pbeParams = PBKDF2Params.getInstance(pbmac1Params.getKeyDerivationFunc().getParameters());
    }
    
    public JcePBMac1CalculatorBuilder setProvider(final Provider provider) {
        this.helper = new ProviderJcaJceHelper(provider);
        return this;
    }
    
    public JcePBMac1CalculatorBuilder setProvider(final String s) {
        this.helper = new NamedJcaJceHelper(s);
        return this;
    }
    
    JcePBMac1CalculatorBuilder setHelper(final JcaJceHelper helper) {
        this.helper = helper;
        return this;
    }
    
    public JcePBMac1CalculatorBuilder setIterationCount(final int iterationCount) {
        this.iterationCount = iterationCount;
        return this;
    }
    
    public JcePBMac1CalculatorBuilder setSaltLength(final int saltLength) {
        this.saltLength = saltLength;
        return this;
    }
    
    public JcePBMac1CalculatorBuilder setSalt(final byte[] salt) {
        this.salt = salt;
        return this;
    }
    
    public JcePBMac1CalculatorBuilder setRandom(final SecureRandom random) {
        this.random = random;
        return this;
    }
    
    public JcePBMac1CalculatorBuilder setPrf(final AlgorithmIdentifier prf) {
        this.prf = prf;
        return this;
    }
    
    public MacCalculator build(final char[] array) throws OperatorCreationException {
        if (this.random == null) {
            this.random = new SecureRandom();
        }
        try {
            final Mac mac = this.helper.createMac(this.macAlgorithm.getAlgorithm().getId());
            if (this.pbeParams == null) {
                if (this.salt == null) {
                    if (this.saltLength < 0) {
                        this.saltLength = mac.getMacLength();
                    }
                    this.salt = new byte[this.saltLength];
                    this.random.nextBytes(this.salt);
                }
            }
            else {
                this.salt = this.pbeParams.getSalt();
                this.iterationCount = BigIntegers.intValueExact(this.pbeParams.getIterationCount());
                this.keySize = BigIntegers.intValueExact(this.pbeParams.getKeyLength()) * 8;
                this.prf = this.pbeParams.getPrf();
            }
            final SecretKey generateSecret = this.helper.createSecretKeyFactory("PBKDF2").generateSecret(new PBKDF2KeySpec(array, this.salt, this.iterationCount, this.keySize, this.prf));
            mac.init(generateSecret);
            return new MacCalculator() {
                @Override
                public AlgorithmIdentifier getAlgorithmIdentifier() {
                    return new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBMAC1, new PBMAC1Params(new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBES2, new PBKDF2Params(JcePBMac1CalculatorBuilder.this.salt, JcePBMac1CalculatorBuilder.this.iterationCount, (JcePBMac1CalculatorBuilder.this.keySize + 7) / 8, JcePBMac1CalculatorBuilder.this.prf)), JcePBMac1CalculatorBuilder.this.macAlgorithm));
                }
                
                @Override
                public OutputStream getOutputStream() {
                    return new MacOutputStream(mac);
                }
                
                @Override
                public byte[] getMac() {
                    return mac.doFinal();
                }
                
                @Override
                public GenericKey getKey() {
                    return new GenericKey(this.getAlgorithmIdentifier(), generateSecret.getEncoded());
                }
            };
        }
        catch (final Exception ex) {
            throw new OperatorCreationException("unable to create MAC calculator: " + ex.getMessage(), ex);
        }
    }
    
    static {
        PRF_SHA224 = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_hmacWithSHA224, DERNull.INSTANCE);
        PRF_SHA256 = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_hmacWithSHA256, DERNull.INSTANCE);
        PRF_SHA384 = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_hmacWithSHA384, DERNull.INSTANCE);
        PRF_SHA512 = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_hmacWithSHA512, DERNull.INSTANCE);
        PRF_SHA3_224 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_hmacWithSHA3_224);
        PRF_SHA3_256 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_hmacWithSHA3_256);
        PRF_SHA3_384 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_hmacWithSHA3_384);
        PRF_SHA3_512 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_hmacWithSHA3_512);
        defaultFinder = new DefaultMacAlgorithmIdentifierFinder();
    }
}
