Filename | /home/micha/Projekt/spreadsheet-parsexlsx/lib/Spreadsheet/ParseXLSX/Decryptor/Standard.pm |
Statements | Executed 7 statements in 338µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 12µs | 13µs | BEGIN@3 | Spreadsheet::ParseXLSX::Decryptor::Standard::
1 | 1 | 1 | 5µs | 52µs | BEGIN@10 | Spreadsheet::ParseXLSX::Decryptor::Standard::
1 | 1 | 1 | 3µs | 18µs | BEGIN@4 | Spreadsheet::ParseXLSX::Decryptor::Standard::
0 | 0 | 0 | 0s | 0s | _generateDecryptionKey | Spreadsheet::ParseXLSX::Decryptor::Standard::
0 | 0 | 0 | 0s | 0s | decrypt | Spreadsheet::ParseXLSX::Decryptor::Standard::
0 | 0 | 0 | 0s | 0s | decryptFile | Spreadsheet::ParseXLSX::Decryptor::Standard::
0 | 0 | 0 | 0s | 0s | verifyPassword | Spreadsheet::ParseXLSX::Decryptor::Standard::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Spreadsheet::ParseXLSX::Decryptor::Standard; | ||||
2 | |||||
3 | 2 | 17µs | 2 | 14µs | # spent 13µs (12+1) within Spreadsheet::ParseXLSX::Decryptor::Standard::BEGIN@3 which was called:
# once (12µs+1µs) by Spreadsheet::ParseXLSX::Decryptor::BEGIN@18 at line 3 # spent 13µs making 1 call to Spreadsheet::ParseXLSX::Decryptor::Standard::BEGIN@3
# spent 1µs making 1 call to strict::import |
4 | 2 | 16µs | 2 | 33µs | # spent 18µs (3+15) within Spreadsheet::ParseXLSX::Decryptor::Standard::BEGIN@4 which was called:
# once (3µs+15µs) by Spreadsheet::ParseXLSX::Decryptor::BEGIN@18 at line 4 # spent 18µs making 1 call to Spreadsheet::ParseXLSX::Decryptor::Standard::BEGIN@4
# spent 15µs making 1 call to warnings::import |
5 | |||||
6 | # VERSION | ||||
7 | |||||
8 | # ABSTRACT: standard decryption | ||||
9 | |||||
10 | 2 | 304µs | 2 | 99µs | # spent 52µs (5+47) within Spreadsheet::ParseXLSX::Decryptor::Standard::BEGIN@10 which was called:
# once (5µs+47µs) by Spreadsheet::ParseXLSX::Decryptor::BEGIN@18 at line 10 # spent 52µs making 1 call to Spreadsheet::ParseXLSX::Decryptor::Standard::BEGIN@10
# spent 47µs making 1 call to base::import |
11 | |||||
12 | sub decrypt { | ||||
13 | my $self = shift; | ||||
14 | my ($encryptedValue) = @_; | ||||
15 | |||||
16 | my $key = $self->_generateDecryptionKey("\x00" x 4); | ||||
17 | my $ecb = Crypt::Mode::ECB->new($self->{cipherAlgorithm}, 0); | ||||
18 | return $ecb->decrypt($encryptedValue, $key); | ||||
19 | } | ||||
20 | |||||
21 | sub decryptFile { | ||||
22 | my $self = shift; | ||||
23 | my ($inFile, $outFile, $bufferLength, $fileSize) = @_; | ||||
24 | |||||
25 | my $key = $self->_generateDecryptionKey("\x00" x 4); | ||||
26 | my $ecb = Crypt::Mode::ECB->new($self->{cipherAlgorithm}, 0); | ||||
27 | |||||
28 | my $inbuf; | ||||
29 | my $i = 0; | ||||
30 | |||||
31 | while (($fileSize > 0) && (my $inlen = $inFile->read($inbuf, $bufferLength))) { | ||||
32 | if ($inlen < $bufferLength) { | ||||
33 | $inbuf .= "\x00" x ($bufferLength - $inlen); | ||||
34 | } | ||||
35 | |||||
36 | my $outbuf = $ecb->decrypt($inbuf, $key); | ||||
37 | if ($fileSize < $inlen) { | ||||
38 | $inlen = $fileSize; | ||||
39 | } | ||||
40 | |||||
41 | $outFile->write($outbuf, $inlen); | ||||
42 | $i++; | ||||
43 | $fileSize -= $inlen; | ||||
44 | } | ||||
45 | } | ||||
46 | |||||
47 | sub _generateDecryptionKey { | ||||
48 | my $self = shift; | ||||
49 | my ($blockKey) = @_; | ||||
50 | |||||
51 | my $hash; | ||||
52 | unless ($self->{pregeneratedKey}) { | ||||
53 | $hash = $self->{hashProc}->($self->{salt} . Encode::encode('UTF-16LE', $self->{password})); | ||||
54 | for (my $i = 0 ; $i < $self->{spinCount} ; $i++) { | ||||
55 | $hash = $self->{hashProc}->(pack('L<', $i) . $hash); | ||||
56 | } | ||||
57 | $self->{pregeneratedKey} = $hash; | ||||
58 | } | ||||
59 | |||||
60 | $hash = $self->{hashProc}->($self->{pregeneratedKey} . $blockKey); | ||||
61 | |||||
62 | my $x1 = $self->{hashProc}->(("\x36" x 64) ^ $hash); | ||||
63 | if (length($x1) >= $self->{keyLength}) { | ||||
64 | $hash = substr($x1, 0, $self->{keyLength}); | ||||
65 | } else { | ||||
66 | my $x2 = $self->{hashProc}->(("\x5C" x 64) ^ $hash); | ||||
67 | $hash = substr($x1 . $x2, 0, $self->{keyLength}); | ||||
68 | } | ||||
69 | |||||
70 | return $hash; | ||||
71 | } | ||||
72 | |||||
73 | sub verifyPassword { | ||||
74 | my $self = shift; | ||||
75 | my ($encryptedVerifier, $encryptedVerifierHash) = @_; | ||||
76 | |||||
77 | my $verifier = $self->decrypt($encryptedVerifier); | ||||
78 | my $verifierHash = $self->decrypt($encryptedVerifierHash); | ||||
79 | |||||
80 | my $verifierHash0 = $self->{hashProc}->($verifier); | ||||
81 | |||||
82 | die "Wrong password: $self" unless ($verifierHash0 eq substr($verifierHash, 0, length($verifierHash0))); | ||||
83 | } | ||||
84 | |||||
85 | =begin Pod::Coverage | ||||
86 | |||||
87 | decrypt | ||||
88 | decryptFile | ||||
89 | verifyPassword | ||||
90 | |||||
91 | =end Pod::Coverage | ||||
92 | |||||
93 | =cut | ||||
94 | |||||
95 | 1 | 2µs | 1; |