// use Apoloz\AlfaBank\Contracts\SignerInterface; use Apoloz\AlfaBank\Exceptions\SignerException; // //
// class OpenSslCmsSigner implements SignerInterface { //
// public function __construct( protected string $certificatePath, protected string $privateKeyPath ) {} //
//
// public function sign(string $data): string { $privateKey = openssl_pkey_get_private(file_get_contents($this->privateKeyPath)); // assuming no passphrase for simplicity here if (!$privateKey) { throw new SignerException("Could not read private key for signing from {$this->privateKeyPath}."); } $cert = openssl_x509_read(file_get_contents($this->certificatePath)); if (!$cert) { throw new SignerException("Could not read certificate for signing from {$this->certificatePath}."); } $inFile = tempnam(sys_get_temp_dir(), 'alfa_in'); $outFile = tempnam(sys_get_temp_dir(), 'alfa_out'); file_put_contents($inFile, $data); $status = openssl_pkcs7_sign( $inFile, $outFile, $cert, $privateKey, [], PKCS7_BINARY | PKCS7_DETACHED ); if (!$status) { if (file_exists($inFile)) unlink($inFile); if (file_exists($outFile)) unlink($outFile); throw new SignerException("Signing failed: " . openssl_error_string()); } $signedData = file_get_contents($outFile); unlink($inFile); unlink($outFile); $parts = explode("\n\n", $signedData); $signature = end($parts); return str_replace(["\n", "\r"], '', $signature); } //
} //