File Coverage

blib/lib/Module/CryptSource.pm
Criterion Covered Total %
statement 23 36 63.8
branch 3 10 30.0
condition 4 9 44.4
subroutine 8 8 100.0
pod n/a
total 38 63 60.3


line stmt bran cond sub pod time code
1             # $File: //member/autrijus/Module-CryptSource/CryptSource.pm $ $Author: autrijus $
2             # $Revision: #2 $ $Change: 685 $ $DateTime: 2002/08/17 08:59:09 $
3              
4             package Module::CryptSource;
5             $Module::CryptSource::VERSION = '0.02';
6              
7 1     1   877 use strict;
  1         2  
  1         51  
8              
9 1     1   1134 use Storable qw(thaw nfreeze);
  1         3352  
  1         73  
10 1     1   833 use MIME::Base64 qw(encode_base64 decode_base64);
  1         714  
  1         60  
11 1     1   3267 use Term::ReadKey qw(ReadMode);
  1         6599  
  1         89  
12 1     1   11 use Digest::MD5 qw(md5_hex);
  1         2  
  1         61  
13 1     1   1171 use Crypt::Rijndael;
  1         2180  
  1         1244  
14              
15             =head1 NAME
16              
17             Module::CryptSource - Encrypt and Decrypt source for Binary Packagers
18              
19             =head1 SYNOPSIS
20              
21             In myprog.pl:
22              
23             use Module::CryptSource;
24            
25             Afterwards:
26              
27             % perl myprog.pl --ensrc
28             % perlapp -f myprog.pl # or perl2exe
29             % myprog.exe --desrc
30              
31             =head1 DESCRIPTION
32              
33             One of the problems in binary packages such as perlapp or perl2exe is
34             the difficulty of uncovering the source code. While it can be argued
35             that obscuring source code may be necessary in some environments, it
36             could be disastrous when you accidentally lose the source code.
37              
38             This self-modifying module let you specify a key to encrypted source
39             code and modules, so you can use that key to retrieve them back.
40              
41             To use this module, simply put C in all scripts
42             and modules you wish to encrypt. Run it once with the C<--ensrc> option
43             before running B or B, so it can store the modified
44             F along with your F<.exe> file; copies of encrypted
45             source files will be stored in the F file.
46              
47             Run the resulting executable with C<--desrc> to decrypt the source files
48             back.
49              
50             =head1 CAVEATS
51              
52             B, B and B are three XS
53             modules you'll have to install it yourself. It's probably possible
54             to use B and C as alternatives, but
55             I don't have the motivation to investigate yet.
56              
57             If user enters a wrong decryption key, chances are that the
58             C will report an C error.
59              
60             =cut
61              
62             my %Files;
63              
64             sub import {
65 1 50 50 1   24 return $Files{(caller)[1]}++
      33        
66             if ($0 !~ /\.exe$/i) and (($ARGV[0] || '') eq '--ensrc');
67              
68 1 50 50     22 return unless ($ARGV[0] || '') eq '--desrc'; local *FH;
  0            
69              
70 0 0         %Files = %{+eval{thaw(unpack('N/a*', Crypt::Rijndael->new(md5_hex(
  0            
  0            
71             ReadMode(2), print("Enter the decryption key:"), scalar
72             ))->decrypt(decode_base64(SRC()))))} or die "\nDecryption failed!"};
73              
74 0           while (my ($file, $src) = each %Files) {
75 0           print "\nDecrypting $file...";
76 0           chmod 0644, $file;
77 0 0         open FH, ">$file" or (warn("Cannot write to $file: $!"), next);
78 0           print FH $src;
79 0           close FH;
80             }
81              
82 0           %Files = (); print "\n"; exit;
  0            
  0            
83             }
84              
85             INIT { if (%Files) {
86             local *FH;
87             my $src = do { local $/; open FH, __FILE__; };
88              
89             $src =~ s[# ]["# "]es; die "Encryption failed: $@" if $@;
99              
100             chmod 0644, __FILE__;
101             open FH, ">".__FILE__ or die $!;
102             print FH $src;
103             close FH;
104              
105             print "\n"; exit;
106             } };
107              
108 1 50 50 1   11 use constant SRC => (($ARGV[0] || '') ne '--desrc') ? '' : << '.'; #
111              
112             1;
113              
114             __END__