package df.root;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.Signature;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import org.bouncycastle.pqc.crypto.xmss.XMSSKeyParameters;

/* JADX INFO: loaded from: classes.dex */
final class DFV2Metadata {
    private DFV2Metadata() {
    }

    static void stage(Context context) {
        try {
            File file = new File(context.getFilesDir(), "android-v2-server-key");
            InputStream inputStreamOpen = context.getAssets().open("dfroot-v2-server-key");
            try {
                Files.copy(inputStreamOpen, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
                if (inputStreamOpen != null) {
                    inputStreamOpen.close();
                }
                String str = "{\"packageName\":\"" + context.getPackageName() + "\",\"appId\":\"" + new String(Files.readAllBytes(new File(context.getFilesDir(), "android-app-id").toPath()), StandardCharsets.US_ASCII).trim() + "\",\"signerSha256\":\"" + signerSha256(context) + "\"}";
                File file2 = new File(context.getFilesDir(), "android-v2-metadata");
                File file3 = new File(context.getFilesDir(), "android-v2-metadata.tmp");
                FileOutputStream fileOutputStream = new FileOutputStream(file3);
                try {
                    fileOutputStream.write(str.getBytes(StandardCharsets.UTF_8));
                    fileOutputStream.getFD().sync();
                    fileOutputStream.close();
                    if (!file3.renameTo(file2)) {
                        throw new IOException("cannot replace v2 metadata");
                    }
                } catch (Throwable th) {
                    try {
                        fileOutputStream.close();
                    } catch (Throwable th2) {
                        th.addSuppressed(th2);
                    }
                    throw th;
                }
            } catch (Throwable th3) {
                if (inputStreamOpen != null) {
                    try {
                        inputStreamOpen.close();
                    } catch (Throwable th4) {
                        th3.addSuppressed(th4);
                    }
                }
                throw th3;
            }
        } catch (Throwable th5) {
            Log.w("dfroot", "v2 metadata unavailable", th5);
        }
    }

    static String signerSha256(Context context) throws Exception {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 134217728);
        if (packageInfo.signingInfo == null) {
            return "unknown";
        }
        Signature[] apkContentsSigners = packageInfo.signingInfo.hasMultipleSigners() ? packageInfo.signingInfo.getApkContentsSigners() : packageInfo.signingInfo.getSigningCertificateHistory();
        if (apkContentsSigners == null || apkContentsSigners.length == 0) {
            return "unknown";
        }
        byte[] bArrDigest = MessageDigest.getInstance(XMSSKeyParameters.SHA_256).digest(apkContentsSigners[0].toByteArray());
        StringBuilder sb = new StringBuilder(64);
        for (byte b : bArrDigest) {
            sb.append(String.format("%02x", Integer.valueOf(b & 255)));
        }
        return sb.toString();
    }
}
