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 PGPSecretKeyRingCollection implements Iterable<PGPSecretKeyRing> {
    private List<Long> order;
    private Map<Long, PGPSecretKeyRing> secretRings;

    public PGPSecretKeyRingCollection(InputStream inputStream, KeyFingerPrintCalculator keyFingerPrintCalculator) throws IOException, PGPException {
        this.secretRings = 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 PGPSecretKeyRing)) {
                    throw new PGPException(objNextObject.getClass().getName() + " found where PGPSecretKeyRing expected");
                }
                PGPSecretKeyRing pGPSecretKeyRing = (PGPSecretKeyRing) objNextObject;
                Long lValueOf = Longs.valueOf(pGPSecretKeyRing.getPublicKey().getKeyID());
                this.secretRings.put(lValueOf, pGPSecretKeyRing);
                this.order.add(lValueOf);
            }
        }
    }

    public PGPSecretKeyRingCollection(Collection<PGPSecretKeyRing> collection) {
        this.secretRings = new HashMap();
        this.order = new ArrayList();
        for (PGPSecretKeyRing pGPSecretKeyRing : collection) {
            Long lValueOf = Longs.valueOf(pGPSecretKeyRing.getPublicKey().getKeyID());
            this.secretRings.put(lValueOf, pGPSecretKeyRing);
            this.order.add(lValueOf);
        }
    }

    private PGPSecretKeyRingCollection(Map<Long, PGPSecretKeyRing> map, List<Long> list) {
        this.secretRings = new HashMap();
        this.order = new ArrayList();
        this.secretRings = map;
        this.order = list;
    }

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

    public static PGPSecretKeyRingCollection addSecretKeyRing(PGPSecretKeyRingCollection pGPSecretKeyRingCollection, PGPSecretKeyRing pGPSecretKeyRing) {
        Long lValueOf = Longs.valueOf(pGPSecretKeyRing.getPublicKey().getKeyID());
        if (pGPSecretKeyRingCollection.secretRings.containsKey(lValueOf)) {
            throw new IllegalArgumentException("Collection already contains a key with a keyID for the passed in ring.");
        }
        HashMap map = new HashMap(pGPSecretKeyRingCollection.secretRings);
        ArrayList arrayList = new ArrayList(pGPSecretKeyRingCollection.order);
        map.put(lValueOf, pGPSecretKeyRing);
        arrayList.add(lValueOf);
        return new PGPSecretKeyRingCollection(map, arrayList);
    }

    public static PGPSecretKeyRingCollection removeSecretKeyRing(PGPSecretKeyRingCollection pGPSecretKeyRingCollection, PGPSecretKeyRing pGPSecretKeyRing) {
        Long lValueOf = Longs.valueOf(pGPSecretKeyRing.getPublicKey().getKeyID());
        if (!pGPSecretKeyRingCollection.secretRings.containsKey(lValueOf)) {
            throw new IllegalArgumentException("Collection does not contain a key with a keyID for the passed in ring.");
        }
        HashMap map = new HashMap(pGPSecretKeyRingCollection.secretRings);
        ArrayList arrayList = new ArrayList(pGPSecretKeyRingCollection.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 PGPSecretKeyRingCollection(map, arrayList);
    }

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

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

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

    public Iterator<PGPSecretKeyRing> getKeyRings() {
        return this.secretRings.values().iterator();
    }

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

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

    public Iterator<PGPSecretKeyRing> getKeyRings(String str, boolean z, boolean z2) {
        Iterator<PGPSecretKeyRing> keyRings = getKeyRings();
        ArrayList arrayList = new ArrayList();
        if (z2) {
            str = Strings.toLowerCase(str);
        }
        while (keyRings.hasNext()) {
            PGPSecretKeyRing next = keyRings.next();
            Iterator<String> userIDs = next.getSecretKey().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 PGPSecretKey getSecretKey(long j) {
        Iterator<PGPSecretKeyRing> keyRings = getKeyRings();
        while (keyRings.hasNext()) {
            PGPSecretKey secretKey = keyRings.next().getSecretKey(j);
            if (secretKey != null) {
                return secretKey;
            }
        }
        return null;
    }

    public PGPSecretKeyRing getSecretKeyRing(long j) {
        Long lValueOf = Longs.valueOf(j);
        if (this.secretRings.containsKey(lValueOf)) {
            return this.secretRings.get(lValueOf);
        }
        Iterator<PGPSecretKeyRing> keyRings = getKeyRings();
        while (keyRings.hasNext()) {
            PGPSecretKeyRing next = keyRings.next();
            if (next.getSecretKey(j) != null) {
                return next;
            }
        }
        return null;
    }

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

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