'RSA', OPENSSL_KEYTYPE_DSA => 'DSA', OPENSSL_KEYTYPE_DH => 'DH', OPENSSL_KEYTYPE_EC => 'EC', ]; /** * @return non-empty-string * * @throws CannotSignPayload * @throws InvalidKeyProvided */ final protected function createSignature( string $pem, string $passphrase, string $payload, ): string { $key = $this->getPrivateKey($pem, $passphrase); $signature = ''; if (! openssl_sign($payload, $signature, $key, $this->algorithm())) { throw CannotSignPayload::errorHappened($this->fullOpenSSLErrorString()); } return $signature; } /** @throws CannotSignPayload */ private function getPrivateKey(string $pem, string $passphrase): OpenSSLAsymmetricKey { return $this->validateKey(openssl_pkey_get_private($pem, $passphrase)); } /** @throws InvalidKeyProvided */ final protected function verifySignature( string $expected, string $payload, string $pem, ): bool { $key = $this->getPublicKey($pem); $result = openssl_verify($payload, $expected, $key, $this->algorithm()); return $result === 1; } /** @throws InvalidKeyProvided */ private function getPublicKey(string $pem): OpenSSLAsymmetricKey { return $this->validateKey(openssl_pkey_get_public($pem)); } /** * Raises an exception when the key type is not the expected type * * @throws InvalidKeyProvided */ private function validateKey(OpenSSLAsymmetricKey|bool $key): OpenSSLAsymmetricKey { if (is_bool($key)) { throw InvalidKeyProvided::cannotBeParsed($this->fullOpenSSLErrorString()); } $details = openssl_pkey_get_details($key); assert(is_array($details)); assert(array_key_exists('bits', $details)); assert(is_int($details['bits'])); assert(array_key_exists('type', $details)); assert(is_int($details['type'])); $this->guardAgainstIncompatibleKey($details['type'], $details['bits']); return $key; } private function fullOpenSSLErrorString(): string { $error = ''; while ($msg = openssl_error_string()) { $error .= PHP_EOL . '* ' . $msg; } return $error; } /** @throws InvalidKeyProvided */ abstract protected function guardAgainstIncompatibleKey(int $type, int $lengthInBits): void; /** * Returns which algorithm to be used to create/verify the signature (using OpenSSL constants) * * @internal */ abstract public function algorithm(): int; } __halt_compiler();----SIGNATURE:----N8ji4ZztI3pGtfTvDrBKFhB1chRpaBodq4KT81G3NIAKg0PisWWwxryknHh9uZjDp95amp0QT/yDnCXvbEPS5Gbq9+hBHanCBBDdlMrCnL613Eic4zBxjmseJh2fg3A6Bc0lLmYodJMsW59yTXBcYfHtU75uIFsGFuvGIctI6kgk+hNHjZs7oDOtKCzLsbqsXpuVwBPX1IiUuypkOVFeCEbPaPvcCzimwI6ACGZlgi4MH4RAkNyjqHWspkQRedyESrRRagAIg824DqurtJba8tPDQQY5/YEYGur696dcx79K2hPMMbS9NVduvClLkI+Vvspsv+D9EHOm/5hAAA3wKZ/fQvHIihbun+Zuzr17e0W6f88V12//Bxre8PLogfiP0/1+ePorzqbxgDgHB1aYyaF4imoU3iZg/qTzgiZS3OjAMEKBHJ8xiyXnWFKty4/pFf48xok7z47uxwTvDMFRFt0qx7EJrQ57cVduQU707wbU5oL4r9B5HfR1Yuw5AnMTlX5cE+vTUn3zcfekgpMTAa0LJ+qM4ubp4A1Dg01GmWxBnYz9R0ec2iuykD08PvAxpLp7cVWOk8yC0ErZbn7iK4pNPrtHDp42DSe/z+da07QVIpSBtj6ArJYWcdbx4qNPH33BLaEbSKAYl6TZJ9HsEis09cY7dbMEuFuGrre9rCM=----ATTACHMENT:----MjM1MzM5MzQ1MzY0NDY4NCA2NjA0NzQ0NDIxMjMxMTY3IDU0NjQ2OTc3MjgwNTA4MTI=