line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# Crypt::HCE_SHA |
3
|
|
|
|
|
|
|
# implements one way hash chaining encryption using SHA |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# $Id: HCE_SHA.pm,v 1.3 2000/02/19 03:47:11 eric Exp $ |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
package Crypt::HCE_SHA; |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
1170
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
66
|
|
11
|
2
|
|
|
2
|
|
10
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
172
|
|
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
1296
|
use Digest::SHA; |
|
2
|
|
|
|
|
7474
|
|
|
2
|
|
|
|
|
124
|
|
14
|
2
|
|
|
2
|
|
1294
|
use MIME::Base64; |
|
2
|
|
|
|
|
1386
|
|
|
2
|
|
|
|
|
134
|
|
15
|
2
|
|
|
2
|
|
16
|
use Carp; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
1594
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
require Exporter; |
18
|
|
|
|
|
|
|
require AutoLoader; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
@ISA = qw(Exporter AutoLoader); |
21
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
22
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
23
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
24
|
|
|
|
|
|
|
@EXPORT = qw( |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
$VERSION = '0.75'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub new { |
31
|
4
|
|
|
4
|
0
|
6006126
|
my $class = shift; |
32
|
4
|
|
|
|
|
12
|
my $self = {}; |
33
|
|
|
|
|
|
|
|
34
|
4
|
|
|
|
|
9
|
bless $self, $class; |
35
|
|
|
|
|
|
|
|
36
|
4
|
50
|
33
|
|
|
19
|
if ((@_ != 2) && (@_ != 3)) { |
37
|
0
|
|
|
|
|
0
|
croak "Error: must be invoked HCE_SHA->new(key, random_thing, [algorithm to use (1, 224, 256, 384, 512)])"; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
4
|
|
|
|
|
24
|
$self->{SKEY} = shift(@_); |
41
|
4
|
|
|
|
|
15
|
$self->{RKEY} = shift(@_); |
42
|
4
|
50
|
|
|
|
10
|
if (@_ > 0) { |
43
|
0
|
|
|
|
|
0
|
$self->{BITS} = shift(@_); |
44
|
|
|
|
|
|
|
} else { |
45
|
4
|
|
|
|
|
6
|
$self->{BITS} = 1; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
4
|
|
|
|
|
12
|
return $self; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _new_key { |
52
|
52
|
|
|
52
|
|
46
|
my $self = shift; |
53
|
52
|
|
|
|
|
54
|
my ($rnd) = @_; |
54
|
|
|
|
|
|
|
|
55
|
52
|
|
|
|
|
119
|
my $context = new Digest::SHA->new($self->{BITS}); |
56
|
52
|
|
|
|
|
733
|
$context->add($self->{SKEY}, $rnd); |
57
|
52
|
|
|
|
|
182
|
my $digest = $context->digest(); |
58
|
52
|
|
|
|
|
171
|
my @e_block = unpack('C*', $digest); |
59
|
52
|
|
|
|
|
260
|
return @e_block; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub hce_block_encrypt { |
63
|
8
|
|
|
8
|
0
|
18
|
my $self = shift; |
64
|
8
|
|
|
|
|
13
|
my ($data) = @_; |
65
|
8
|
|
|
|
|
10
|
my ($i, $key, $data_size, $ans, $mod, @e_block, @data, @key, @ans); |
66
|
|
|
|
|
|
|
|
67
|
8
|
|
|
|
|
34
|
@key = unpack ('C*', $self->{SKEY}); |
68
|
8
|
|
|
|
|
46
|
@data = unpack ('C*', $data); |
69
|
|
|
|
|
|
|
|
70
|
8
|
|
|
|
|
17
|
undef @ans; |
71
|
8
|
|
|
|
|
20
|
@e_block = $self->_new_key($self->{RKEY}); |
72
|
8
|
|
|
|
|
11
|
$data_size = scalar(@data); |
73
|
8
|
|
|
|
|
17
|
for($i=0; $i < $data_size; $i++) { |
74
|
446
|
|
|
|
|
263
|
$mod = $i % 20; |
75
|
446
|
100
|
100
|
|
|
583
|
if (($mod == 0) && ($i > 19)) { |
76
|
18
|
|
|
|
|
69
|
@e_block = $self->_new_key(pack 'C*', (@ans)[($i-20)..($i-1)]); |
77
|
|
|
|
|
|
|
} |
78
|
446
|
|
|
|
|
602
|
$ans[$i] = $e_block[$mod] ^ $data[$i]; |
79
|
|
|
|
|
|
|
} |
80
|
8
|
|
|
|
|
27
|
$ans = pack 'C*', @ans; |
81
|
8
|
|
|
|
|
32
|
return $ans; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub hce_block_decrypt { |
85
|
8
|
|
|
8
|
0
|
11
|
my $self = shift; |
86
|
8
|
|
|
|
|
10
|
my ($data) = @_; |
87
|
8
|
|
|
|
|
10
|
my ($i, $key, $data_size, $ans, $mod, @e_block, @data, @key, @ans); |
88
|
|
|
|
|
|
|
|
89
|
8
|
|
|
|
|
25
|
@key = unpack ('C*', $self->{SKEY}); |
90
|
8
|
|
|
|
|
71
|
@data = unpack ('C*', $data); |
91
|
|
|
|
|
|
|
|
92
|
8
|
|
|
|
|
18
|
undef @ans; |
93
|
8
|
|
|
|
|
17
|
@e_block = $self->_new_key($self->{RKEY}); |
94
|
8
|
|
|
|
|
13
|
$data_size = scalar(@data); |
95
|
8
|
|
|
|
|
21
|
for($i=0; $i < $data_size; $i++) { |
96
|
446
|
|
|
|
|
245
|
$mod = $i % 20; |
97
|
446
|
100
|
100
|
|
|
681
|
if (($mod == 0) && ($i > 19)) { |
98
|
18
|
|
|
|
|
76
|
@e_block = $self->_new_key(pack 'C*', (@data)[($i-20)..($i-1)]); |
99
|
|
|
|
|
|
|
} |
100
|
446
|
|
|
|
|
756
|
$ans[$i] = $e_block[$mod] ^ $data[$i]; |
101
|
|
|
|
|
|
|
} |
102
|
8
|
|
|
|
|
31
|
$ans = pack 'C*', @ans; |
103
|
8
|
|
|
|
|
33
|
return $ans; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub hce_block_encode_mime { |
107
|
6
|
|
|
6
|
0
|
251
|
my $self = shift; |
108
|
6
|
|
|
|
|
10
|
my ($data) = @_; |
109
|
|
|
|
|
|
|
|
110
|
6
|
|
|
|
|
14
|
my $new_data = $self->hce_block_encrypt($data); |
111
|
6
|
|
|
|
|
49
|
my $encode = encode_base64($new_data, ""); |
112
|
6
|
|
|
|
|
11
|
return $encode; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub hce_block_decode_mime { |
116
|
6
|
|
|
6
|
0
|
2171
|
my $self = shift; |
117
|
6
|
|
|
|
|
11
|
my ($data) = @_; |
118
|
|
|
|
|
|
|
|
119
|
6
|
|
|
|
|
20
|
my $decode = decode_base64($data); |
120
|
6
|
|
|
|
|
19
|
my $new_data = $self->hce_block_decrypt($decode); |
121
|
6
|
|
|
|
|
13
|
return $new_data; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# Autoload methods go after =cut, and are processed by the autosplit program. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; |
127
|
|
|
|
|
|
|
__END__ |