Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Verifier | |
100.00% |
10 / 10 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
| hmac | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| rsa | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| ec | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| verify | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Crypt\Signature; |
| 16 | |
| 17 | use Pop\Crypt\Exception; |
| 18 | |
| 19 | /** |
| 20 | * Pop Crypt signature verifier class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Crypt |
| 24 | * @author Nick Sagona, III <nick@popphp.org> |
| 25 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 4.0.0 |
| 28 | */ |
| 29 | class Verifier |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Verify an HMAC signature |
| 34 | * |
| 35 | * @param string $data |
| 36 | * @param string $signature |
| 37 | * @param string $secret |
| 38 | * @param string $algo |
| 39 | * @return bool |
| 40 | */ |
| 41 | public static function hmac(string $data, string $signature, string $secret, string $algo = 'sha256'): bool |
| 42 | { |
| 43 | $expected = hash_hmac($algo, $data, $secret, true); |
| 44 | return hash_equals($expected, $signature); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Verify an RSA signature |
| 49 | * |
| 50 | * @param string $data |
| 51 | * @param string $signature |
| 52 | * @param string $publicKey |
| 53 | * @param string $algo |
| 54 | * @throws Exception |
| 55 | * @return bool |
| 56 | */ |
| 57 | public static function rsa(string $data, string $signature, string $publicKey, string $algo = 'sha256'): bool |
| 58 | { |
| 59 | return self::verify($data, $signature, $publicKey, $algo); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Verify an EC (ECDSA) signature |
| 64 | * |
| 65 | * @param string $data |
| 66 | * @param string $signature |
| 67 | * @param string $publicKey |
| 68 | * @param string $algo |
| 69 | * @throws Exception |
| 70 | * @return bool |
| 71 | */ |
| 72 | public static function ec(string $data, string $signature, string $publicKey, string $algo = 'sha256'): bool |
| 73 | { |
| 74 | return self::verify($data, $signature, $publicKey, $algo); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Run openssl_verify() and translate its tri-state (1/0/-1) result |
| 79 | * |
| 80 | * @param string $data |
| 81 | * @param string $signature |
| 82 | * @param string $publicKey |
| 83 | * @param string $algo |
| 84 | * @throws Exception |
| 85 | * @return bool |
| 86 | */ |
| 87 | protected static function verify(string $data, string $signature, string $publicKey, string $algo): bool |
| 88 | { |
| 89 | $result = @openssl_verify($data, $signature, $publicKey, $algo); |
| 90 | |
| 91 | if ($result === false || $result === -1) { |
| 92 | throw new Exception( |
| 93 | 'Unable to verify the signature: ' . (openssl_error_string() ?: 'invalid key or algorithm') |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | return ($result === 1); |
| 98 | } |
| 99 | |
| 100 | } |