package n;

import B0.C0016c;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import com.google.android.gms.internal.measurement.C1;
import com.google.crypto.tink.shaded.protobuf.AbstractC0742h;
import com.google.crypto.tink.shaded.protobuf.C0741g;
import com.google.crypto.tink.shaded.protobuf.C0748n;
import java.io.ByteArrayInputStream;
import java.io.CharConversionException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStoreException;
import java.security.ProviderException;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicMarkableReference;
import l4.C1131c;
import m4.C1172h;
import m4.C1179o;
import q4.C1463c;

/* JADX INFO: loaded from: classes.dex */
public final class U0 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public Object f14793a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public Object f14794b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public Object f14795c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public Object f14796d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public Object f14797e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public Object f14798f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public Object f14799g;

    public U0(String str, C1463c c1463c, C1131c c1131c) {
        this.f14796d = new C0016c(this, false);
        this.f14797e = new C0016c(this, true);
        this.f14798f = new C1179o();
        this.f14799g = new AtomicMarkableReference(null, false);
        this.f14795c = str;
        this.f14793a = new C1172h(c1463c);
        this.f14794b = c1131c;
    }

    public static byte[] c(String str, String str2, Context context) throws CharConversionException {
        if (str == null) {
            throw new IllegalArgumentException("keysetName cannot be null");
        }
        Context applicationContext = context.getApplicationContext();
        try {
            String string = (str2 == null ? PreferenceManager.getDefaultSharedPreferences(applicationContext) : applicationContext.getSharedPreferences(str2, 0)).getString(str, null);
            if (string == null) {
                return null;
            }
            return C1.o(string);
        } catch (ClassCastException | IllegalArgumentException unused) {
            throw new CharConversionException(I1.a.p("can't read keyset; the pref value ", str, " is not a valid hex string"));
        }
    }

    public static H3.f d(byte[] bArr) throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
        try {
            T3.g0 g0VarD = T3.g0.D(byteArrayInputStream, C0748n.a());
            byteArrayInputStream.close();
            return new H3.f(3, (T3.d0) ((T3.g0) A1.y.q(g0VarD).f363r).v());
        } catch (Throwable th) {
            byteArrayInputStream.close();
            throw th;
        }
    }

    public synchronized N3.a a() {
        N3.a aVar;
        try {
            if (((String) this.f14794b) == null) {
                throw new IllegalArgumentException("keysetName cannot be null");
            }
            synchronized (N3.a.f4736b) {
                try {
                    byte[] bArrC = c((String) this.f14794b, (String) this.f14795c, (Context) this.f14793a);
                    if (bArrC == null) {
                        if (((String) this.f14796d) != null) {
                            this.f14797e = f();
                        }
                        this.f14799g = b();
                    } else if (((String) this.f14796d) != null) {
                        this.f14799g = e(bArrC);
                    } else {
                        this.f14799g = d(bArrC);
                    }
                    aVar = new N3.a(this);
                } finally {
                }
            }
        } catch (Throwable th) {
            throw th;
        }
        return aVar;
    }

    public H3.f b() throws GeneralSecurityException, IOException {
        if (((H3.g) this.f14798f) == null) {
            throw new GeneralSecurityException("cannot read or generate keyset");
        }
        H3.f fVar = new H3.f(3, T3.g0.C());
        H3.g gVar = (H3.g) this.f14798f;
        synchronized (fVar) {
            fVar.a(gVar.f3081a);
        }
        fVar.h(H3.o.a((T3.g0) fVar.c().f363r).y().A());
        Context context = (Context) this.f14793a;
        String str = (String) this.f14794b;
        String str2 = (String) this.f14795c;
        if (str == null) {
            throw new IllegalArgumentException("keysetName cannot be null");
        }
        Context applicationContext = context.getApplicationContext();
        SharedPreferences.Editor editorEdit = str2 == null ? PreferenceManager.getDefaultSharedPreferences(applicationContext).edit() : applicationContext.getSharedPreferences(str2, 0).edit();
        if (((N3.b) this.f14797e) != null) {
            A1.y yVarC = fVar.c();
            N3.b bVar = (N3.b) this.f14797e;
            byte[] bArr = new byte[0];
            T3.g0 g0Var = (T3.g0) yVarC.f363r;
            byte[] bArrA = bVar.a(g0Var.e(), bArr);
            try {
                if (!T3.g0.E(bVar.b(bArrA, bArr), C0748n.a()).equals(g0Var)) {
                    throw new GeneralSecurityException("cannot encrypt keyset");
                }
                T3.M mZ = T3.N.z();
                C0741g c0741gC = AbstractC0742h.c(bArrA, 0, bArrA.length);
                mZ.h();
                T3.N.w((T3.N) mZ.f10543r, c0741gC);
                T3.k0 k0VarA = H3.o.a(g0Var);
                mZ.h();
                T3.N.x((T3.N) mZ.f10543r, k0VarA);
                if (!editorEdit.putString(str, C1.r(((T3.N) mZ.d()).e())).commit()) {
                    throw new IOException("Failed to write to SharedPreferences");
                }
            } catch (com.google.crypto.tink.shaded.protobuf.B unused) {
                throw new GeneralSecurityException("invalid keyset, corrupted key material");
            }
        } else if (!editorEdit.putString(str, C1.r(((T3.g0) fVar.c().f363r).e())).commit()) {
            throw new IOException("Failed to write to SharedPreferences");
        }
        return fVar;
    }

    public H3.f e(byte[] bArr) {
        try {
            this.f14797e = new N3.c().c((String) this.f14796d);
            try {
                return new H3.f(3, (T3.d0) ((T3.g0) A1.y.y(new H3.f(1, new ByteArrayInputStream(bArr)), (N3.b) this.f14797e).f363r).v());
            } catch (IOException | GeneralSecurityException e8) {
                try {
                    return d(bArr);
                } catch (IOException unused) {
                    throw e8;
                }
            }
        } catch (GeneralSecurityException | ProviderException e9) {
            try {
                H3.f fVarD = d(bArr);
                Log.w("a", "cannot use Android Keystore, it'll be disabled", e9);
                return fVarD;
            } catch (IOException unused2) {
                throw e9;
            }
        }
    }

    public N3.b f() throws KeyStoreException {
        N3.c cVar = new N3.c();
        try {
            boolean zA = N3.c.a((String) this.f14796d);
            try {
                return cVar.c((String) this.f14796d);
            } catch (GeneralSecurityException | ProviderException e8) {
                if (!zA) {
                    throw new KeyStoreException(I1.a.p("the master key ", (String) this.f14796d, " exists but is unusable"), e8);
                }
                Log.w("a", "cannot use Android Keystore, it'll be disabled", e8);
                return null;
            }
        } catch (GeneralSecurityException | ProviderException e9) {
            Log.w("a", "cannot use Android Keystore, it'll be disabled", e9);
            return null;
        }
    }

    public U0(int i) {
        switch (i) {
            case 2:
                this.f14793a = new AtomicBoolean();
                this.f14794b = null;
                this.f14795c = new HashMap(16, 1.0f);
                this.f14796d = new HashMap(16, 1.0f);
                this.f14797e = new HashMap(16, 1.0f);
                this.f14798f = new HashMap(16, 1.0f);
                this.f14799g = null;
                break;
            default:
                this.f14793a = null;
                this.f14794b = null;
                this.f14795c = null;
                this.f14796d = null;
                this.f14797e = null;
                this.f14798f = null;
                break;
        }
    }
}
