line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::Scrypt; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
220581
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
125
|
|
4
|
3
|
|
|
3
|
|
18
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
103
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
19
|
use Carp qw(croak); |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
233
|
|
7
|
3
|
|
|
3
|
|
20
|
use XSLoader; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1751
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
10
|
|
|
|
|
|
|
our $XS_VERSION = $VERSION; |
11
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
XSLoader::load(__PACKAGE__, $XS_VERSION); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my @errors = ( |
16
|
|
|
|
|
|
|
'success', |
17
|
|
|
|
|
|
|
'getrlimit or sysctl(hw.usermem) failed', |
18
|
|
|
|
|
|
|
'clock_getres or clock_gettime failed', |
19
|
|
|
|
|
|
|
'error computing derived key', |
20
|
|
|
|
|
|
|
'could not read salt from /dev/urandom', |
21
|
|
|
|
|
|
|
'error in OpenSSL', |
22
|
|
|
|
|
|
|
'malloc failed', |
23
|
|
|
|
|
|
|
'data is not a valid scrypt-encrypted block', |
24
|
|
|
|
|
|
|
'unrecognized scrypt format', |
25
|
|
|
|
|
|
|
'decrypting input would take too much memory', |
26
|
|
|
|
|
|
|
'decrypting input would take too long', |
27
|
|
|
|
|
|
|
'key is incorrect', |
28
|
|
|
|
|
|
|
'error writing output file', |
29
|
|
|
|
|
|
|
'error reading input file', |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my %defaults = ( |
33
|
|
|
|
|
|
|
max_mem => 0, |
34
|
|
|
|
|
|
|
max_mem_frac => 0.125, |
35
|
|
|
|
|
|
|
max_time => 5, |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub new { |
39
|
3
|
|
|
3
|
1
|
12897
|
my ($class, @params) = @_; |
40
|
3
|
100
|
|
|
|
21
|
my %params = (@params % 2) ? (key => @params) : @params; |
41
|
|
|
|
|
|
|
|
42
|
3
|
100
|
|
|
|
247
|
croak q('key' is required) unless defined $params{key}; |
43
|
|
|
|
|
|
|
|
44
|
2
|
|
|
|
|
12
|
return bless \%params, $class; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub encrypt { |
48
|
3
|
|
|
3
|
1
|
2010
|
my ($self, $data, @params) = @_; |
49
|
3
|
50
|
|
|
|
27
|
my %params = (@params % 2) ? (key => @params) : @params; |
50
|
|
|
|
|
|
|
|
51
|
3
|
50
|
33
|
|
|
17
|
croak 'plaintext must be a scalar or scalar ref' |
52
|
|
|
|
|
|
|
if ref $data and 'SCALAR' ne ref $data; |
53
|
|
|
|
|
|
|
|
54
|
3
|
50
|
|
|
|
11
|
if (ref $self) { |
55
|
0
|
|
|
|
|
0
|
%params = (%defaults, %$self, %params); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
else { |
58
|
3
|
50
|
|
|
|
13
|
croak q('key' is required) unless defined $params{key}; |
59
|
3
|
|
|
|
|
26
|
%params = (%defaults, %params); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
3
|
|
|
|
|
13394749
|
my ($status, $out) = _encrypt( |
63
|
|
|
|
|
|
|
$data, @params{qw(key max_mem max_mem_frac max_time)}, |
64
|
|
|
|
|
|
|
); |
65
|
3
|
50
|
0
|
|
|
39
|
croak $errors[$status] || 'unknown scrypt error' if $status; |
66
|
|
|
|
|
|
|
|
67
|
3
|
|
|
|
|
69
|
return $out; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub decrypt { |
71
|
3
|
|
|
3
|
1
|
1895
|
my ($self, $data, @params) = @_; |
72
|
3
|
50
|
|
|
|
33
|
my %params = (@params % 2) ? (key => @params) : @params; |
73
|
|
|
|
|
|
|
|
74
|
3
|
50
|
33
|
|
|
97
|
croak 'ciphertext must be a scalar or scalar ref' |
75
|
|
|
|
|
|
|
if ref $data and 'SCALAR' ne ref $data; |
76
|
|
|
|
|
|
|
|
77
|
3
|
50
|
|
|
|
11
|
if (ref $self) { |
78
|
0
|
|
|
|
|
0
|
%params = (%defaults, %$self, %params); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
else { |
81
|
3
|
50
|
|
|
|
17
|
croak q('key' is required) unless defined $params{key}; |
82
|
3
|
|
|
|
|
35
|
%params = (%defaults, %params); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
3
|
|
|
|
|
8358349
|
my ($status, $out) = _decrypt( |
86
|
|
|
|
|
|
|
$data, @params{qw(key max_mem max_mem_frac max_time)}, |
87
|
|
|
|
|
|
|
); |
88
|
3
|
100
|
50
|
|
|
488
|
croak $errors[$status] || 'unknown scrypt error' if $status; |
89
|
|
|
|
|
|
|
|
90
|
2
|
|
|
|
|
36
|
return $out; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__END__ |