Suffit/RSA.pm

filter: Statementstmt 78.1% 68 / 87 Branchbran 35.7% 10 / 28 Conditioncond 23.8% 15 / 63 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 90.9% 10 / 11 Podpod 100% 5 / 5 totaltotal 48.8% 108 / 221 CC 34 SCAR 35.0
CC
34
Cov
52%
verify
110.0
decrypt
15.3
encrypt
13.1
CRAP 33.0
« Suffit/JWT.pm ↑ Summary Suffit/Util.pm »
1 package WWW::Suffit::RSA;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf8
6  
7 =head1 NAME
8  
9 WWW::Suffit::RSA - The RSA encryption and signing subclass
10  
11 =head1 SYNOPSIS
12  
13 use WWW::Suffit::RSA;
14  
15 my $rsa = WWW::Suffit::RSA->new;
16  
17 $rsa->keygen(2048);
18 my $private_key = $rsa->private_key;
19 my $public_key = $rsa->public_key;
20  
21 my $b64_cipher_text = $rsa->encrypt("test");
22 my $plain_text = $rsa->decrypt($b64_cipher_text);
23  
24 my $signature = $rsa->sign("Text", 256) or die $rsa->error;
25 $rsa->verify("Text", $signature, 256) or die $rsa->error || "Incorrect signature";
26  
27 =head1 DESCRIPTION
28  
29 The RSA encryption and signing subclass
30  
31 This module based on L<Crypt::OpenSSL::RSA>
32  
33 =head1 METHODS
34  
35 L<WWW::Suffit::RSA> inherits all of the properties and methods from L<Mojo::Base> and implements the following new ones.
36  
37 =head2 decrypt
38  
39 my $plain_text = $rsa->decrypt($b64_cipher_text);
40  
41 Decrypt a base64 short "string" to plain text
42  
43 =head2 encrypt
44  
45 my $b64_cipher_text = $rsa->encrypt("test");
46  
47 Encrypt a short "string" using the public key and returns base64 string
48  
49 =head2 error
50  
51 $rsa->error($new_error);
52 my $error = $rsa->error;
53  
54 Sets/gets the error string
55  
56 =head2 keygen
57  
58 $rsa->keygen( $key_size );
59 my $public_key = $rsa->public_key;
60 my $private_key = $rsa->private_key;
61  
62 Create a new private/public key pair (the public exponent is 65537).
63 The argument is the key size, default is 2048
64  
65 =head2 private_key
66  
67 The RSA private key to be used in edcoding an asymmetrically signed data
68  
69 =head2 public_key
70  
71 The RSA public key to be used in decoding an asymmetrically signed data
72  
73 =head2 sign
74  
75 my $signature = $rsa->sign($string, $size);
76  
77 Returns the RSA signature for the given size and string.
78 The L</private_key> attribute is used as the private key.
79 The result is not yet base64 encoded.
80 This method is provided mostly for the purposes of subclassing.
81  
82 =head2 verify
83  
84 my $bool = $rsa->verify($string, $signature, $size);
85  
86 Returns true if the given RSA size algorithm validates the given string and signature.
87 The L</public_key> attribute is used as the public key.
88 This method is provided mostly for the purposes of subclassing.
89  
90 =head1 DEPENDENCIES
91  
92 L<Crypt::OpenSSL::RSA>
93  
94 =head1 HISTORY
95  
96 See C<Changes> file
97  
98 =head1 TO DO
99  
100 See C<TODO> file
101  
102 =head1 SEE ALSO
103  
104 L<Mojolicious>, L<Crypt::OpenSSL::RSA>
105  
106 =head1 AUTHOR
107  
108 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
109  
110 =head1 COPYRIGHT
111  
112 Copyright (C) 1998-2026 D&D Corporation
113  
114 =head1 LICENSE
115  
116 This program is distributed under the terms of the Artistic License Version 2.0
117  
118 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
119  
120 =cut
121  
122 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 2 ▶use Mojo::Util qw/b64_encode b64_decode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 2 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
127 2 KEY_SIZE => 2048,
128 SHA_SIZE => 256,
129 KEY_SIZES_MAP => [512, 1024, 2048, 4096],
130 SHA_SIZES_MAP => [224, 256, 384, 512],
131 2 };
132  
133 2 has 'key_size' => KEY_SIZE; # RSA key size
134 2 has 'sha_size' => SHA_SIZE; # RSA SHA size
135 2 has 'private_key' => ''; # RSA private key
136 2 has 'public_key' => ''; # RSA public key
137 2 has 'error' => ''; # Error string
138  
139 2 ▶sub keygen {
Subroutine keygen: called CC=4 SCAR=14.2
Pod documented
140 2 my $self = shift;
141 2 ▶ my $key_size = shift || $self->key_size || KEY_SIZE;
Condition40%2/5
shift() || $self->key_size || 2048
l!l
20
shift() or $self->key_size
l!l&&r!l&&!r
020
MC/DC0%0/2
shift() || $self->key_size || 2048
shift() $self->key_size
Truth table33%1/3
A or B
A= shift()B= $self->key_size
ABresult
1X1
011
000
142 2 $self->error(''); # Flush error string first
143  
144 # Correct key size
145 $key_size = KEY_SIZE
146 2 ▶ unless grep {$_ == $key_size} @{(KEY_SIZES_MAP)};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $key_size;} @{[512,1024,2048,4096];} 0 2 2
147  
148 2 my $rsa = Crypt::OpenSSL::RSA->generate_key($key_size);
149 2 my $private_key = $rsa->get_private_key_string;
150 2 $self->private_key($private_key);
151 2 my $public_key = $rsa->get_public_key_string;
152 2 $self->public_key($public_key);
153  
154 2 return $self;
155 }
156 1 ▶sub encrypt {
Subroutine encrypt: called CC=7 SCAR=25.7
Pod documented
157 1 my ($self, $text) = @_;
158 1 $self->error(''); # Flush error string first
159 1 ▶ $self->error('The text for encrypting is not specified') && return unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 1 1
Condition20%1/5
$text || $self->error('The text for encrypting is not specified') && (return)
l!l
100
$self->error("The text for encrypting is not specified") and return
!ll
00
MC/DC0%0/2
$text || $self->error('The text for encrypting is not specified') && (return)
$text $self->error("The text for encrypting is not specified")
Truth table33%1/3
A || B
A= $textB= $self->error("The text for encrypting is not specified")
ABresult
1X1
011
000
160  
161 # Get RSA public key
162 1 ▶ my $public_key = $self->public_key // '';
Condition50%1/2
$self->public_key // ""
l!l
10
MC/DC0%0/1
$self->public_key // ""
$self->public_key
Truth table50%1/2
A
A= $self->public_key
Aresult
11
00
163 1 ▶ $self->error('Public key not specified') && return unless length $public_key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $public_key 0 1 1
Condition20%1/5
length $public_key || $self->error('Public key not specified') && (return)
l!l
100
$self->error("Public key not specified") and return
!ll
00
MC/DC0%0/2
length $public_key || $self->error('Public key not specified') && (return)
length $public_key $self->error("Public key not specified")
Truth table33%1/3
A || B
A= length $public_keyB= $self->error("Public key not specified")
ABresult
1X1
011
000
164  
165 # Create RSA object
166 1 my $rsa_pub = eval {Crypt::OpenSSL::RSA->new_public_key($public_key)};
167 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
168 0 chomp $@;
169 0 $self->error($@);
170 0 return;
171 }
172  
173 1 return b64_encode($rsa_pub->encrypt($text), '');
174 }
175 1 ▶sub decrypt {
Subroutine decrypt: called CC=8 SCAR=27.3
Pod documented
176 1 my ($self, $cipher) = @_;
177 1 $self->error(''); # Flush error string first
178 1 ▶ $self->error('The ciphertext for decryption is not specified') && return unless $cipher;
Branch: 1 branch
BranchTrueFalseTotal
unless $cipher 0 1 1
Condition20%1/5
$cipher || $self->error('The ciphertext for decryption is not specified') && (return)
l!l
100
$self->error("The ciphertext for decryption is not specified") and return
!ll
00
MC/DC0%0/2
$cipher || $self->error('The ciphertext for decryption is not specified') && (return)
$cipher $self->error("The ciphertext for decryption is not specified")
Truth table33%1/3
A || B
A= $cipherB= $self->error("The ciphertext for decryption is not specified")
ABresult
1X1
011
000
179  
180 # Get RSA private key
181 1 ▶ my $private_key = $self->private_key // '';
Condition50%1/2
$self->private_key // ""
l!l
10
MC/DC0%0/1
$self->private_key // ""
$self->private_key
Truth table50%1/2
A
A= $self->private_key
Aresult
11
00
182 1 ▶ $self->error('Private key not specified') && return unless length $private_key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $private_key 0 1 1
Condition20%1/5
length $private_key || $self->error('Private key not specified') && (return)
l!l
100
$self->error("Private key not specified") and return
!ll
00
MC/DC0%0/2
length $private_key || $self->error('Private key not specified') && (return)
length $private_key $self->error("Private key not specified")
Truth table33%1/3
A || B
A= length $private_keyB= $self->error("Private key not specified")
ABresult
1X1
011
000
183  
184 # Create RSA object
185 1 my $rsa_priv = Crypt::OpenSSL::RSA->new_private_key($private_key);
186  
187 1 ▶ my $plaintext = eval {$rsa_priv->decrypt(b64_decode($cipher))} // '';
Condition50%1/2
eval { do { $rsa_priv->decrypt(&b64_decode($cipher)) } } // ""
l!l
10
MC/DC0%0/1
eval { do { $rsa_priv->decrypt(&b64_decode($cipher)) } } // ""
eval { do { $rsa_priv->decrypt(&b64_decode($cipher)) } }
Truth table50%1/2
A
A= eval { do { $rsa_priv->decrypt(&b64_decode($cipher)) } }
Aresult
11
00
188 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
189 0 chomp $@;
190 0 $self->error($@);
191 0 return;
192 }
193  
194 1 return $plaintext;
195 }
196 2 ▶sub sign {
Subroutine sign: called CC=9 SCAR=25.3
Pod documented
197 2 my ($self, $text, $size) = @_;
198 2 ▶ $size ||= $self->sha_size || SHA_SIZE;
Condition60%3/5
$size ||= $self->sha_size || 256
l!l&&r!l&&!r
110
$self->sha_size || 256
l!l
10
MC/DC0%0/2
$size ||= $self->sha_size || 256
$size $self->sha_size
Truth table66%2/3
A ||= B
A= $sizeB= $self->sha_size
ABresult
1X1
011
000
199 2 $self->error(''); # Flush error string first
200  
201 # Get RSA private key
202 2 ▶ my $private_key = $self->private_key // '';
Condition50%1/2
$self->private_key // ""
l!l
20
MC/DC0%0/1
$self->private_key // ""
$self->private_key
Truth table50%1/2
A
A= $self->private_key
Aresult
11
00
203 2 ▶ $self->error('Private key not specified') && return unless length $private_key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $private_key 0 2 2
Condition25%1/4
length $private_key || $self->error('Private key not specified') && (return)
l!l
20
$self->error("Private key not specified") and return
!ll
00
MC/DC0%0/2
length $private_key || $self->error('Private key not specified') && (return)
length $private_key $self->error("Private key not specified")
Truth table33%1/3
A || B
A= length $private_keyB= $self->error("Private key not specified")
ABresult
1X1
011
000
204  
205 # Correct sha size
206 $size = SHA_SIZE
207 2 ▶ unless grep {$_ == $size} @{(SHA_SIZES_MAP)};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $size;} @{[224,256,384,512];} 0 2 2
208  
209 # Create RSA object
210 2 my $rsa_priv = Crypt::OpenSSL::RSA->new_private_key($private_key);
211  
212 2 my $m = $rsa_priv->can("use_sha${size}_hash");
213 2 ▶ $self->error('Unsupported SHA hash size') && return unless $m;
Branch: 1 branch
BranchTrueFalseTotal
unless $m 0 2 2
Condition25%1/4
$m || $self->error('Unsupported SHA hash size') && (return)
l!l
20
$self->error("Unsupported SHA hash size") and return
!ll
00
MC/DC0%0/2
$m || $self->error('Unsupported SHA hash size') && (return)
$m $self->error("Unsupported SHA hash size")
Truth table33%1/3
A || B
A= $mB= $self->error("Unsupported SHA hash size")
ABresult
1X1
011
000
214 2 $rsa_priv->$m; # Switch to alg
215  
216 # Sign!
217 2 return b64_encode($rsa_priv->sign($text), '');
218 }
219 0 ▶sub verify {
Subroutine verify: not called CC=10 SCAR=47.0
Pod documented
220 0 my ($self, $text, $signature, $size) = @_;
221 0 ▶ $size ||= $self->sha_size || SHA_SIZE;
Condition0%0/5
$size ||= $self->sha_size || 256
l!l&&r!l&&!r
000
$self->sha_size || 256
l!l
00
MC/DC0%0/2
$size ||= $self->sha_size || 256
$size $self->sha_size
Truth table0%0/3
A ||= B
A= $sizeB= $self->sha_size
ABresult
1X1
011
000
222 0 $self->error(''); # Flush error string first
223  
224 # Get RSA public key
225 0 ▶ my $public_key = $self->public_key // '';
Condition0%0/2
$self->public_key // ""
l!l
00
MC/DC0%0/1
$self->public_key // ""
$self->public_key
Truth table0%0/2
A
A= $self->public_key
Aresult
11
00
226 0 ▶ $self->error('Public key not specified') && return unless length $public_key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $public_key 0 0 0
Condition0%0/5
length $public_key || $self->error('Public key not specified') && (return)
l!l&&r!l&&!r
000
$self->error("Public key not specified") and return
!ll
00
MC/DC0%0/2
length $public_key || $self->error('Public key not specified') && (return)
length $public_key $self->error("Public key not specified")
Truth table0%0/3
A || B
A= length $public_keyB= $self->error("Public key not specified")
ABresult
1X1
011
000
227  
228 # Correct sha size
229 $size = SHA_SIZE
230 0 ▶ unless grep {$_ == $size} @{(SHA_SIZES_MAP)};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $size;} @{[224,256,384,512];} 0 0 0
231  
232 # Create RSA object
233 0 my $rsa_pub = Crypt::OpenSSL::RSA->new_public_key($public_key);
234  
235 0 my $m = $rsa_pub->can("use_sha${size}_hash");
236 0 ▶ $self->error('Unsupported SHA hash size') && return unless $m;
Branch: 1 branch
BranchTrueFalseTotal
unless $m 0 0 0
Condition0%0/5
$m || $self->error('Unsupported SHA hash size') && (return)
l!l&&r!l&&!r
000
$self->error("Unsupported SHA hash size") and return
!ll
00
MC/DC0%0/2
$m || $self->error('Unsupported SHA hash size') && (return)
$m $self->error("Unsupported SHA hash size")
Truth table0%0/3
A || B
A= $mB= $self->error("Unsupported SHA hash size")
ABresult
1X1
011
000
237 0 $rsa_pub->$m; # Switch to alg
238  
239 # Verify!
240 0 ▶ return $rsa_pub->verify($text, b64_decode($signature)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rsa_pub->verify($text, &b64_decode($signature)) ? : 0 0 0
241 }
242  
243 2 1;
244  
245 __END__
« Suffit/JWT.pm ↑ Summary Suffit/Util.pm »