/** * @param mixed[] $data * @param string $signature * @param SignatureDataFormatter $signatureDataFormatter * @return bool * @throws PublicKeyFileException * @throws VerificationFailedException */ public function verifyData(array $data, string $signature, SignatureDataFormatter $signatureDataFormatter) : bool { $message = $signatureDataFormatter->formatDataForSignature($data); $publicKey = file_get_contents($this->bankPublicKeyFile); $publicKeyId = openssl_pkey_get_public($publicKey); if ($publicKeyId === false) { throw new PublicKeyFileException($this->bankPublicKeyFile); } $signature = base64_decode($signature); if ($signature === false) { throw new VerificationFailedException($data, 'Unable to decode signature.'); } $verifyResult = openssl_verify($message, $signature, $publicKeyId, self::HASH_METHOD); openssl_free_key($publicKeyId); if ($verifyResult === -1) { throw new VerificationFailedException($data, openssl_error_string()); } return $verifyResult === 1; }
/** * @param mixed[] $keyPriority * @param mixed[] $data * @param string $expectedData * * @dataProvider getFormatDataForSignatureData */ public function testFormatDataForSignature(array $keyPriority, array $data, string $expectedData) { $signatureDataFormatter = new SignatureDataFormatter($keyPriority); $this->assertSame($expectedData, $signatureDataFormatter->formatDataForSignature($data)); }