18910140161

php签名

顺晟科技

2021-09-21 14:15:55

175

安装

composer require whereof/signature

支持加密方式

alg实现方式key参数ES384openssl数组包含publicKey和privateKeyES256openssl数组包含publicKey和privateKeyHS256hash_hmac字符串HS384hash_hmac字符串HS512hash_hmac字符串RS256openssl数组包含publicKey和privateKeyRS384openssl数组包含publicKey和privateKeyRS512openssl数组包含publicKey和privateKeyEdDSAsodium_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);
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航