package o;

import android.util.Base64;
import com.google.android.gms.common.api.Api;
import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.json.JSONObject;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public abstract class Fh0 {
    public static final C1014dc0 a = new C1014dc0(new C0781b40(11));

    public static byte[] a(byte[] bArr, byte[] bArr2, byte[] bArr3) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
        Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
        AbstractC2461sm0.d(12, bArr3.length);
        cipher.init(2, new SecretKeySpec(bArr2, "AES"), new IvParameterSpec(Arrays.copyOfRange(bArr3, 0, 12)));
        return cipher.doFinal(bArr);
    }

    public static byte[] b(String str) {
        return Base64.decode(str.replace('-', '+').replace('_', '/'), 2);
    }

    public static byte[] c(String str, byte[] bArr) throws IllegalStateException, NoSuchAlgorithmException, InvalidKeyException {
        byte[] bArrE = e(bArr, d());
        byte[] bArrD = d();
        Mac mac = Mac.getInstance("HmacSHA512");
        mac.init(new SecretKeySpec(bArrD, "HmacSHA512"));
        return Arrays.copyOf(e(AbstractC2219q9.U(AbstractC2219q9.U(str.getBytes(AbstractC2355rh.a), new byte[]{1}), Arrays.copyOf(mac.doFinal(bArrE), 32)), bArrE), 32);
    }

    public static byte[] d() {
        return (byte[]) a.getValue();
    }

    public static byte[] e(byte[] bArr, byte[] bArr2) throws NoSuchAlgorithmException, InvalidKeyException {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(bArr2, "HmacSHA256"));
        return mac.doFinal(bArr);
    }

    public static byte[] f(int i) {
        return new byte[]{(byte) ((i >> 24) & 255), (byte) ((i >> 16) & 255), (byte) ((i >> 8) & 255), (byte) (i & 255)};
    }

    public static String g(String str) throws IllegalStateException, NoSuchAlgorithmException, DataFormatException, InvalidKeyException {
        int i = 0;
        List listK0 = Za0.k0(str, new char[]{'.'});
        if (listK0.size() != 6) {
            throw new IllegalArgumentException("Failed requirement.");
        }
        String str2 = (String) listK0.get(0);
        byte[] bArrB = b((String) listK0.get(1));
        byte[] bArrB2 = b((String) listK0.get(2));
        byte[] bArrB3 = b((String) listK0.get(3));
        byte[] bArrB4 = b((String) listK0.get(4));
        byte[] bArrB5 = b((String) listK0.get(5));
        if (!F3.c(str2, "v1")) {
            throw new IllegalArgumentException("Failed requirement.");
        }
        Charset charset = AbstractC2355rh.a;
        JSONObject jSONObject = new JSONObject(new String(bArrB4, charset));
        if (!F3.c(jSONObject.getString("v"), "v1")) {
            throw new IllegalArgumentException("Failed requirement.");
        }
        if (!AbstractC2219q9.T(Arrays.copyOf(MessageDigest.getInstance("SHA-256").digest(AbstractC2219q9.U(bArrB, d())), 8), "", new C2694v90(10), 30).equals(jSONObject.getString("sch"))) {
            throw new IllegalArgumentException("Failed requirement.");
        }
        byte[] bArrC = c("enc-key-2", bArrB);
        MessageDigest.getInstance("SHA-256").digest(AbstractC2219q9.U(bArrB4, bArrB2));
        List listJ0 = Za0.j0(new String(a(a(bArrB5, bArrC, bArrB3), c("enc-key-1", bArrB), bArrB2), charset), new String[]{":"}, 0, 6);
        if (listJ0.size() != 2) {
            throw new IllegalArgumentException("invalid layer5 format");
        }
        String str3 = new String(b((String) listJ0.get(0)), charset);
        if (!F3.c(new JSONObject(new String(b((String) listJ0.get(1)), charset)).getString("chk"), AbstractC2219q9.T(Arrays.copyOf(e(str3.getBytes(charset), AbstractC2219q9.U(d(), bArrB)), 16), "", new C2694v90(11), 30))) {
            throw new IllegalArgumentException("checksum mismatch");
        }
        int length = str3.length();
        byte[] bArr = new byte[length / 2];
        for (int i2 = 0; i2 < length; i2 += 2) {
            bArr[i2 / 2] = (byte) (Character.digit(str3.charAt(i2 + 1), 16) + (Character.digit(str3.charAt(i2), 16) << 4));
        }
        byte[] bArr2 = (byte[]) bArr.clone();
        byte[] bytes = "sbox".getBytes(AbstractC2355rh.a);
        byte[] bArrU = AbstractC2219q9.U(bArrB, d());
        Mac mac = Mac.getInstance("HmacSHA512");
        mac.init(new SecretKeySpec(bArrU, "HmacSHA512"));
        byte[] bArrDoFinal = mac.doFinal(bytes);
        int length2 = bArrDoFinal.length;
        int[] iArr = new int[256];
        for (int i3 = 0; i3 < 256; i3++) {
            iArr[i3] = i3;
        }
        for (int i4 = 255; i4 > 0; i4--) {
            int i5 = ((((bArrDoFinal[i4 % length2] & 255) ^ (bArrDoFinal[(i4 * 13) % length2] & 255)) + (bArrDoFinal[(i4 * 7) % length2] & 255)) + i4) % (i4 + 1);
            int i6 = iArr[i4];
            iArr[i4] = iArr[i5];
            iArr[i5] = i6;
        }
        int[] iArr2 = new int[256];
        for (int i7 = 0; i7 < 256; i7++) {
            iArr2[iArr[i7]] = i7;
        }
        int length3 = bArr2.length;
        for (int i8 = 0; i8 < length3; i8++) {
            bArr2[i8] = (byte) iArr2[bArr2[i8] & 255];
        }
        byte[] bArrE = e("layer-seed".getBytes(AbstractC2355rh.a), AbstractC2219q9.U(bArrB, d()));
        ArrayList arrayList = new ArrayList(bArr2.length);
        for (byte b : bArr2) {
            arrayList.add(Byte.valueOf(b));
        }
        ArrayList arrayList2 = new ArrayList();
        char c = 1;
        int size = arrayList.size() - 1;
        int i9 = 0;
        while (size > 0) {
            i9++;
            byte[] bArrE2 = e(f(i9), bArrE);
            AbstractC2461sm0.d(4, bArrE2.length);
            byte[] bArrCopyOfRange = Arrays.copyOfRange(bArrE2, 0, 4);
            arrayList2.add(new C3001yV(Integer.valueOf(size), Integer.valueOf((((bArrCopyOfRange[3] & 255) | ((((bArrCopyOfRange[0] & 255) << 24) | ((bArrCopyOfRange[c] & 255) << 16)) | ((bArrCopyOfRange[2] & 255) << 8))) & Api.BaseClientBuilder.API_PRIORITY_OTHER) % (size + 1))));
            size--;
            c = 1;
        }
        int size2 = arrayList2.size();
        while (true) {
            size2--;
            if (-1 >= size2) {
                break;
            }
            C3001yV c3001yV = (C3001yV) arrayList2.get(size2);
            int iIntValue = ((Number) c3001yV.l).intValue();
            int iIntValue2 = ((Number) c3001yV.m).intValue();
            byte bByteValue = ((Number) arrayList.get(iIntValue)).byteValue();
            arrayList.set(iIntValue, arrayList.get(iIntValue2));
            arrayList.set(iIntValue2, Byte.valueOf(bByteValue));
        }
        byte[] bArrR0 = AbstractC3117zi.r0(arrayList);
        int length4 = bArrR0.length;
        byte[] bArr3 = new byte[length4];
        int i10 = 0;
        int i11 = 0;
        while (i10 < bArrR0.length) {
            i11++;
            byte[] bArrF = f(i11);
            byte[] bArrE3 = e(bArrF, bArrE);
            byte[] bArrE4 = e(AbstractC2219q9.U(bArrF, "X".getBytes(AbstractC2355rh.a)), bArrE);
            int iMin = Math.min(bArrE3.length, bArrE4.length);
            ArrayList arrayList3 = new ArrayList(iMin);
            for (int i12 = 0; i12 < iMin; i12++) {
                arrayList3.add(Byte.valueOf((byte) (bArrE3[i12] ^ bArrE4[i12])));
            }
            byte[] bArrR02 = AbstractC3117zi.r0(arrayList3);
            int iMin2 = Math.min(bArrR02.length, bArrR0.length - i10);
            for (int i13 = 0; i13 < iMin2; i13++) {
                int i14 = i10 + i13;
                bArr3[i14] = (byte) (bArrR0[i14] ^ bArrR02[i13]);
            }
            i10 += iMin2;
        }
        if (length4 != 0) {
            byte[] bArr4 = new byte[length4];
            int i15 = length4 - 1;
            if (i15 >= 0) {
                while (true) {
                    bArr4[i15 - i] = bArr3[i];
                    if (i == i15) {
                        break;
                    }
                    i++;
                }
            }
            bArr3 = bArr4;
        }
        Inflater inflater = new Inflater(true);
        inflater.setInput(bArr3);
        byte[] bArr5 = new byte[bArr3.length * 10];
        int iInflate = inflater.inflate(bArr5);
        inflater.end();
        return new String(Arrays.copyOf(bArr5, iInflate), AbstractC2355rh.a);
    }
}
