line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
302937
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
70
|
|
2
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
90
|
|
3
|
|
|
|
|
|
|
package OpusVL::SimpleCrypto; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# ABSTRACT: Very simple encryption methods. |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
1342
|
use Moo; |
|
2
|
|
|
|
|
27632
|
|
|
2
|
|
|
|
|
14
|
|
8
|
2
|
|
|
2
|
|
6913
|
use Crypt::Sodium; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use MIME::Base64; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.008'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has key_string => (is => 'rw', lazy => 1, builder => '_build_key_string'); |
14
|
|
|
|
|
|
|
has key => (is => 'ro', lazy => 1, builder => '_build_key'); |
15
|
|
|
|
|
|
|
has deterministic_salt => (is => 'ro', lazy => 1, builder => '_build_deterministic_salt'); |
16
|
|
|
|
|
|
|
has deterministic_salt_string => (is => 'ro', lazy => 1, builder => '_build_deterministic_salt_string'); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub _build_deterministic_salt |
19
|
|
|
|
|
|
|
{ |
20
|
|
|
|
|
|
|
my $self = shift; |
21
|
|
|
|
|
|
|
die 'Must specify deterministic_salt or deterministic_salt_string' unless $self->deterministic_salt_string; |
22
|
|
|
|
|
|
|
return decode_base64($self->deterministic_salt_string); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _build_deterministic_salt_string |
26
|
|
|
|
|
|
|
{ |
27
|
|
|
|
|
|
|
my $self = shift; |
28
|
|
|
|
|
|
|
die 'Must specify deterministic_salt or deterministic_salt_string' unless $self->deterministic_salt; |
29
|
|
|
|
|
|
|
return encode_base64($self->deterministic_salt); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _build_key |
33
|
|
|
|
|
|
|
{ |
34
|
|
|
|
|
|
|
my $self = shift; |
35
|
|
|
|
|
|
|
die 'Must specify key or key_string' unless $self->key_string; |
36
|
|
|
|
|
|
|
return decode_base64($self->key_string); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub _build_key_string |
40
|
|
|
|
|
|
|
{ |
41
|
|
|
|
|
|
|
my $self = shift; |
42
|
|
|
|
|
|
|
die 'Must specify key or key_string' unless $self->key; |
43
|
|
|
|
|
|
|
return encode_base64($self->key); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub GenerateKey |
47
|
|
|
|
|
|
|
{ |
48
|
|
|
|
|
|
|
my $k = crypto_stream_key(); |
49
|
|
|
|
|
|
|
my $salt = crypto_pwhash_salt(); |
50
|
|
|
|
|
|
|
return OpusVL::SimpleCrypto->new({ key => $k, deterministic_salt => $salt }); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub encrypt |
54
|
|
|
|
|
|
|
{ |
55
|
|
|
|
|
|
|
my $self = shift; |
56
|
|
|
|
|
|
|
my $message = shift; |
57
|
|
|
|
|
|
|
my $n = crypto_stream_nonce(); |
58
|
|
|
|
|
|
|
my $t = crypto_secretbox($message, $n, $self->key); |
59
|
|
|
|
|
|
|
return sprintf("%s:%s", encode_base64($n), encode_base64($t)); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub encrypt_deterministic |
63
|
|
|
|
|
|
|
{ |
64
|
|
|
|
|
|
|
my $self = shift; |
65
|
|
|
|
|
|
|
my $message = shift; |
66
|
|
|
|
|
|
|
my $salt = $self->deterministic_salt; |
67
|
|
|
|
|
|
|
my $n = crypto_pwhash_scrypt($message, $salt, crypto_stream_NONCEBYTES); |
68
|
|
|
|
|
|
|
my $t = crypto_secretbox($message, $n, $self->key); |
69
|
|
|
|
|
|
|
return sprintf("%s:%s", encode_base64($n), encode_base64($t)); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub decrypt |
73
|
|
|
|
|
|
|
{ |
74
|
|
|
|
|
|
|
my $self = shift; |
75
|
|
|
|
|
|
|
my $ciphertext = shift; |
76
|
|
|
|
|
|
|
my ($nonce, $cipher) = split /:/, $ciphertext; |
77
|
|
|
|
|
|
|
return crypto_secretbox_open(decode_base64($cipher), decode_base64($nonce), $self->key); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
1; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
__END__ |