JSON-WebToken

 view release on metacpan or  search on metacpan

lib/JSON/WebToken/Crypt/RSA.pm  view on Meta::CPAN

our $ALGORITHM2SIGNING_METHOD_MAP = {
    RS256  => 'use_sha256_hash',
    RS384  => 'use_sha384_hash',
    RS512  => 'use_sha512_hash',
    RSA1_5 => 'use_pkcs1_padding',
};

sub sign {
    my ($class, $algorithm, $message, $key) = @_;

    my $private_key = Crypt::OpenSSL::RSA->new_private_key($key);
    my $method = $ALGORITHM2SIGNING_METHOD_MAP->{$algorithm};
    $private_key->$method;
    return $private_key->sign($message);
}

sub verify {
    my ($class, $algorithm, $message, $key, $signature) = @_;

    my $public_key = Crypt::OpenSSL::RSA->new_public_key($key);
    my $method = $ALGORITHM2SIGNING_METHOD_MAP->{$algorithm};
    $public_key->$method;
    return $public_key->verify($message, $signature) ? 1 : 0;
}

t/02_rsa.t  view on Meta::CPAN

use strict;
use warnings;
use t::Util;

use Test::More;
use Test::Requires 'Crypt::OpenSSL::RSA';

my $rsa = Crypt::OpenSSL::RSA->generate_key(1024);
my $private_key = $rsa->get_private_key_string;
my $public_key  = $rsa->get_public_key_string;

test_encode_decode(
    desc  => 'RS256',
    input => {
        claims     => { foo => 'bar' },
        secret     => $private_key,
        public_key => $public_key,
        algorithm  => 'RS256',
    },
);

test_encode_decode(
    desc  => 'RS384',
    input => {
        claims     => { foo => 'bar' },
        secret     => $private_key,
        public_key => $public_key,
        algorithm  => 'RS384',
    },
);

test_encode_decode(
    desc  => 'RS512',
    input => {
        claims     => { foo => 'bar' },
        secret     => $private_key,
        public_key => $public_key,
        algorithm  => 'RS512',
    },
);

done_testing;

t/spec/draft-ietf-jose-json-web-signature-08-A2.rsa_sha256.t  view on Meta::CPAN

ok $rsa->verify($singing_input, $S);

my $guard = mock_guard(
    'JSON::WebToken' => {
        encode_json => sub {
            my $array = [$header, $claims];
            sub { shift @$array };
        }->(),
    },
    'Crypt::OpenSSL::RSA' => {
        new_private_key => $rsa,
    },
);

my $public_key = $rsa->get_public_key_string;
my $jwt = JSON::WebToken->encode({}, 'dummy', 'RS256');
is $jwt, join q{}, qw{
    eyJhbGciOiJSUzI1NiJ9
    .
    eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt
    cGxlLmNvbS9pc19yb290Ijp0cnVlfQ



( run in 0.267 second using v1.01-cache-2.11-cpan-4d50c553e7e )