From 3472b19591b836ce8dd3bb6f41d24c3c0efcec08 Mon Sep 17 00:00:00 2001 From: Eduardo Bacarin Date: Tue, 24 Jun 2025 14:41:29 -0300 Subject: [PATCH] Identation and fixes to load keys --- src/Kore.php | 37 ++++++++++++++++++++++++------------- src/helpers.php | 4 ++-- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Kore.php b/src/Kore.php index 1c2be7b..7744c2a 100644 --- a/src/Kore.php +++ b/src/Kore.php @@ -2,29 +2,40 @@ namespace Kontrl\PhpKore; +use phpseclib3\Common\Functions\Strings; +use phpseclib3\Crypt\EC; +use phpseclib3\Crypt\EC\PublicKey; use phpseclib3\Crypt\PublicKeyLoader; +use phpseclib3\Crypt\RSA; class Kore { private $url = 'https://httpbin.org'; - public function __construct(){ - + public function __construct() {} + + public function signBody($body, string|array $key, string|bool $password = false) + { + $privateKey = PublicKeyLoader::loadPrivateKey(base64_decode($key), $password); + $sig = $privateKey->sign($body); + $sig = base64_encode($sig); + return $sig; } - public function signBody(array $body, string|array $key, string|bool $password = false){ - $publicKey = PublicKeyLoader::loadPrivateKey($key, $password); - return base64_encode($publicKey->sign(json_encode($body))); + public function verifySignature($signature, $message, $key) + { + $publicKey = PublicKeyLoader::loadPublicKey($key); + return $publicKey->verify($message, $signature); } - - public function testRequest(){ + + public function testRequest() + { return [ - 'get' => curlRequest($this->url.'/get', 'GET')['status'], - 'post' => curlRequest($this->url.'/post', 'POST', [], [], ['test' => 'test'])['status'], - 'patch' => curlRequest($this->url.'/patch', 'PATCH', [], [], ['test' => 'test'])['status'], - 'put' => curlRequest($this->url.'/put', 'PUT', [], [], ['test' => 'test'])['status'], - 'delete' => curlRequest($this->url.'/delete', 'DELETE')['status'] + 'get' => curlRequest($this->url . '/get', 'GET')['status'], + 'post' => curlRequest($this->url . '/post', 'POST', [], [], ['test' => 'test'])['status'], + 'patch' => curlRequest($this->url . '/patch', 'PATCH', [], [], ['test' => 'test'])['status'], + 'put' => curlRequest($this->url . '/put', 'PUT', [], [], ['test' => 'test'])['status'], + 'delete' => curlRequest($this->url . '/delete', 'DELETE')['status'] ]; } - } diff --git a/src/helpers.php b/src/helpers.php index 75b0cf6..517c0d2 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -13,7 +13,7 @@ if (!function_exists('curlRequest')) { if (!empty($queryParams)) { $url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($queryParams); } - + $options = [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, @@ -29,7 +29,7 @@ if (!function_exists('curlRequest')) { } $options[CURLOPT_POSTFIELDS] = $body; } - + if (!empty($headers)) { $options[CURLOPT_HTTPHEADER] = $headers; }