NAME

Crypt::JWS::OpenSSL::Util::JWK - Utility to convert pem encoded keys to and from JWKs

VERSION

version 0.001

SYNOPSIS

use Crypt::JWS::OpenSSL::Util::JWK;
my $util = Crypt::JWS::OpenSSL::Util::JWK;

my $jwk = $util->pem_to_jwk( kid => 'my-kid-1', key => $pem_key_in );

my $pem_key_out = $util->jwk_to_pem( $jwk );

DESCRIPTION

Utility to convert pem keys to JWKs and JWKs to pem keys

METHODS

pem_to_jwk( kid => $kid, key => $key, alg => $rsa_alg )

Returns a reference to a hash containing the structure of a JWK.

Parameters

kid

Required. Is added to the returned JWK ref as its unique indentifier.

key

The text of a pem encoded key. For example

  -----BEGIN PUBLIC KEY-----
  MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEeXZBO6GGtqGAH1jAYVfutJdDTdISICoa
  3vMNBrm88aw3LGbDMGXfDbQvUREyN8oGj8yqNkU2yxZPD0n5z4Vxfo1OL49b6jpi
  QErHjC96K0+CmfypI5OXPbd+vpBlUAIj
  -----END PUBLIC KEY-----
alg

If provided for an RSA key, it is added to the returned JWK hash. The value is ignored for an ECC key.

Return values

An RSA public key returns the hash ref structure

{
    kid => '<kid input>',
    kty => 'RSA',
    use => 'sig',
    e   => '..........',
    n   => '..........'
}

An RSA private key returns the hash ref structure

{
    kid => '<kid input>',
    kty => 'RSA',
    use => 'sig',
    e   => '..........',
    n   => '..........',
    d   => '..........',
    p   => '..........',
    q   => '..........',
}

Any RSA key can sign using any supported RSA algorithim. If the optional alg parameter is provided for an RSA key, an 'alg' will be added to the returned JWK hash ref.

An ECC public key returns the hash ref structure ( for a P-384 curve )

{
    kid => '<kid input>',
    kty => 'EC',
    use => 'sig',
    alg => 'ES384',
    crv => 'P-384'
    x   => '..........',
    y   => '..........'
}

An ECC private key returns the hash ref structure ( for a P-521 curve )

{
    kid => '<kid input>',
    kty => 'EC',
    use => 'sig',
    alg => 'ES512',
    crv => 'P-521'
    x   => '..........',
    y   => '..........',
    d   => '..........'
}

jwk_to_pem( $jwk )

Converts a JWK to a pem encoded key

Parameter

jwk

A hash reference containing a JWK structure.

Return value

Returns the text of a pem encoded key

AUTHOR

Mark Dootson <mdootson@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2026 by Mark Dootson

This library is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.