modified: spffq.py
[GalaxyCodeBases.git] / etc / agfw / autoproxy-gfwlist / validateChecksum.pl
bloba0efbf996f516ecb94577a956a5e0a89efcc80a8
1 #!/usr/bin/perl
3 #############################################################################
4 # This is a reference script to validate the checksum in downloadable #
5 # subscription. This performs the same validation as Adblock Plus when it #
6 # downloads the subscription. #
7 # #
8 # To validate a subscription file, run the script like this: #
9 # #
10 # perl validateChecksum.pl subscription.txt #
11 # #
12 # Note: your subscription file should be saved in UTF-8 encoding, otherwise #
13 # the validation result might be incorrect. #
14 # #
15 # 20100418: Stolen from ABP with minor modification for AutoProxy project #
16 #############################################################################
18 use strict;
19 use warnings;
20 use Digest::MD5 qw(md5_base64);
22 die "Usage: $^X $0 subscription.txt\n" unless @ARGV;
24 my $file = $ARGV[0];
25 my $data = readFile($file);
27 # Normalize data
28 $data =~ s/\r//g;
29 $data =~ s/\n+/\n/g;
31 # Extract checksum
33 # Remove checksum
34 $data =~ s/^\s*!\s*checksum[\s\-:]+([\w\+\/=]+).*\n//mi;
35 my $checksum = $1;
36 die "Error: couldn't find a checksum in the file\n" unless $checksum;
38 # Calculate new checksum
39 my $checksumExpected = md5_base64($data);
41 # Compare checksums
42 die "Error: invalid checksum\n" unless $checksum eq $checksumExpected;
44 sub readFile
46 my $file = shift;
48 open(local *FILE, "<", $file) || die "Error: could not read file '$file'";
49 binmode(FILE);
50 local $/;
51 my $result = <FILE>;
52 close(FILE);
54 return $result;