我在PHP中验证RS256签名时遇到了困难。
我有过
$to_verify = substr(Cookie::get('CF_Authorization'), 0, strrpos(Cookie::get('CF_Authorization'), "."));
$signature = base64_decode(explode('.', Cookie::get('CF_Authorization'))[2]);
return openssl_verify($to_verify, $signature, $cert, 'RSA-SHA256'));
我的部分问题是,我不知道什么是最正确的算法使用标题"alg":"RS256“。这就是:
我已经尝试过php文档中的所有这些,听起来可能是正确的,但不管我尝试了什么,它总是返回0(没有验证),但是根据jwt.io的jwt,签名和公钥是有效的。
我不知道我做错了什么。
发布于 2022-01-05 01:10:18
经过更多的搜索后,我找到了一些比较接近的实现,并找到了答案。
首先,算法需要设置为"SHA256“。
其次,我需要稍微修改一下解码,而不是base64_decode,我在开源实现中找到了一个函数。
private function safeUrlDecode($input)
{
$remainder = strlen($input) % 4;
if ($remainder) {
$pad = 4 - $remainder;
$input .= str_repeat('=', $pad);
}
return base64_decode(strtr($input, '-_', '+/'));
}这两个人在一起现在起作用了。
https://stackoverflow.com/questions/70586015
复制相似问题