package org.bouncycastle.bcpg.sig;

import java.util.ArrayList;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public class PreferredAEADCiphersuites extends PreferredAlgorithms {
    private static final Combination AES_128_OCB = new Combination(7, 2);
    private final Combination[] algorithms;

    public static final class Builder {
        private final List<Combination> combinations;
        private final boolean isCritical;

        private Builder(boolean z) {
            this.combinations = new ArrayList();
            this.isCritical = z;
        }

        public Builder addCombination(int i, int i2) {
            this.combinations.add(new Combination(i, i2));
            return this;
        }

        public PreferredAEADCiphersuites build() {
            return new PreferredAEADCiphersuites(this.isCritical, (Combination[]) this.combinations.toArray(new Combination[0]));
        }
    }

    public static class Combination {
        private final int aeadAlgorithm;
        private final int symmetricAlgorithm;

        public Combination(int i, int i2) {
            this.symmetricAlgorithm = i;
            this.aeadAlgorithm = i2;
        }

        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof Combination)) {
                return false;
            }
            Combination combination = (Combination) obj;
            return getSymmetricAlgorithm() == combination.getSymmetricAlgorithm() && getAeadAlgorithm() == combination.getAeadAlgorithm();
        }

        public int getAeadAlgorithm() {
            return this.aeadAlgorithm;
        }

        public int getSymmetricAlgorithm() {
            return this.symmetricAlgorithm;
        }

        public int hashCode() {
            return (getSymmetricAlgorithm() * 13) + (getAeadAlgorithm() * 17);
        }
    }

    public PreferredAEADCiphersuites(boolean z, boolean z2, byte[] bArr) {
        super(39, z, z2, requireEven(bArr));
        this.algorithms = parseCombinations(bArr);
    }

    public PreferredAEADCiphersuites(boolean z, Combination[] combinationArr) {
        this(z, false, encodeCombinations(combinationArr));
    }

    public static PreferredAEADCiphersuites DEFAULT() {
        return new PreferredAEADCiphersuites(false, new Combination[]{AES_128_OCB});
    }

    public static Builder builder(boolean z) {
        return new Builder(z);
    }

    private static boolean contains(Combination combination, Combination[] combinationArr) {
        for (int i = 0; i != combinationArr.length; i++) {
            if (combinationArr[i].equals(combination)) {
                return true;
            }
        }
        return false;
    }

    private static byte[] encodeCombinations(Combination[] combinationArr) {
        byte[] bArr = new byte[combinationArr.length * 2];
        for (int i = 0; i < combinationArr.length; i++) {
            Combination combination = combinationArr[i];
            int i2 = i * 2;
            bArr[i2] = (byte) (combination.getSymmetricAlgorithm() & 255);
            bArr[i2 + 1] = (byte) (combination.getAeadAlgorithm() & 255);
        }
        return bArr;
    }

    private static Combination[] parseCombinations(byte[] bArr) {
        int length = bArr.length / 2;
        Combination[] combinationArr = new Combination[length];
        for (int i = 0; i < length; i++) {
            int i2 = i * 2;
            combinationArr[i] = new Combination(bArr[i2], bArr[i2 + 1]);
        }
        return combinationArr;
    }

    private static byte[] requireEven(byte[] bArr) {
        if (bArr.length % 2 == 0) {
            return bArr;
        }
        throw new IllegalArgumentException("Even number of bytes expected.");
    }

    public Combination[] getAlgorithms() {
        if (contains(AES_128_OCB, this.algorithms)) {
            return getRawAlgorithms();
        }
        Combination[] combinationArr = new Combination[this.algorithms.length + 1];
        System.arraycopy(this.algorithms, 0, combinationArr, 0, this.algorithms.length);
        combinationArr[this.algorithms.length] = AES_128_OCB;
        return combinationArr;
    }

    public Combination[] getRawAlgorithms() {
        Combination[] combinationArr = new Combination[this.algorithms.length];
        System.arraycopy(this.algorithms, 0, combinationArr, 0, this.algorithms.length);
        return combinationArr;
    }

    public boolean isSupported(Combination combination) {
        return contains(combination, getAlgorithms());
    }
}
