Codebase list libcryptx-perl / 5c6c0b7
Add ability to export JWK thumbprint. Issue #8 Felipe Gasper authored 7 years ago Karel Miko committed 7 years ago
3 changed file(s) with 33 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
10661066
10671067 B<BEWARE:> For JWK support you need to have L<JSON::PP>, L<JSON::XS> or L<Cpanel::JSON::XS> module.
10681068
1069 =head2 export_key_jwk_thumbprint
1070
1071 Exports the key’s JSON Web Key Thumbprint as a string.
1072
1073 If you don’t know what this is, see RFC 7638 (C<https://tools.ietf.org/html/rfc7638>).
1074
1075 my $thumbprint = $pk->export_key_jwk_thumbprint('SHA256');
1076
10691077 =head2 export_key_raw
10701078
10711079 Export raw public/private key. Public key is exported in ANS X9.63 format (compressed or uncompressed),
576576
577577 B<BEWARE:> For JWK support you need to have L<JSON::PP>, L<JSON::XS> or L<Cpanel::JSON::XS> module.
578578
579 =head2 export_key_jwk_thumbprint
580
581 Exports the key’s JSON Web Key Thumbprint as a string.
582
583 If you don’t know what this is, see RFC 7638 (C<https://tools.ietf.org/html/rfc7638>).
584
585 my $thumbprint = $pk->export_key_jwk_thumbprint('SHA256');
586
579587 =head2 encrypt
580588
581589 my $pk = Crypt::PK::RSA->new($pub_key_filename);
22 use Test::More;
33
44 plan skip_all => "No JSON::* module installed" unless eval { require JSON::PP } || eval { require JSON::XS } || eval { require Cpanel::JSON::XS };
5 plan tests => 90;
5 plan tests => 92;
66
77 use Crypt::PK::RSA;
88 use Crypt::PK::ECC;
3838 size => 256,
3939 type => 1,
4040 };
41
42 my $RSA1_jwk_thumbprint_sha256 = 'NzbLsXh8uDCcd-6MNwXF4W_7noWXFZAfHkxZsRGC9Xs';
4143
4244 my $RSA2 = {
4345 d => "",
7678 ok(exists $jwkh->{n}, "RSA n test export_key_jwk as hash");
7779 ok(exists $jwkh->{e}, "RSA e test export_key_jwk as hash");
7880 ok(exists $jwkh->{p}, "RSA p test export_key_jwk as hash");
81 my $jwk_tp = $rsa->export_key_jwk_thumbprint('SHA256');
82 is($jwk_tp, $RSA1_jwk_thumbprint_sha256, 'export_key_jwk_thumbprint(SHA256)');
7983 ### jwk re-import private key
8084 $rsa->import_key(\$jwk);
8185 $kh = $rsa->key2hash;
8892 is($kh->{dP}, $RSA1->{dP}, "RSA private dP test JWK1");
8993 is($kh->{dQ}, $RSA1->{dQ}, "RSA private dQ test JWK1");
9094 is($kh->{qP}, $RSA1->{qP}, "RSA private qP test JWK1");
95 $jwk_tp = $rsa->export_key_jwk_thumbprint('SHA256');
96 is($jwk_tp, $RSA1_jwk_thumbprint_sha256, 'export_key_jwk_thumbprint(SHA256)');
9197 ### jwk re-import public key
9298 $rsa->import_key(\$jwkp);
9399 $kh = $rsa->key2hash;
100106 is($kh->{dP}, "", "RSA private dP test JWK2");
101107 is($kh->{dQ}, "", "RSA private dQ test JWK2");
102108 is($kh->{qP}, "", "RSA private qP test JWK2");
109 $jwk_tp = $rsa->export_key_jwk_thumbprint('SHA256');
110 is($jwk_tp, $RSA1_jwk_thumbprint_sha256, 'export_key_jwk_thumbprint(SHA256)');
103111 }
104112
105113 {
147155 size => 32,
148156 type => 1,
149157 };
158
159 my $ec1_jwk_thumbprint_sha256 = 'cn-I_WNMClehiVp51i_0VpOENW1upEerA8sEam5hn-s';
150160
151161 my $EC2 = {
152162 curve_A => "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC",
186196 ok(exists $jwkh->{x}, "ECC x test export_key_jwk as hash");
187197 ok(exists $jwkh->{y}, "ECC y test export_key_jwk as hash");
188198 ok(exists $jwkh->{d}, "ECC d test export_key_jwk as hash");
199 my $jwk_tp = $ec->export_key_jwk_thumbprint('SHA256');
200 is($jwk_tp, $ec1_jwk_thumbprint_sha256, 'export_key_jwk_thumbprint(SHA256)');
189201 ### jwk re-import private key
190202 $ec->import_key(\$jwk);
191203 $kh = $ec->key2hash;
194206 is($kh->{k}, $EC1->{k}, "EC k test JWK1");
195207 is($kh->{curve_name}, "secp256r1", "EC curve test JWK1");
196208 ok($ec->is_private, "EC private test JWK1");
209 $jwk_tp = $ec->export_key_jwk_thumbprint('SHA256');
210 is($jwk_tp, $ec1_jwk_thumbprint_sha256, 'export_key_jwk_thumbprint(SHA256)');
197211 ### jwk re-import public key
198212 $ec->import_key(\$jwkp);
199213 $kh = $ec->key2hash;
202216 is($kh->{k}, "", "EC k test JWK2");
203217 is($kh->{curve_name}, "secp256r1", "EC curve test JWK2");
204218 ok(!$ec->is_private, "EC !private test JWK2");
219 $jwk_tp = $ec->export_key_jwk_thumbprint('SHA256');
220 is($jwk_tp, $ec1_jwk_thumbprint_sha256, 'export_key_jwk_thumbprint(SHA256)');
205221 }
206222
207223 {