web-token/jwt-signature-algorithm-rsa
RSA-based signature algorithms for JWT using the web-token/JWT stack. Provides RSA sign/verify support (e.g., RS256/RS384/RS512) to add strong asymmetric cryptography to token creation and validation workflows.
Installation
composer require web-token/jwt-signature-algorithm-rsa
Ensure your project uses firebase/php-jwt (v5+ recommended) as the base JWT library.
First Use Case Generate an RSA-signed JWT token:
use Firebase\JWT\JWT;
use WebToken\Signature\Algorithm\Rsa\Rsa;
$key = file_get_contents('path/to/private.key');
$token = JWT::encode(
['user_id' => 123],
$key,
'RS256', // Algorithm identifier
null,
['alg' => 'RS256', 'key_id' => 'my-key-id']
);
Where to Look First
tests/ directory for usage examples.composer.json for dependencies and version constraints.Key Management
config).$privateKey = config('jwt.private_key');
$token = JWT::encode($payload, $privateKey, 'RS256');
Algorithm Selection
RS256 for production (SHA-256 + RSA).RS512 for larger payloads (SHA-512 + RSA).RS384 unless explicitly required.Verification
$publicKey = file_get_contents('path/to/public.key');
$decoded = JWT::decode($token, $publicKey, ['RS256']);
Laravel Integration
JWTGuard or Tymon\JWTAuth:
// In AuthServiceProvider
$this->app['auth']->extend('jwt', function ($app) {
return new JWTGuard(
new JWT($app['config']['jwt.key'], 'RS256')
);
});
Batch Processing
array_map with the JWT methods.Key Format
-----BEGIN RSA PRIVATE KEY-----).openssl rsa -in key.pem -outform PEM -out key_converted.pem
Algorithm Mismatch
alg claim matches the key type (e.g., RS256 for RSA).HS256; enforce RS* in your app.Key Size
Clock Skew
nbf (not before) or exp (expiry), account for server clock drift:
$now = time();
$token = JWT::encode($payload, $key, 'RS256', $now - 300); // Allow 5-min skew
Invalid Signature
chmod 600), and format.openssl rsautl -verify -in token.raw -pubin -inkey public.key -sign
Decoding Failures
JWT::decode() without a key to inspect claims).kid (key ID) claim if using multiple keys.Performance
Custom Key Resolvers
$keyResolver = function ($keyId) {
return file_get_contents("keys/{$keyId}.pem");
};
JWT::decode($token, $keyResolver, ['RS256']);
Algorithm Switching
$supportedAlgs = ['RS256', 'RS512', 'HS256'];
JWT::decode($token, $key, $supportedAlgs);
Laravel Service Provider
$this->app->bind('jwt.rsa', function () {
return new Rsa();
});
How can I help you explore Laravel packages today?