package org.bouncycastle.openpgp.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes3.dex */
public interface KeyPassphraseProvider {

    /* JADX INFO: loaded from: classes.dex */
    public static class DefaultKeyPassphraseProvider implements KeyPassphraseProvider {
        private KeyPassphraseProvider callback;
        private final Map<OpenPGPKey.OpenPGPSecretKey, char[]> passphraseMap = new HashMap();
        private final List<char[]> allPassphrases = new ArrayList();

        public DefaultKeyPassphraseProvider() {
        }

        public DefaultKeyPassphraseProvider(OpenPGPKey openPGPKey, char[] cArr) {
            this.allPassphrases.add(cArr);
            Iterator<OpenPGPKey.OpenPGPSecretKey> it = openPGPKey.getSecretKeys().values().iterator();
            while (it.hasNext()) {
                this.passphraseMap.put(it.next(), cArr);
            }
        }

        public DefaultKeyPassphraseProvider addPassphrase(OpenPGPKey.OpenPGPSecretKey openPGPSecretKey, char[] cArr) {
            boolean zIsLocked = openPGPSecretKey.isLocked();
            Map<OpenPGPKey.OpenPGPSecretKey, char[]> map = this.passphraseMap;
            if (zIsLocked) {
                map.put(openPGPSecretKey, cArr);
                return this;
            }
            map.put(openPGPSecretKey, null);
            return this;
        }

        public DefaultKeyPassphraseProvider addPassphrase(OpenPGPKey openPGPKey, char[] cArr) {
            for (OpenPGPKey.OpenPGPSecretKey openPGPSecretKey : openPGPKey.getSecretKeys().values()) {
                boolean zIsLocked = openPGPSecretKey.isLocked();
                Map<OpenPGPKey.OpenPGPSecretKey, char[]> map = this.passphraseMap;
                if (zIsLocked) {
                    char[] cArr2 = map.get(openPGPSecretKey);
                    if (cArr2 == null || !openPGPSecretKey.isPassphraseCorrect(cArr2)) {
                        this.passphraseMap.put(openPGPSecretKey, cArr);
                    }
                } else {
                    map.put(openPGPSecretKey, null);
                }
            }
            return this;
        }

        public DefaultKeyPassphraseProvider addPassphrase(char[] cArr) {
            Iterator<char[]> it = this.allPassphrases.iterator();
            boolean zAreEqual = false;
            while (it.hasNext()) {
                zAreEqual |= Arrays.areEqual(it.next(), cArr);
            }
            if (!zAreEqual) {
                this.allPassphrases.add(cArr);
            }
            return this;
        }

        @Override // org.bouncycastle.openpgp.api.KeyPassphraseProvider
        public char[] getKeyPassword(OpenPGPKey.OpenPGPSecretKey openPGPSecretKey) {
            boolean zIsLocked = openPGPSecretKey.isLocked();
            Map<OpenPGPKey.OpenPGPSecretKey, char[]> map = this.passphraseMap;
            if (!zIsLocked) {
                map.put(openPGPSecretKey, null);
                return null;
            }
            char[] cArr = map.get(openPGPSecretKey);
            if (cArr != null) {
                return cArr;
            }
            for (char[] cArr2 : this.allPassphrases) {
                if (openPGPSecretKey.isPassphraseCorrect(cArr2)) {
                    addPassphrase(openPGPSecretKey, cArr2);
                    return cArr2;
                }
            }
            if (this.callback == null) {
                return cArr;
            }
            char[] keyPassword = this.callback.getKeyPassword(openPGPSecretKey);
            addPassphrase(openPGPSecretKey, keyPassword);
            return keyPassword;
        }

        public DefaultKeyPassphraseProvider setMissingPassphraseCallback(KeyPassphraseProvider keyPassphraseProvider) {
            this.callback = keyPassphraseProvider;
            return this;
        }
    }

    char[] getKeyPassword(OpenPGPKey.OpenPGPSecretKey openPGPSecretKey);
}
