package org.bouncycastle.openpgp;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.bouncycastle.util.Iterable;
import org.bouncycastle.util.Longs;
import org.bouncycastle.util.Strings;

/* JADX INFO: loaded from: classes2.dex */
public class PGPPublicKeyRingCollection implements Iterable<PGPPublicKeyRing> {
    private List<Long> order;
    private Map<Long, PGPPublicKeyRing> pubRings;

    public PGPPublicKeyRingCollection(InputStream inputStream, KeyFingerPrintCalculator keyFingerPrintCalculator) throws IOException, PGPException {
        this.pubRings = new HashMap();
        this.order = new ArrayList();
        PGPObjectFactory pGPObjectFactory = new PGPObjectFactory(inputStream, keyFingerPrintCalculator);
        while (true) {
            Object objNextObject = pGPObjectFactory.nextObject();
            if (objNextObject == null) {
                return;
            }
            if (!(objNextObject instanceof PGPMarker) && !(objNextObject instanceof PGPPadding)) {
                if (!(objNextObject instanceof PGPPublicKeyRing)) {
                    throw new PGPException(objNextObject.getClass().getName() + " found where PGPPublicKeyRing expected");
                }
                PGPPublicKeyRing pGPPublicKeyRing = (PGPPublicKeyRing) objNextObject;
                Long lValueOf = Longs.valueOf(pGPPublicKeyRing.getPublicKey().getKeyID());
                this.pubRings.put(lValueOf, pGPPublicKeyRing);
                this.order.add(lValueOf);
            }
        }
    }

    public PGPPublicKeyRingCollection(Collection<PGPPublicKeyRing> collection) {
        this.pubRings = new HashMap();
        this.order = new ArrayList();
        for (PGPPublicKeyRing pGPPublicKeyRing : collection) {
            Long lValueOf = Longs.valueOf(pGPPublicKeyRing.getPublicKey().getKeyID());
            this.pubRings.put(lValueOf, pGPPublicKeyRing);
            this.order.add(lValueOf);
        }
    }

    private PGPPublicKeyRingCollection(Map<Long, PGPPublicKeyRing> map, List<Long> list) {
        this.pubRings = new HashMap();
        this.order = new ArrayList();
        this.pubRings = map;
        this.order = list;
    }

    public PGPPublicKeyRingCollection(byte[] bArr, KeyFingerPrintCalculator keyFingerPrintCalculator) throws IOException, PGPException {
        this(new ByteArrayInputStream(bArr), keyFingerPrintCalculator);
    }

    public static PGPPublicKeyRingCollection addPublicKeyRing(PGPPublicKeyRingCollection pGPPublicKeyRingCollection, PGPPublicKeyRing pGPPublicKeyRing) {
        Long lValueOf = Longs.valueOf(pGPPublicKeyRing.getPublicKey().getKeyID());
        if (pGPPublicKeyRingCollection.pubRings.containsKey(lValueOf)) {
            throw new IllegalArgumentException("Collection already contains a key with a keyID for the passed in ring.");
        }
        HashMap map = new HashMap(pGPPublicKeyRingCollection.pubRings);
        ArrayList arrayList = new ArrayList(pGPPublicKeyRingCollection.order);
        map.put(lValueOf, pGPPublicKeyRing);
        arrayList.add(lValueOf);
        return new PGPPublicKeyRingCollection(map, arrayList);
    }

    public static PGPPublicKeyRingCollection removePublicKeyRing(PGPPublicKeyRingCollection pGPPublicKeyRingCollection, PGPPublicKeyRing pGPPublicKeyRing) {
        Long lValueOf = Longs.valueOf(pGPPublicKeyRing.getPublicKey().getKeyID());
        if (!pGPPublicKeyRingCollection.pubRings.containsKey(lValueOf)) {
            throw new IllegalArgumentException("Collection does not contain a key with a keyID for the passed in ring.");
        }
        HashMap map = new HashMap(pGPPublicKeyRingCollection.pubRings);
        ArrayList arrayList = new ArrayList(pGPPublicKeyRingCollection.order);
        map.remove(lValueOf);
        for (int i = 0; i < arrayList.size(); i++) {
            if (((Long) arrayList.get(i)).longValue() == lValueOf.longValue()) {
                arrayList.remove(i);
                break;
            }
        }
        return new PGPPublicKeyRingCollection(map, arrayList);
    }

