package ib;

import android.content.SharedPreferences;
import android.util.Base64;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.stats.CodePackage;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import org.json.JSONException;
import org.json.JSONObject;
import ua.g;
import ua.i;

/* JADX INFO: loaded from: classes2.dex */
public final class c {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final String[] f26606c = {"*", "FCM", CodePackage.GCM, ""};

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final SharedPreferences f26607a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final String f26608b;

    /* JADX WARN: Removed duplicated region for block: B:12:0x003e  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public c(g gVar) {
        gVar.a();
        this.f26607a = gVar.f33401a.getSharedPreferences("com.google.android.gms.appid", 0);
        gVar.a();
        i iVar = gVar.f33403c;
        String gcmSenderId = iVar.getGcmSenderId();
        if (gcmSenderId == null) {
            gVar.a();
            gcmSenderId = iVar.f33416b;
            if (gcmSenderId.startsWith("1:") || gcmSenderId.startsWith("2:")) {
                String[] strArrSplit = gcmSenderId.split(":");
                if (strArrSplit.length != 4) {
                    gcmSenderId = null;
                } else {
                    gcmSenderId = strArrSplit[1];
                    if (gcmSenderId.isEmpty()) {
                    }
                }
            }
        }
        this.f26608b = gcmSenderId;
    }

    @Nullable
    private static String getIdFromPublicKey(@NonNull PublicKey publicKey) {
        try {
            byte[] bArrDigest = MessageDigest.getInstance("SHA1").digest(publicKey.getEncoded());
            bArrDigest[0] = (byte) (((bArrDigest[0] & 15) + 112) & 255);
            return Base64.encodeToString(bArrDigest, 0, 8, 11);
        } catch (NoSuchAlgorithmException unused) {
            return null;
        }
    }

    @Nullable
    private PublicKey parseKey(String str) {
        try {
            return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(Base64.decode(str, 8)));
        } catch (IllegalArgumentException | NoSuchAlgorithmException | InvalidKeySpecException e10) {
            e10.toString();
            return null;
        }
    }

    @Nullable
    private String readInstanceIdFromLocalStorage() {
        String string;
        synchronized (this.f26607a) {
            string = this.f26607a.getString("|S|id", null);
        }
        return string;
    }

    @Nullable
    private String readPublicKeyFromLocalStorageAndCalculateInstanceId() {
        synchronized (this.f26607a) {
            try {
                String string = this.f26607a.getString("|S||P|", null);
                if (string == null) {
                    return null;
                }
                PublicKey key = parseKey(string);
                if (key == null) {
                    return null;
                }
                return getIdFromPublicKey(key);
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Nullable
    public String readIid() {
        synchronized (this.f26607a) {
            try {
                String instanceIdFromLocalStorage = readInstanceIdFromLocalStorage();
                if (instanceIdFromLocalStorage != null) {
                    return instanceIdFromLocalStorage;
                }
                return readPublicKeyFromLocalStorageAndCalculateInstanceId();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Nullable
    public String readToken() {
        synchronized (this.f26607a) {
            try {
                String[] strArr = f26606c;
                int i10 = 0;
                while (true) {
                    String string = null;
                    if (i10 >= 4) {
                        return null;
                    }
                    String str = strArr[i10];
                    String string2 = this.f26607a.getString("|T|" + this.f26608b + "|" + str, null);
                    if (string2 != null && !string2.isEmpty()) {
                        if (string2.startsWith("{")) {
                            try {
                                string = new JSONObject(string2).getString("token");
                            } catch (JSONException unused) {
                            }
                            string2 = string;
                        }
                        return string2;
                    }
                    i10++;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @VisibleForTesting
    public c(@NonNull SharedPreferences sharedPreferences, @Nullable String str) {
        this.f26607a = sharedPreferences;
        this.f26608b = str;
    }
}
