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

package org.bouncycastle.crypto.generators;

import org.bouncycastle.crypto.params.DESParameters;
import org.bouncycastle.crypto.params.DESedeParameters;
import org.bouncycastle.crypto.CryptoServiceProperties;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
import org.bouncycastle.crypto.CryptoServicePurpose;
import org.bouncycastle.crypto.KeyGenerationParameters;

public class DESedeKeyGenerator extends DESKeyGenerator
{
    private static final int MAX_IT = 20;
    
    @Override
    public void init(final KeyGenerationParameters keyGenerationParameters) {
        this.random = keyGenerationParameters.getRandom();
        this.strength = (keyGenerationParameters.getStrength() + 7) / 8;
        if (this.strength == 0 || this.strength == 21) {
            this.strength = 24;
        }
        else if (this.strength == 14) {
            this.strength = 16;
        }
        else if (this.strength != 24 && this.strength != 16) {
            throw new IllegalArgumentException("DESede key must be 192 or 128 bits long.");
        }
        CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties("DESedeKeyGen", 112, null, CryptoServicePurpose.KEYGEN));
    }
    
    @Override
    public byte[] generateKey() {
        final byte[] array = new byte[this.strength];
        int n = 0;
        do {
            this.random.nextBytes(array);
            DESParameters.setOddParity(array);
        } while (++n < 20 && (DESedeParameters.isWeakKey(array, 0, array.length) || !DESedeParameters.isRealEDEKey(array, 0)));
        if (DESedeParameters.isWeakKey(array, 0, array.length) || !DESedeParameters.isRealEDEKey(array, 0)) {
            throw new IllegalStateException("Unable to generate DES-EDE key");
        }
        return array;
    }
}
