0

我无法从 JWT 验证 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”。是这样的:

  • OPENSSL_ALGO_SHA256
  • “SHA256”
  • “sha256”
  • “RSA-SHA256”
  • “sha256WithRSAEncryption”
  • 还有什么?

我已经尝试了所有来自 php 文档的方法,听起来它们可能是正确的,但无论我尝试什么,它总是返回 0(未验证),但根据 jwt.io,我正在测试 jwt、签名和公钥with 是有效的。

我不确定我做错了什么。

4

1 回答 1

0

经过更多搜索并弄清楚了,我能够找到一些接近的实现。

首先,算法需要设置为“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, '-_', '+/'));
}

这两个一起,现在它工作了。

于 2022-01-05T01:10:18.850 回答