    public boolean contains(long j) {
        return getPublicKey(j) != null;
    }

    public boolean contains(byte[] bArr) {
        return getPublicKey(bArr) != null;
    }

    public void encode(OutputStream outputStream) throws IOException {
        BCPGOutputStream bCPGOutputStreamWrap = BCPGOutputStream.wrap(outputStream);
        Iterator<Long> it = this.order.iterator();
        while (it.hasNext()) {
            this.pubRings.get(it.next()).encode(bCPGOutputStreamWrap);
        }
    }

    public byte[] getEncoded() throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        encode(byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

    public Iterator<PGPPublicKeyRing> getKeyRings() {
        return this.pubRings.values().iterator();
    }

    public Iterator<PGPPublicKeyRing> getKeyRings(String str) {
        return getKeyRings(str, false, false);
    }

    public Iterator<PGPPublicKeyRing> getKeyRings(String str, boolean z) {
        return getKeyRings(str, z, false);
    }

    public Iterator<PGPPublicKeyRing> getKeyRings(String str, boolean z, boolean z2) {
        Iterator<PGPPublicKeyRing> keyRings = getKeyRings();
        ArrayList arrayList = new ArrayList();
        if (z2) {
            str = Strings.toLowerCase(str);
        }
        while (keyRings.hasNext()) {
            PGPPublicKeyRing next = keyRings.next();
            Iterator<String> userIDs = next.getPublicKey().getUserIDs();
            while (userIDs.hasNext()) {
                String next2 = userIDs.next();
                if (z2) {
                    next2 = Strings.toLowerCase(next2);
                }
                if (z) {
                    if (next2.indexOf(str) >= 0) {
                        arrayList.add(next);
                    }
                } else if (next2.equals(str)) {
                    arrayList.add(next);
                }
            }
        }
        return arrayList.iterator();
    }

    public Iterator<PGPPublicKey> getKeysWithSignaturesBy(long j) {
        ArrayList arrayList = new ArrayList();
        Iterator<PGPPublicKeyRing> it = iterator();
        while (it.hasNext()) {
            Iterator<PGPPublicKey> keysWithSignaturesBy = it.next().getKeysWithSignaturesBy(j);
            while (keysWithSignaturesBy.hasNext()) {
                arrayList.add(keysWithSignaturesBy.next());
            }
        }
        return arrayList.iterator();
    }

    public PGPPublicKey getPublicKey(long j) {
        Iterator<PGPPublicKeyRing> keyRings = getKeyRings();
        while (keyRings.hasNext()) {
            PGPPublicKey publicKey = keyRings.next().getPublicKey(j);
            if (publicKey != null) {
                return publicKey;
            }
        }
        return null;
    }

    public PGPPublicKey getPublicKey(byte[] bArr) {
        Iterator<PGPPublicKeyRing> keyRings = getKeyRings();
        while (keyRings.hasNext()) {
            PGPPublicKey publicKey = keyRings.next().getPublicKey(bArr);
            if (publicKey != null) {
                return publicKey;
            }
        }
        return null;
    }

    public PGPPublicKeyRing getPublicKeyRing(long j) {
        Long lValueOf = Longs.valueOf(j);
        if (this.pubRings.containsKey(lValueOf)) {
            return this.pubRings.get(lValueOf);
        }
        Iterator<PGPPublicKeyRing> keyRings = getKeyRings();
        while (keyRings.hasNext()) {
            PGPPublicKeyRing next = keyRings.next();
            if (next.getPublicKey(j) != null) {
                return next;
            }
        }
        return null;
    }

    public PGPPublicKeyRing getPublicKeyRing(byte[] bArr) {
        Iterator<PGPPublicKeyRing> keyRings = getKeyRings();
        while (keyRings.hasNext()) {
            PGPPublicKeyRing next = keyRings.next();
            if (next.getPublicKey(bArr) != null) {
                return next;
            }
        }
        return null;
    }

    @Override // org.bouncycastle.util.Iterable, java.lang.Iterable
    public Iterator<PGPPublicKeyRing> iterator() {
        return new KeyRingIterator(this.order, this.pubRings);
    }

    public int size() {
        return this.order.size();
    }
}
