简体   繁体   中英

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

How to generate and validate JWT with rsa algorithm use Crypt::JWT ?
I not found any example about how to use Crypt::JWT module, can any one give me a example?

There are examples in the documentation (see, for example, the 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');

What more information do you need?

I have found the way to use Crypt::JWT module:

#!/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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM