| 1 | ############################################################################## | ||||||||||
| 2 | # | ||||||||||
| 3 | # Data::Tools::Crypto perl module, base class | ||||||||||
| 4 | # Copyright (c) 2013-2026 Vladi Belperchinov-Shabanski "Cade" | ||||||||||
| 5 | # <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||||||||||
| 6 | # http://cade.noxrun.com/ | ||||||||||
| 7 | # | ||||||||||
| 8 | # GPL | ||||||||||
| 9 | # | ||||||||||
| 10 | ############################################################################## | ||||||||||
| 11 | # | ||||||||||
| 12 | # freeze/thaw API actually uses JSON as safer option than Storable | ||||||||||
| 13 | # | ||||||||||
| 14 | ############################################################################## | ||||||||||
| 15 | package Data::Tools::Crypto; | ||||||||||
| 16 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 6 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 6 | ▶ | use Data::Tools qw( str_hex str_unhex ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 6 | ▶ | use MIME::Base64; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 6 | our $VERSION = '1.53'; | |||||||||
| 23 | |||||||||||
| 24 | ############################################################################## | ||||||||||
| 25 | |||||||||||
| 26 | # decrypt() returns undef for data which does not decrypt, thaw must pass that | ||||||||||
| 27 | # on instead of handing undef to decode_json(), which would die | ||||||||||
| 28 | |||||||||||
| 29 | sub __thaw_json | ||||||||||
| 30 | 33 | ▶ | { | ||||||||
|
Subroutine
__thaw_json: called CC=2 SCAR=6.9
| |||||||||||
| 31 | 33 | my $json = shift; | |||||||||
| 32 | |||||||||||
| 33 | 33 | ▶ | return undef unless defined $json; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 17 | return decode_json( $json ); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | #----------------------------------------------------------------------------- | ||||||||||
| 38 | |||||||||||
| 39 | sub encrypt_hex | ||||||||||
| 40 | 12 | ▶ | { | ||||||||
|
Subroutine
encrypt_hex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 41 | 12 | my $self = shift; | |||||||||
| 42 | |||||||||||
| 43 | 12 | return str_hex( $self->encrypt( $_[0] ) ); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | sub decrypt_hex | ||||||||||
| 47 | 13 | ▶ | { | ||||||||
|
Subroutine
decrypt_hex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 48 | 13 | my $self = shift; | |||||||||
| 49 | |||||||||||
| 50 | 13 | return $self->decrypt( str_unhex( $_[0] ) ); | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | #----------------------------------------------------------------------------- | ||||||||||
| 54 | |||||||||||
| 55 | sub freeze | ||||||||||
| 56 | 14 | ▶ | { | ||||||||
|
Subroutine
freeze: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 57 | 14 | my $self = shift; | |||||||||
| 58 | |||||||||||
| 59 | # reference to any data/scalar/hash/array | ||||||||||
| 60 | 14 | return $self->encrypt( encode_json( $_[0] ) ); | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | sub thaw | ||||||||||
| 64 | 16 | ▶ | { | ||||||||
|
Subroutine
thaw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 65 | 16 | my $self = shift; | |||||||||
| 66 | |||||||||||
| 67 | # reference to any data/scalar/hash/array | ||||||||||
| 68 | 16 | return __thaw_json( $self->decrypt( $_[0] ) ); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | sub freeze_hex | ||||||||||
| 72 | 6 | ▶ | { | ||||||||
|
Subroutine
freeze_hex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 73 | 6 | my $self = shift; | |||||||||
| 74 | |||||||||||
| 75 | # reference to any data/scalar/hash/array | ||||||||||
| 76 | 6 | return $self->encrypt_hex( encode_json( $_[0] ) ); | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | sub thaw_hex | ||||||||||
| 80 | 7 | ▶ | { | ||||||||
|
Subroutine
thaw_hex: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 81 | 7 | my $self = shift; | |||||||||
| 82 | |||||||||||
| 83 | # reference to any data/scalar/hash/array | ||||||||||
| 84 | 7 | return __thaw_json( $self->decrypt_hex( $_[0] ) ); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | #----------------------------------------------------------------------------- | ||||||||||
| 88 | |||||||||||
| 89 | sub encrypt_base64 | ||||||||||
| 90 | 12 | ▶ | { | ||||||||
|
Subroutine
encrypt_base64: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 91 | 12 | my $self = shift; | |||||||||
| 92 | |||||||||||
| 93 | 12 | return MIME::Base64::encode_base64( $self->encrypt( $_[0] ), '' ); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | sub decrypt_base64 | ||||||||||
| 97 | 12 | ▶ | { | ||||||||
|
Subroutine
decrypt_base64: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 98 | 12 | my $self = shift; | |||||||||
| 99 | |||||||||||
| 100 | 12 | return $self->decrypt( MIME::Base64::decode_base64( $_[0] ) ); | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | sub freeze_base64 | ||||||||||
| 104 | 6 | ▶ | { | ||||||||
|
Subroutine
freeze_base64: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 105 | 6 | my $self = shift; | |||||||||
| 106 | |||||||||||
| 107 | # reference to any data/scalar/hash/array | ||||||||||
| 108 | 6 | return $self->encrypt_base64( encode_json( $_[0] ) ); | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | sub thaw_base64 | ||||||||||
| 112 | 6 | ▶ | { | ||||||||
|
Subroutine
thaw_base64: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 113 | 6 | my $self = shift; | |||||||||
| 114 | |||||||||||
| 115 | # reference to any data/scalar/hash/array | ||||||||||
| 116 | 6 | return __thaw_json( $self->decrypt_base64( $_[0] ) ); | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | #----------------------------------------------------------------------------- | ||||||||||
| 120 | |||||||||||
| 121 | sub encrypt_base64url | ||||||||||
| 122 | 13 | ▶ | { | ||||||||
|
Subroutine
encrypt_base64url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 123 | 13 | my $self = shift; | |||||||||
| 124 | |||||||||||
| 125 | 13 | return MIME::Base64::encode_base64url( $self->encrypt( $_[0] ) ); | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | sub decrypt_base64url | ||||||||||
| 129 | 13 | ▶ | { | ||||||||
|
Subroutine
decrypt_base64url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 130 | 13 | my $self = shift; | |||||||||
| 131 | |||||||||||
| 132 | 13 | return $self->decrypt( MIME::Base64::decode_base64url( $_[0] ) ); | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | sub freeze_base64url | ||||||||||
| 136 | 6 | ▶ | { | ||||||||
|
Subroutine
freeze_base64url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 137 | 6 | my $self = shift; | |||||||||
| 138 | |||||||||||
| 139 | # reference to any data/scalar/hash/array | ||||||||||
| 140 | 6 | return $self->encrypt_base64url( encode_json( $_[0] ) ); | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | sub thaw_base64url | ||||||||||
| 144 | 6 | ▶ | { | ||||||||
|
Subroutine
thaw_base64url: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 145 | 6 | my $self = shift; | |||||||||
| 146 | |||||||||||
| 147 | # reference to any data/scalar/hash/array | ||||||||||
| 148 | 6 | return __thaw_json( $self->decrypt_base64url( $_[0] ) ); | |||||||||
| 149 | } | ||||||||||
| 150 | |||||||||||
| 151 | ############################################################################## | ||||||||||
| 152 | |||||||||||
| 153 | =pod | ||||||||||
| 154 | |||||||||||
| 155 | |||||||||||
| 156 | =head1 NAME | ||||||||||
| 157 | |||||||||||
| 158 | Data::Tools::Crypto - authenticated symmetric and hybrid RSA encryption and signatures, one common API | ||||||||||
| 159 | |||||||||||
| 160 | =head1 SYNOPSIS | ||||||||||
| 161 | |||||||||||
| 162 | # Data::Tools::Crypto is the base class, it is never used on its own, | ||||||||||
| 163 | # use one of the implementations which inherit it: | ||||||||||
| 164 | |||||||||||
| 165 | use Data::Tools::Crypto::Symmetric; | ||||||||||
| 166 | use Data::Tools::Crypto::RSA; | ||||||||||
| 167 | |||||||||||
| 168 | my $crypto = Data::Tools::Crypto::Symmetric->new( $key ); | ||||||||||
| 169 | |||||||||||
| 170 | # -------------------------------------------------------------------------- | ||||||||||
| 171 | |||||||||||
| 172 | # raw binary data in, raw binary data out | ||||||||||
| 173 | |||||||||||
| 174 | my $ctext = $crypto->encrypt( $ptext ); | ||||||||||
| 175 | my $ptext = $crypto->decrypt( $ctext ); | ||||||||||
| 176 | |||||||||||
| 177 | # -------------------------------------------------------------------------- | ||||||||||
| 178 | |||||||||||
| 179 | # the same, in text-safe encodings | ||||||||||
| 180 | |||||||||||
| 181 | my $hex = $crypto->encrypt_hex( $ptext ); | ||||||||||
| 182 | my $ptext = $crypto->decrypt_hex( $hex ); | ||||||||||
| 183 | |||||||||||
| 184 | my $b64 = $crypto->encrypt_base64( $ptext ); | ||||||||||
| 185 | my $ptext = $crypto->decrypt_base64( $b64 ); | ||||||||||
| 186 | |||||||||||
| 187 | my $b64u = $crypto->encrypt_base64url( $ptext ); | ||||||||||
| 188 | my $ptext = $crypto->decrypt_base64url( $b64u ); | ||||||||||
| 189 | |||||||||||
| 190 | # -------------------------------------------------------------------------- | ||||||||||
| 191 | |||||||||||
| 192 | # any perl data structure, JSON-serialised and encrypted in one step | ||||||||||
| 193 | |||||||||||
| 194 | my $data = { name => 'test', list => [ 1, 2, 3 ] }; | ||||||||||
| 195 | |||||||||||
| 196 | my $sealed = $crypto->freeze( $data ); | ||||||||||
| 197 | my $data = $crypto->thaw( $sealed ); | ||||||||||
| 198 | |||||||||||
| 199 | my $sealed = $crypto->freeze_hex( $data ); | ||||||||||
| 200 | my $sealed = $crypto->freeze_base64( $data ); | ||||||||||
| 201 | my $sealed = $crypto->freeze_base64url( $data ); | ||||||||||
| 202 | |||||||||||
| 203 | # -------------------------------------------------------------------------- | ||||||||||
| 204 | |||||||||||
| 205 | =head1 DESCRIPTION | ||||||||||
| 206 | |||||||||||
| 207 | Data::Tools::Crypto provides authenticated encryption, hybrid public key | ||||||||||
| 208 | encryption and digital signatures, with a single common API: | ||||||||||
| 209 | |||||||||||
| 210 | * Data::Tools::Crypto::Symmetric -- shared secret key, ChaCha20-Poly1305 | ||||||||||
| 211 | * Data::Tools::Crypto::RSA -- public/private key pair, hybrid, signatures | ||||||||||
| 212 | |||||||||||
| 213 | This module is the base class of both. It implements no encryption of its own, | ||||||||||
| 214 | it defines the API which every implementation offers and provides the encoding | ||||||||||
| 215 | and serialisation wrappers on top of the encrypt() and decrypt() methods that | ||||||||||
| 216 | the inheriting module must provide. | ||||||||||
| 217 | |||||||||||
| 218 | Errors behave exactly as in the inheriting module, since every method here | ||||||||||
| 219 | goes through its encrypt() or decrypt(): caller errors raise an exception, | ||||||||||
| 220 | and data which does not decrypt makes decrypt_*() and thaw*() return undef. | ||||||||||
| 221 | |||||||||||
| 222 | The base class is not usable by itself and has nothing to export, always use | ||||||||||
| 223 | one of the implementations listed above. | ||||||||||
| 224 | |||||||||||
| 225 | freeze/thaw use JSON as a safer option than Storable, so only plain data can | ||||||||||
| 226 | be carried: hashes, arrays, scalars and numbers. Blessed objects, code | ||||||||||
| 227 | references and circular structures cannot be serialised. | ||||||||||
| 228 | |||||||||||
| 229 | =head1 METHODS | ||||||||||
| 230 | |||||||||||
| 231 | =head2 encrypt( $plaintext ), decrypt( $cryptotext ) | ||||||||||
| 232 | |||||||||||
| 233 | Implemented by the inheriting module, not by the base class. All the methods below are | ||||||||||
| 234 | built on top of these two. | ||||||||||
| 235 | |||||||||||
| 236 | =head2 encrypt_hex( $plaintext ), decrypt_hex( $cryptotext ) | ||||||||||
| 237 | |||||||||||
| 238 | As encrypt()/decrypt() but the cryptotext is HEX encoded, so it is plain text | ||||||||||
| 239 | and safe to store or pass anywhere a binary string would not survive. | ||||||||||
| 240 | |||||||||||
| 241 | =head2 encrypt_base64( $plaintext ), decrypt_base64( $cryptotext ) | ||||||||||
| 242 | |||||||||||
| 243 | As encrypt()/decrypt() but the cryptotext is BASE64 encoded, on a single line | ||||||||||
| 244 | with no newlines added. | ||||||||||
| 245 | |||||||||||
| 246 | =head2 encrypt_base64url( $plaintext ), decrypt_base64url( $cryptotext ) | ||||||||||
| 247 | |||||||||||
| 248 | As encrypt_base64() but using the URL and filename safe BASE64 alphabet, so | ||||||||||
| 249 | the result can be used in a URL or a file name without further escaping. | ||||||||||
| 250 | |||||||||||
| 251 | =head2 freeze( $data_ref ), thaw( $cryptotext ) | ||||||||||
| 252 | |||||||||||
| 253 | freeze() serialises any plain perl data structure to JSON and encrypts it. | ||||||||||
| 254 | thaw() reverses this and returns the data structure back, or undef if the | ||||||||||
| 255 | cryptotext cannot be decrypted, exactly as decrypt() does. | ||||||||||
| 256 | |||||||||||
| 257 | =head2 freeze_hex(), freeze_base64(), freeze_base64url() and their thaw pairs | ||||||||||
| 258 | |||||||||||
| 259 | As freeze()/thaw() but with the cryptotext encoded as described above: | ||||||||||
| 260 | |||||||||||
| 261 | my $sealed = $crypto->freeze_hex( $data_ref ); | ||||||||||
| 262 | my $data = $crypto->thaw_hex( $sealed ); | ||||||||||
| 263 | |||||||||||
| 264 | =head1 REQUIRED MODULES | ||||||||||
| 265 | |||||||||||
| 266 | Data::Tools::Crypto uses: | ||||||||||
| 267 | |||||||||||
| 268 | * Data::Tools | ||||||||||
| 269 | * JSON | ||||||||||
| 270 | * MIME::Base64 | ||||||||||
| 271 | |||||||||||
| 272 | =head1 GITHUB REPOSITORY | ||||||||||
| 273 | |||||||||||
| 274 | https://github.com/cade-vs/perl-data-tools-crypto | ||||||||||
| 275 | |||||||||||
| 276 | git clone https://github.com/cade-vs/perl-data-tools-crypto.git | ||||||||||
| 277 | |||||||||||
| 278 | =head1 AUTHOR | ||||||||||
| 279 | |||||||||||
| 280 | Vladi Belperchinov-Shabanski "Cade" | ||||||||||
| 281 | <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||||||||||
| 282 | http://cade.noxrun.com/ | ||||||||||
| 283 | |||||||||||
| 284 | |||||||||||
| 285 | =cut | ||||||||||
| 286 | |||||||||||
| 287 | ############################################################################## | ||||||||||
| 288 | |||||||||||
| 289 | 6 | 1; | |||||||||