package ja;

import android.app.Application;
import android.content.SharedPreferences;
import android.security.keystore.KeyGenParameterSpec;
import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.Key;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.util.List;
import javax.crypto.AEADBadTagException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import kotlin.jvm.internal.k;
import kotlin.jvm.internal.t;
import kotlin.text.z;

/* loaded from: classes.dex */
public final class a implements ja.c {

    /* renamed from: c, reason: collision with root package name */
    public static final C0511a f24017c = new C0511a(null);

    /* renamed from: a, reason: collision with root package name */
    private final boolean f24018a;

    /* renamed from: b, reason: collision with root package name */
    private final SharedPreferences f24019b;

    /* renamed from: ja.a$a, reason: collision with other inner class name */
    /* loaded from: classes.dex */
    public static final class C0511a {
        private C0511a() {
        }

        public /* synthetic */ C0511a(k kVar) {
            this();
        }
    }

    /* loaded from: classes.dex */
    public static final class b extends RuntimeException {
        public /* synthetic */ b(String str, int i10, k kVar) {
            this((i10 & 1) != 0 ? "data encrypted using depreciated algorithm encountered" : str);
        }

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public b(String msg) {
            super(msg);
            t.g(msg, "msg");
        }
    }

    /* loaded from: classes.dex */
    public static final class c extends RuntimeException {
        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public c(Throwable e10) {
            super(e10);
            t.g(e10, "e");
        }
    }

    public a(Application application) {
        t.g(application, "application");
        SharedPreferences sharedPreferences = application.getSharedPreferences(application.getPackageName() + "_secured_preferences", 0);
        t.f(sharedPreferences, "getSharedPreferences(...)");
        this.f24019b = sharedPreferences;
    }

    private final byte[] c(String str) {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.reset();
        Charset forName = Charset.forName("UTF-8");
        t.f(forName, "forName(...)");
        byte[] bytes = str.getBytes(forName);
        t.f(bytes, "getBytes(...)");
        byte[] digest = messageDigest.digest(bytes);
        t.f(digest, "digest(...)");
        return digest;
    }

    /* JADX WARN: Multi-variable type inference failed */
    private final String d(String str) {
        boolean U;
        List I0;
        String str2 = null;
        Object[] objArr = 0;
        try {
            U = z.U(str, ":", false, 2, null);
            int i10 = 1;
            if (!U) {
                throw new b(str2, i10, objArr == true ? 1 : 0);
            }
            I0 = z.I0(str, new String[]{":"}, false, 0, 6, null);
            byte[] decode = Base64.decode((String) I0.get(0), 0);
            byte[] decode2 = Base64.decode((String) I0.get(1), 0);
            Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
            cipher.init(2, i("EncryptedSharedPref_KEY_NAME"), new GCMParameterSpec(128, decode2));
            byte[] doFinal = cipher.doFinal(decode);
            t.d(doFinal);
            return new String(doFinal, kotlin.text.d.f26484b);
        } catch (UnsupportedEncodingException e10) {
            throw new c(e10);
        }
    }

    private final String e(String str) {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] bArr = new byte[cipher.getBlockSize()];
        byte[] bytes = "fldsjfodasjifudslfjdsaofshaufihadsf".getBytes(kotlin.text.d.f26484b);
        t.f(bytes, "getBytes(...)");
        System.arraycopy(bytes, 0, bArr, 0, cipher.getBlockSize());
        cipher.init(2, new SecretKeySpec(c("W8K2RYZG4JYHR24FKPMX"), "AES/CBC/PKCS5Padding"), new IvParameterSpec(bArr));
        try {
            byte[] doFinal = cipher.doFinal(Base64.decode(str, 2));
            t.d(doFinal);
            Charset forName = Charset.forName("UTF-8");
            t.f(forName, "forName(...)");
            return new String(doFinal, forName);
        } catch (Exception e10) {
            tr.a.f36618a.d(e10, e10.getMessage(), new Object[0]);
            throw new c(e10);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    private final String f(String str) {
        boolean U;
        List I0;
        String str2 = null;
        Object[] objArr = 0;
        try {
            U = z.U(str, ":", false, 2, null);
            int i10 = 1;
            if (!U) {
                throw new b(str2, i10, objArr == true ? 1 : 0);
            }
            I0 = z.I0(str, new String[]{":"}, false, 0, 6, null);
            byte[] decode = Base64.decode((String) I0.get(0), 0);
            byte[] decode2 = Base64.decode((String) I0.get(1), 0);
            Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
            Charset charset = kotlin.text.d.f26484b;
            byte[] bytes = "Your16ByYour16ByteAESKeyteAESKey".getBytes(charset);
            t.f(bytes, "getBytes(...)");
            cipher.init(2, new SecretKeySpec(bytes, "AES"), new GCMParameterSpec(128, decode2));
            byte[] doFinal = cipher.doFinal(decode);
            t.d(doFinal);
            return new String(doFinal, charset);
        } catch (UnsupportedEncodingException e10) {
            throw new c(e10);
        }
    }

    private final String g(String str) {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(1, i("EncryptedSharedPref_KEY_NAME"));
        byte[] iv = cipher.getIV();
        byte[] bytes = str.getBytes(kotlin.text.d.f26484b);
        t.f(bytes, "getBytes(...)");
        return Base64.encodeToString(cipher.doFinal(bytes), 0) + ":" + Base64.encodeToString(iv, 0);
    }

    private final KeyStore h() {
        KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
        keyStore.load(null);
        t.d(keyStore);
        return keyStore;
    }

    private final SecretKey i(String str) {
        Key key = h().getKey(str, null);
        if (key != null) {
            return (SecretKey) key;
        }
        KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(str, 3);
        builder.setBlockModes("GCM");
        builder.setEncryptionPaddings("NoPadding");
        builder.setKeySize(256);
        KeyGenParameterSpec build = builder.build();
        t.f(build, "build(...)");
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "AndroidKeyStore");
        keyGenerator.init(build);
        SecretKey generateKey = keyGenerator.generateKey();
        t.f(generateKey, "generateKey(...)");
        return generateKey;
    }

    private final void j(String str, String str2) {
        this.f24019b.edit().putString(str, g(str2)).apply();
    }

    private final String k(String str) {
        return this.f24018a ? g(str) : str;
    }

    @Override // ja.c
    public boolean a(String key) {
        t.g(key, "key");
        return this.f24019b.contains(k(key));
    }

    @Override // ja.c
    public void b(String key) {
        t.g(key, "key");
        this.f24019b.edit().remove(k(key)).apply();
    }

    /* JADX WARN: Removed duplicated region for block: B:11:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:8:0x0032  */
    @Override // ja.c
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public String getString(String key, String str) {
        String string;
        String f10;
        String str2;
        t.g(key, "key");
        String k10 = k(key);
        if (!this.f24019b.contains(k10) || (string = this.f24019b.getString(k10, null)) == null) {
            return str;
        }
        try {
            str2 = d(string);
        } catch (b unused) {
            f10 = this.e(string);
            this.putString(key, f10);
            str2 = f10;
            if (str2 == null) {
            }
        } catch (AEADBadTagException unused2) {
            f10 = this.f(string);
            this.putString(key, f10);
            str2 = f10;
            if (str2 == null) {
            }
        }
        return str2 == null ? str : str2;
    }

    @Override // ja.c
    public void putString(String key, String str) {
        t.g(key, "key");
        if (str == null) {
            this.f24019b.edit().remove(k(key)).apply();
        } else {
            j(k(key), str);
        }
    }
}
