package com.bytedance.sdk.component.utils;

import android.os.Build;
import android.text.TextUtils;
import com.bytedance.sdk.component.pglcrypt.PglCryptUtils;
import defpackage.e;
import java.security.SecureRandom;
import java.util.Random;
import org.json.JSONObject;

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

    /* JADX INFO: renamed from: com.bytedance.sdk.component.utils.fm$fm, reason: collision with other inner class name */
    public static class C0040fm {
        static final Random fm = fm.lb();
    }

    public static JSONObject fm(JSONObject jSONObject) {
        return jSONObject == null ? new JSONObject() : fm(jSONObject.toString());
    }

    public static String lb(String str) {
        if (TextUtils.isEmpty(str) || str.length() < 49) {
            return str;
        }
        String strFm = fm(str.substring(1, 33), 32);
        String strSubstring = str.substring(33, 49);
        return (strSubstring == null || strFm == null) ? str : com.bytedance.sdk.component.yz.fm.ro(str.substring(49), strSubstring, strFm);
    }

    public static String ro(String str) {
        if (TextUtils.isEmpty(str)) {
            return str;
        }
        String strFm = fm();
        String strFm2 = fm(strFm, 32);
        String strRo = ro();
        return e.k("3", strFm, strRo, (strFm2 == null || strRo == null) ? null : com.bytedance.sdk.component.yz.fm.fm(str, strRo, strFm2));
    }

    public static JSONObject fm(String str) {
        JSONObject jSONObject = new JSONObject();
        if (TextUtils.isEmpty(str)) {
            return jSONObject;
        }
        try {
            try {
                String strRo = ro(str);
                if (!TextUtils.isEmpty(strRo)) {
                    jSONObject.put(PglCryptUtils.KEY_MESSAGE, strRo);
                    jSONObject.put("cypher", 3);
                } else {
                    jSONObject.put(PglCryptUtils.KEY_MESSAGE, str);
                    jSONObject.put("cypher", 0);
                }
            } catch (Throwable th) {
                th.getMessage();
            }
        } catch (Throwable unused) {
            jSONObject.put(PglCryptUtils.KEY_MESSAGE, str);
            jSONObject.put("cypher", 0);
        }
        return jSONObject;
    }

    public static Random lb() {
        if (Build.VERSION.SDK_INT >= 26) {
            try {
                return SecureRandom.getInstanceStrong();
            } catch (Throwable unused) {
                return new SecureRandom();
            }
        }
        return new SecureRandom();
    }

    public static String ro() {
        String strFm = fm(8);
        if (strFm == null || strFm.length() != 16) {
            return null;
        }
        return strFm;
    }

    public static String fm() {
        String strFm = fm(16);
        if (strFm == null || strFm.length() != 32) {
            return null;
        }
        return strFm;
    }

    public static String fm(String str, int i10) {
        if (str == null || str.length() != i10) {
            return null;
        }
        int i11 = i10 / 2;
        return str.substring(i11, i10) + str.substring(0, i11);
    }

    public static String fm(int i10) {
        try {
            byte[] bArr = new byte[i10];
            C0040fm.fm.nextBytes(bArr);
            return jnr.fm(bArr);
        } catch (Exception unused) {
            return null;
        }
    }
}
