装置

composer require whereof/signature

反对加密形式

alg实现形式key参数
ES384openssl数组蕴含publicKey和privateKey
ES256openssl数组蕴含publicKey和privateKey
HS256hash_hmac字符串
HS384hash_hmac字符串
HS512hash_hmac字符串
RS256openssl数组蕴含publicKey和privateKey
RS384openssl数组蕴含publicKey和privateKey
RS512openssl数组蕴含publicKey和privateKey
EdDSAsodium_crypto字符串

异样捕获

whereof\Signature\Exceptions\SignatureInvalidException 签名不正确whereof\Signature\Exceptions\BeforeValidException 签名在某个工夫点之后能力用whereof\Signature\Exceptions\ExpiredException 签名生效

Example

$time    = time();$payload = [    'iss'  => 'github.com',//签发人    'iat'  => $time, //签发工夫    'nbf'  => $time, //生成签名之后失效    'exp'  => $time + 7200, //过期工夫    'data' => [        'id'       => 88,        'username' => 'whereof'    ],];$jwt = new \whereof\Signature\Jwt(\whereof\Signature\Support\KeyHelper::key());$token = $jwt->encode($payload);$data = $jwt->encode($token);

Example with RS256 (openssl)

$jwt = new \whereof\Signature\Jwt( \whereof\Signature\Support\KeyHelper::RS256(),'RS256'));$jwt = new \whereof\Signature\Jwt( \whereof\Signature\Support\KeyHelper::RS256Pem(),'RS256'));$token = $jwt->encode($payload);$data = $jwt->decode($token);

Example with EdDSA (libsodium and Ed25519 signature)

$jwt = new \whereof\Signature\Jwt( \whereof\Signature\Support\KeyHelper::EdDSA(),'EdDSA'));$token = $jwt->encode($payload);$data = $jwt->decode($token);