package fk;

import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.util.Iterator;
import mk.a0;
import mk.c0;
import mk.i0;
import mk.y;
import mk.z;

/* loaded from: classes3.dex */
public final class j {

    /* renamed from: a, reason: collision with root package name */
    private final c0.b f18528a;

    private j(c0.b bVar) {
        this.f18528a = bVar;
    }

    private synchronized boolean d(int i10) {
        Iterator it = this.f18528a.w().iterator();
        while (it.hasNext()) {
            if (((c0.c) it.next()).L() == i10) {
                return true;
            }
        }
        return false;
    }

    private synchronized c0.c e(a0 a0Var) {
        y p10;
        int f10;
        i0 K;
        p10 = r.p(a0Var);
        f10 = f();
        K = a0Var.K();
        if (K == i0.UNKNOWN_PREFIX) {
            K = i0.TINK;
        }
        return (c0.c) c0.c.P().t(p10).u(f10).w(z.ENABLED).v(K).i();
    }

    private synchronized int f() {
        int g10;
        g10 = g();
        while (d(g10)) {
            g10 = g();
        }
        return g10;
    }

    private static int g() {
        SecureRandom secureRandom = new SecureRandom();
        byte[] bArr = new byte[4];
        int i10 = 0;
        while (i10 == 0) {
            secureRandom.nextBytes(bArr);
            i10 = ((bArr[0] & Byte.MAX_VALUE) << 24) | ((bArr[1] & 255) << 16) | ((bArr[2] & 255) << 8) | (bArr[3] & 255);
        }
        return i10;
    }

    public static j i() {
        return new j(c0.O());
    }

    public static j j(i iVar) {
        return new j((c0.b) iVar.f().toBuilder());
    }

    public synchronized j a(g gVar) {
        b(gVar.b(), false);
        return this;
    }

    public synchronized int b(a0 a0Var, boolean z10) {
        c0.c e10;
        e10 = e(a0Var);
        this.f18528a.t(e10);
        if (z10) {
            this.f18528a.x(e10.L());
        }
        return e10.L();
    }

    public synchronized i c() {
        return i.e((c0) this.f18528a.i());
    }

    public synchronized j h(int i10) {
        for (int i11 = 0; i11 < this.f18528a.v(); i11++) {
            c0.c u10 = this.f18528a.u(i11);
            if (u10.L() == i10) {
                if (!u10.N().equals(z.ENABLED)) {
                    throw new GeneralSecurityException("cannot set key as primary because it's not enabled: " + i10);
                }
                this.f18528a.x(i10);
            }
        }
        throw new GeneralSecurityException("key not found: " + i10);
        return this;
    }
}
