| 1 | package Apertur::SDK::Crypto; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | our @EXPORT_OK = qw(encrypt_image); | |||||||||
| 10 | |||||||||||
| 11 | 0 | ▶ | sub encrypt_image { | ||||||||
|
Subroutine
encrypt_image: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 12 | 0 | my ($image_data, $public_key_pem) = @_; | |||||||||
| 13 | |||||||||||
| 14 | # These modules are optional; only required when encryption is used. | ||||||||||
| 15 | 0 | ▶ | eval { require Crypt::OpenSSL::RSA } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | or die "Crypt::OpenSSL::RSA is required for encryption features. " | ||||||||||
| 17 | . "Install it with: cpanm Crypt::OpenSSL::RSA\n"; | ||||||||||
| 18 | 0 | ▶ | eval { require CryptX; require Crypt::AuthEnc::GCM; require Crypt::PRNG } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | or die "CryptX is required for encryption features. " | ||||||||||
| 20 | . "Install it with: cpanm CryptX\n"; | ||||||||||
| 21 | |||||||||||
| 22 | # Generate random AES-256 key (32 bytes) and IV (12 bytes) | ||||||||||
| 23 | 0 | my $aes_key = Crypt::PRNG::random_bytes(32); | |||||||||
| 24 | 0 | my $iv = Crypt::PRNG::random_bytes(12); | |||||||||
| 25 | |||||||||||
| 26 | # Encrypt image data with AES-256-GCM | ||||||||||
| 27 | 0 | my $gcm = Crypt::AuthEnc::GCM->new('AES', $aes_key, $iv); | |||||||||
| 28 | 0 | my $encrypted = $gcm->encrypt_add($image_data); | |||||||||
| 29 | 0 | my $auth_tag = $gcm->encrypt_done(); | |||||||||
| 30 | 0 | my $encrypted_with_tag = $encrypted . $auth_tag; | |||||||||
| 31 | |||||||||||
| 32 | # Wrap AES key with RSA-OAEP (SHA-256) | ||||||||||
| 33 | 0 | my $rsa = Crypt::OpenSSL::RSA->new_public_key($public_key_pem); | |||||||||
| 34 | 0 | $rsa->use_pkcs1_oaep_padding(); | |||||||||
| 35 | 0 | $rsa->use_sha256_hash(); | |||||||||
| 36 | 0 | my $wrapped_key = $rsa->encrypt($aes_key); | |||||||||
| 37 | |||||||||||
| 38 | return { | ||||||||||
| 39 | 0 | encrypted_key => encode_base64($wrapped_key, ''), | |||||||||
| 40 | iv => encode_base64($iv, ''), | ||||||||||
| 41 | encrypted_data => encode_base64($encrypted_with_tag, ''), | ||||||||||
| 42 | algorithm => 'RSA-OAEP+AES-256-GCM', | ||||||||||
| 43 | }; | ||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 1 | 1; | |||||||||
| 47 | |||||||||||
| 48 | __END__ | ||||||||||