| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\JoseVerifier; |
| 6 |
|
| 7 |
use Base64Url\Base64Url; |
| 8 |
use Jose\Component\Core\JWK; |
| 9 |
|
| 10 |
use function preg_match; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
function jose_secret_key(string $secret, ?string $alg = null): JWK |
| 16 |
{ |
| 17 |
if (null !== $alg && preg_match('/^A(\d{3})(?:GCM)?KW$/', $alg, $matches)) { |
| 18 |
|
| 19 |
return derived_key($secret, (int) $matches[1]); |
| 20 |
} |
| 21 |
|
| 22 |
if (null !== $alg && preg_match('/^A(\d{3})(?:GCM|CBC-HS(\d{3}))$/', $alg, $matches)) { |
| 23 |
|
| 24 |
return derived_key($secret, (int) ($matches[2] ?? $matches[1])); |
| 25 |
} |
| 26 |
|
| 27 |
$key = new JWK([ |
| 28 |
'k' => Base64Url::encode($secret), |
| 29 |
'kty' => 'oct', |
| 30 |
]); |
| 31 |
|
| 32 |
return $key; |
| 33 |
} |