|
10 | 10 | use Jose\Component\Core\Util\RSAKey;
|
11 | 11 | use RuntimeException;
|
12 | 12 | use function chr;
|
| 13 | +use function extension_loaded; |
13 | 14 | use function ord;
|
14 | 15 | use const STR_PAD_LEFT;
|
15 | 16 |
|
@@ -38,6 +39,9 @@ public static function sign(RSAKey $key, string $message, string $hash, int $mod
|
38 | 39 | return self::signWithPSS($key, $message, $hash);
|
39 | 40 |
|
40 | 41 | case self::SIGNATURE_PKCS1:
|
| 42 | + if (! extension_loaded('openssl')) { |
| 43 | + throw new RuntimeException('Please install the OpenSSL extension'); |
| 44 | + } |
41 | 45 | $result = openssl_sign($message, $signature, $key->toPEM(), $hash);
|
42 | 46 | if ($result !== true) {
|
43 | 47 | throw new RuntimeException('Unable to sign the data');
|
@@ -70,11 +74,17 @@ public static function signWithPSS(RSAKey $key, string $message, string $hash):
|
70 | 74 |
|
71 | 75 | public static function verify(RSAKey $key, string $message, string $signature, string $hash, int $mode): bool
|
72 | 76 | {
|
73 |
| - return match ($mode) { |
74 |
| - self::SIGNATURE_PSS => self::verifyWithPSS($key, $message, $signature, $hash), |
75 |
| - self::SIGNATURE_PKCS1 => openssl_verify($message, $signature, $key->toPEM(), $hash) === 1, |
76 |
| - default => throw new InvalidArgumentException('Unsupported mode.'), |
77 |
| - }; |
| 77 | + switch ($mode) { |
| 78 | + case self::SIGNATURE_PSS: |
| 79 | + return self::verifyWithPSS($key, $message, $signature, $hash); |
| 80 | + case self::SIGNATURE_PKCS1: |
| 81 | + if (! extension_loaded('openssl')) { |
| 82 | + throw new RuntimeException('Please install the OpenSSL extension'); |
| 83 | + } |
| 84 | + return openssl_verify($message, $signature, $key->toPEM(), $hash) === 1; |
| 85 | + default: |
| 86 | + throw new InvalidArgumentException('Unsupported mode.'); |
| 87 | + } |
78 | 88 | }
|
79 | 89 |
|
80 | 90 | /**
|
|
0 commit comments