package com.binance.connector.client.utils.signaturegenerator;

import com.binance.connector.client.utils.ParameterChecker;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import javax.crypto.EncryptedPrivateKeyInfo;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

/* loaded from: binance-connector-java-3.0.0rc3.jar:com/binance/connector/client/utils/signaturegenerator/RsaSignatureGenerator.class */
public final class RsaSignatureGenerator implements SignatureGenerator {
    private static final String RSA_SHA256 = "SHA256withRSA";
    private static final String RSA = "RSA";
    private RSAPrivateKey privateKey;

    public RsaSignatureGenerator(String privateKey) {
        this(privateKey, null);
    }

    public RsaSignatureGenerator(String privateKey, String password) {
        ParameterChecker.checkParameterType(privateKey, String.class, "privateKey");
        try {
            this.privateKey = parsePrivateKey(new File(privateKey).exists() ? new String(Files.readAllBytes(Paths.get(privateKey, new String[0]))) : privateKey, password);
        } catch (IOException e) {
            throw new RuntimeException("Unable to find/read private key at given file path", e);
        } catch (Exception e2) {
            throw new RuntimeException("Failed to parse RSA private key", e2);
        }
    }

    @Override // com.binance.connector.client.utils.signaturegenerator.SignatureGenerator
    public String getSignature(String data) {
        try {
            Signature privateSignature = Signature.getInstance(RSA_SHA256);
            privateSignature.initSign(this.privateKey);
            privateSignature.update(data.getBytes());
            return Base64.getEncoder().encodeToString(privateSignature.sign());
        } catch (Exception e) {
            throw new RuntimeException("Failed to calculate rsa-sha256", e);
        }
    }

    private RSAPrivateKey parsePrivateKey(String privateKeyPem, String password) throws Exception {
        String parsedPem;
        PKCS8EncodedKeySpec encodedKeySpec;
        String parsedPem2 = privateKeyPem.replace("\n", "").trim();
        if (password != null) {
            parsedPem = parsedPem2.replace("-----BEGIN ENCRYPTED PRIVATE KEY-----", "").replace("-----END ENCRYPTED PRIVATE KEY-----", "");
        } else {
            parsedPem = parsedPem2.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
        }
        byte[] encoded = Base64.getDecoder().decode(parsedPem);
        if (password != null) {
            EncryptedPrivateKeyInfo pkInfo = new EncryptedPrivateKeyInfo(encoded);
            encodedKeySpec = pkInfo.getKeySpec(SecretKeyFactory.getInstance(pkInfo.getAlgName()).generateSecret(new PBEKeySpec(password.toCharArray())));
        } else {
            encodedKeySpec = new PKCS8EncodedKeySpec(encoded);
        }
        return (RSAPrivateKey) KeyFactory.getInstance(RSA).generatePrivate(encodedKeySpec);
    }
}
