3 #############################################################################
4 # This is a reference script to add checksums to downloadable #
5 # subscriptions. The checksum will be validated by AutoProxy on download #
6 # and checksum mismatches (broken downloads) will be rejected. #
8 # To add a checksum to a subscription file, run the script like this: #
10 # perl addChecksum.pl subscription.txt #
12 # Note: your subscription file should be saved in UTF-8 encoding, otherwise #
13 # the generated checksum might be incorrect. #
15 #############################################################################
19 use Digest
::MD5
qw(md5_base64);
21 use POSIX
qw(locale_h);
22 use POSIX
qw(strftime);
24 die "Usage: $^X $0 subscription.txt\n" unless @ARGV;
27 my $data = readFile
($file);
29 # Remove already existing checksum
30 $data =~ s/^.*!\s*checksum[\s\-:]+([\w\+\/=]+).*\n//gmi
;
33 setlocale
(LC_TIME
, "C");
34 my $timestamp = strftime
("%a, %d %b %Y %H:%M:%S %z", localtime(stat($file)->mtime));
35 $data =~ s/^!\s*Last Modified:.*$/! Last Modified: $timestamp/mi;
37 # Calculate new checksum: remove all CR symbols and empty
38 # lines and get an MD5 checksum of the result (base64-encoded,
39 # without the trailing = characters).
40 my $checksumData = $data;
41 $checksumData =~ s/\r//g;
42 $checksumData =~ s/\n+/\n/g;
44 # Calculate new checksum
45 my $checksum = md5_base64
($checksumData);
47 # Insert checksum into the file
48 $data =~ s/(\r?\n)/$1! Checksum: $checksum$1/;
50 writeFile
($file, $data);
56 open(local *FILE
, "<", $file) || die "Could not read file '$file'";
67 my ($file, $contents) = @_;
69 open(local *FILE
, ">", $file) || die "Could not write file '$file'";