package com.ifpdos.factorytest;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemProperties;
import android.util.AttributeSet;
import android.util.Log;
import androidx.constraintlayout.core.motion.utils.TypedValues;
import com.android.org.bouncycastle.util.encoders.Hex;
import com.cvte.vman.VmanMiddleWare;
import com.cvte.vman.impl.AtCfgControlManager;
import com.seewo.sdk.SDKConfigureHelper;
import com.seewo.sdk.SDKSystemHelper;
import com.seewo.vcommons.constants.SeewoConstants;
import com.seewo.vcommons.constants.SeewoIntent;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes.dex */
public class RsaTest extends TestItem {
    private static final String TAG = "RsaTest";
    private static final byte[] key = {100, 115, 115, 38, 115, 100, 116, 101};
    private final ExecutorService mExecutor;
    private Handler mHandler;
    private String rsaExistString;
    public String[] urls;

    public RsaTest(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mExecutor = Executors.newFixedThreadPool(3);
        this.urls = new String[]{"https://dssdevops-o.cvte.com/apiso/log?f=sber_bytello_EK65WA-CS3188A-G&m=save_rsa_key&mode=push_new", "https://dssdevops-be-bak.test.seewo.com/apiso/log?f=sber_bytello_EK65WA-CS3188A-G&m=save_rsa_key&mode=push_new"};
        this.mHandler = new Handler(Looper.getMainLooper());
        Log.d(TAG, "RsaTest: rsa:" + SDKSystemHelper.I.executeCommandWithRoot("sdta_jwt_signer_bundle -g"));
        this.rsaExistString = SDKSystemHelper.I.executeCommandWithRoot("sdta_jwt_signer_bundle -e");
        Log.d(TAG, "RsaTest: rsaExistString: " + this.rsaExistString);
        if (!this.rsaExistString.contains("RSA key exists")) {
            rsaTestDone(false, "RSA key not exists");
        } else {
            setStatus("RSA key exists", -16711936);
        }
    }

    @Override // com.ifpdos.factorytest.TestItem
    boolean doTest() {
        this.mExecutor.execute(new Runnable() { // from class: com.ifpdos.factorytest.RsaTest$$ExternalSyntheticLambda0
            @Override // java.lang.Runnable
            public final void run() {
                this.f$0.lambda$doTest$0();
            }
        });
        return false;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$doTest$0() {
        if (!this.rsaExistString.contains("RSA key exists")) {
            rsaTestDone(false, "RSA key not exists");
            return;
        }
        if (!isDeviceConnectedToInternet(this.mContext)) {
            rsaTestDone(false, this.mContext.getString(R.string.test_network_error));
            return;
        }
        try {
            Response responseExecute = new OkHttpClient.Builder().addInterceptor(new RetryInterceptor()).build().newCall(new Request.Builder().url(this.urls[0]).header("Content-Type", "application/json").post((RequestBody) Objects.requireNonNull(getRequestBody())).build()).execute();
            try {
                Log.d(TAG, "doTest: response:" + responseExecute);
                if (responseExecute.isSuccessful()) {
                    rsaTestDone(true, getContext().getString(R.string.test_rsa_pass));
                } else {
                    rsaTestDone(false, getContext().getString(R.string.test_rsa_pass) + "code:" + responseExecute.code());
                }
                if (responseExecute != null) {
                    responseExecute.close();
                }
            } finally {
            }
        } catch (Exception e) {
            Log.e(TAG, "doTest: error", e);
        }
    }

    public void rsaTestDone(final boolean z, final String str) {
        this.mHandler.post(new Runnable() { // from class: com.ifpdos.factorytest.RsaTest.1
            @Override // java.lang.Runnable
            public void run() {
                RsaTest.this.testDone(z, str);
            }
        });
    }

    public static String desEncode(String str) {
        try {
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(1, new SecretKeySpec(key, "DES"));
            return new String(Hex.encode(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8))));
        } catch (Exception e) {
            Log.e(TAG, "desEncode: ", e);
            return "";
        }
    }

    public static String desDecode(String str) {
        try {
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(2, new SecretKeySpec(key, "DES"));
            return new String(cipher.doFinal(Hex.decode(str)), StandardCharsets.UTF_8).trim();
        } catch (Exception e) {
            Log.e(TAG, "desDecode: ", e);
            return "";
        }
    }

    public static RequestBody getRequestBody() {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put(TypedValues.Custom.S_STRING, desEncode(getCustomerData()));
            Log.d(TAG, "getRequestBody: desDecode:" + desDecode(desEncode(getCustomerData())));
            return RequestBody.create(MediaType.parse("application/json"), jSONObject.toString());
        } catch (Exception e) {
            Log.e(TAG, "getRequestBody: error", e);
            return null;
        }
    }

    public static String getCustomerData() {
        try {
            String strExecuteCommandWithRoot = SDKSystemHelper.I.executeCommandWithRoot("sdta_jwt_signer_bundle -g");
            if (strExecuteCommandWithRoot.contains("inaccessible or not found")) {
                Log.d(TAG, "getCustomerData: rsa = " + strExecuteCommandWithRoot);
                strExecuteCommandWithRoot = "";
            }
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("customer", SystemProperties.get(SeewoConstants.SYSTEM_PROPERTY_FLAVOR_NAME, "null"));
            jSONObject.put("modelId", SystemProperties.get(SeewoConstants.SYSTEM_PROPERTY_CVT_PRODUCT_MODEL_PERSIST, "null"));
            jSONObject.put(SeewoIntent.KEY_MACHINE_SN, SDKConfigureHelper.I.getMachineSN());
            jSONObject.put("mac", VmanMiddleWare.getInstance().getAtCfgControl().getString(AtCfgControlManager.IDX_BOARD_SN));
            jSONObject.put("rsa", strExecuteCommandWithRoot);
            return jSONObject.toString();
        } catch (Exception e) {
            Log.e(TAG, "getCustomerData: ", e);
            return "";
        }
    }

    public class RetryInterceptor implements Interceptor {
        private int retryNum = 0;

        public RetryInterceptor() {
        }

        @Override // okhttp3.Interceptor
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request request = chain.request();
            Response responseProceed = chain.proceed(request);
            while (!responseProceed.isSuccessful() && this.retryNum < 6) {
                responseProceed.close();
                this.retryNum++;
                String str = RsaTest.this.urls[this.retryNum % RsaTest.this.urls.length];
                Request requestBuild = request.newBuilder().url(str).build();
                Log.d(RsaTest.TAG, "Retry: " + this.retryNum);
                Log.d(RsaTest.TAG, "URL: " + str);
                responseProceed = chain.proceed(requestBuild);
            }
            return responseProceed;
        }
    }

    public boolean isDeviceConnectedToInternet(Context context) {
        ConnectivityManager connectivityManager;
        NetworkInfo activeNetworkInfo;
        return (context == null || (connectivityManager = (ConnectivityManager) context.getSystemService("connectivity")) == null || (activeNetworkInfo = connectivityManager.getActiveNetworkInfo()) == null || !activeNetworkInfo.isConnected()) ? false : true;
    }
}
