blib/lib/Crypt/OICQ.pm

filter: Statementstmt 95.1% 79 / 83 Branchbran 85.7% 12 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 6 totaltotal 85.7% 102 / 119 CC 9 SCAR 9.9
CC
9
Cov
92%
decrypt
5.2
encrypt
3.0
new
2.0
CRAP 2.7
↑ Summary
1 package Crypt::OICQ;
2  
3 # $Id: OICQ.pm,v 1.4 2006/01/20 21:31:27 tans Exp $
4  
5 # Copyright (c) 2002-2006 Shufeng Tan. All rights reserved.
6 #
7 # This package is free software and is provided "as is" without express
8 # or implied warranty. It may be used, redistributed and/or modified
9 # under the terms of the Perl Artistic License (see
10 # http://www.perl.com/perl/misc/Artistic.html)
11  
12 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our $VERSION = '1.1';
17  
18 1 our @ISA = qw(Exporter);
19 1 our @EXPORT_OK = qw(encrypt decrypt);
20  
21 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
22 0 my ($class) = @_;
23 0 bless {}, $class;
24 }
25  
26 1 my $TEA_ROUNDS = 0x10;
27 1 my $TEA_DELTA = 0x9E3779B9;
28 1 my $TEA_SUM = 0xE3779B90;
29  
30 1077498 ▶sub tea_decrypt {
Subroutine tea_decrypt: called CC=2 SCAR=6.9
Pod undocumented
31 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1077498 my ($c_block, $key) = @_;
33 1077498 my ($y, $z) = unpack("NN", $c_block);
34 1077498 my ($a, $b, $c, $d) = unpack("NNNN", $key);
35 1077498 my $sum = $TEA_SUM;
36 1077498 my $n = $TEA_ROUNDS;
37 1077498 ▶ while ($n-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n-- > 0) 17239968 1077498 18317466
38 17239968 $z -= ($y<<4)+$c ^ $y+$sum ^ (0x07ffffff & ($y>>5))+$d;
39 17239968 $y -= ($z<<4)+$a ^ $z+$sum ^ (0x07ffffff & ($z>>5))+$b;
40 17239968 $sum -= $TEA_DELTA;
41 }
42 1077498 pack("NN", $y, $z);
43 }
44  
45 16441 ▶sub decrypt {
Subroutine decrypt: called CC=5 SCAR=16.5
Pod undocumented
46 16441 my ($self, $crypt, $key) = @_;
47  
48 16441 my $crypt_len = length($crypt);
49 16441 ▶ if (($crypt_len % 8) || ($crypt_len < 16)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crypt_len % 8 or $crypt_len < 16) 0 16441 16441
Condition33%1/3
$crypt_len % 8 or $crypt_len < 16
l!l&&r!l&&!r
0016441
MC/DC0%0/2
$crypt_len % 8 or $crypt_len < 16
$crypt_len % 8 $crypt_len < 16
Truth table33%1/3
A or B
A= $crypt_len % 8B= $crypt_len < 16
ABresult
1X1
011
000
50 0 die "Crypt::OICQ::decrypt error: invalid input length $crypt_len\n";
51 }
52  
53 16441 my $c_buf = substr($crypt, 0, 8);
54 16441 my $p_buf = tea_decrypt($c_buf, $key);
55 16441 my $pad_len = ord(substr($p_buf, 0, 1) & "\007");
56 16441 my $plain_len = $crypt_len - $pad_len - 10;
57 16441 my $plain = $p_buf;
58 16441 my $pre_plain = $p_buf;
59 16441 my $pre_crypt = $c_buf;
60  
61 16441 ▶ for (my $i = 8; $i < $crypt_len; $i += 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $crypt_len) 1061057 16441 1077498
62 1061057 $c_buf = substr($crypt, $i, 8);
63 1061057 $p_buf = tea_decrypt($c_buf ^ $pre_plain, $key);
64 1061057 $pre_plain = $p_buf;
65 1061057 $p_buf ^= $pre_crypt;
66 1061057 $plain .= $p_buf;
67 1061057 $pre_crypt = $c_buf;
68 }
69 16441 ▶ if (substr($plain, -7, 7) ne "\0\0\0\0\0\0\0") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($plain, -7, 7) ne "\0\0\0\0\0\0\0") 0 16441 16441
70 0 die "Crypt::OICQ::decrypt error: data dumped\n",
71 "crypt: ", unpack("H*", $crypt), "\n",
72 "key: ", unpack("H*", $key), "\n",
73 "plain: ", unpack("H*", $plain), "\n";
74 }
75 16441 return substr($plain, -7-$plain_len, $plain_len);
76 }
77  
78 1077412 ▶sub tea_encrypt {
Subroutine tea_encrypt: called CC=2 SCAR=6.9
Pod undocumented
79 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1077412 my ($p_block, $key) = @_;
81 1077412 my ($y, $z) = unpack("NN", $p_block);
82 1077412 my ($a, $b, $c, $d) = unpack("NNNN", $key);
83 1077412 my $sum = 0;
84 1077412 my $n = $TEA_ROUNDS;
85 1077412 ▶ while ($n-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n-- > 0) 17238592 1077412 18316004
86 17238592 $sum += $TEA_DELTA;
87 17238592 $y += ($z<<4)+$a ^ $z+$sum ^ (0x07ffffff & ($z>>5))+$b;
88 17238592 $z += ($y<<4)+$c ^ $y+$sum ^ (0x07ffffff & ($y>>5))+$d;
89 }
90 1077412 pack("NN", $y, $z);
91 }
92  
93 16429 ▶sub encrypt {
Subroutine encrypt: called CC=3 SCAR=11.0
Pod undocumented
94 16429 my ($self, $plain, $key) = @_;
95 16429 my $plain_len = length($plain);
96 16429 my $head_pad_len = ($plain_len + 10) % 8;
97 16429 ▶ $head_pad_len = 8 - $head_pad_len if $head_pad_len;
Branch: 1 branch
BranchTrueFalseTotal
if $head_pad_len 14378 2051 16429
98 16429 my $padded_plain = chr(0xa8 + $head_pad_len) .
99 rand_str(2+$head_pad_len) .
100 #(chr(0xad) x (2 + $head_pad_len)) .
101 $plain . ("\0" x 7);
102 16429 my $padded_plain_len = length($padded_plain);
103 16429 my $crypt = "";
104 16429 my $pre_plain = "\0" x 8;
105 16429 my $pre_crypt = $pre_plain;
106 16429 ▶ for (my $i = 0; $i < $padded_plain_len; $i += 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $padded_plain_len) 1077412 16429 1093841
107 1077412 my $p_buf = substr($padded_plain, $i, 8) ^ $pre_crypt;
108 1077412 my $c_buf = tea_encrypt($p_buf, $key);
109 1077412 $c_buf ^= $pre_plain;
110 1077412 $crypt .= $c_buf;
111 1077412 $pre_crypt = $c_buf;
112 1077412 $pre_plain = $p_buf;
113 }
114 16429 return $crypt;
115 }
116  
117 32829 ▶sub rand_str {
Subroutine rand_str: called CC=1 SCAR=0.0
Pod undocumented
118 32829 my $len = pop;
119 32829 join('', map(pack("C", rand(0xff)), 1..$len));
120 }
121  
122 1 1;
123  
124 __END__
↑ Summary