Perl:如何使用 rsa 算法使用 Crypt::JWT 生成和验证 JWT?

Perl: How to generate and validate JWT with rsa algorithm use Crypt::JWT?

如何使用 rsa 算法生成和验证 JWT Crypt::JWT?
我没有找到任何关于如何使用 Crypt::JWT 模块的例子,谁能给我一个例子吗?

文档中有示例(例如,参见 MetaCPAN page for the module):

# encoding
use Crypt::JWT qw(encode_jwt);
my $jws_token = encode_jwt(payload=>$data, alg=>'HS256', key=>'secret');
my $jwe_token = encode_jwt(payload=>$data, alg=>'PBES2-HS256+A128KW', enc=>'A128GCM', key=>'secret');
 
# decoding
use Crypt::JWT qw(decode_jwt);
my $data1 = decode_jwt(token=>$jws_token, key=>'secret');
my $data2 = decode_jwt(token=>$jwe_token, key=>'secret');

您还需要什么信息?

我找到了使用Crypt::JWT模块的方法:

#!/usr/bin/perl

use strict;
use warnings;
use Crypt::JWT qw(encode_jwt decode_jwt);
use Crypt::PK::RSA;
use Data::Dumper;

########################
# Encode (Create signature)
########################

my $payload = {
        iss => "issuer",
        sub => "subject",
        exp => time + 86400,
};

my $keyfile = "/path/to/privateKey";
my $key = Crypt::PK::RSA->new($keyfile);
my $alg = "RS256";

my $token = encode_jwt(payload => $payload, key => $key, alg => $alg);
print "token: $token\n";

########################
# Decode (Verify signature)
########################

my $pubkeyfile = "/path/to/publicKey";
my $pubkey = Crypt::PK::RSA->new($pubkeyfile);
my $data = decode_jwt(token => $token, key => $pubkey);
print Dumper $data;