Commit ba33583b authored by Nils Goroll's avatar Nils Goroll

support plain text database input

parent 5da8b391
......@@ -34,6 +34,7 @@ use Carp;
use Crypt::RC4;
use Digest::MD5 (qw(md5));
use MIME::Base64;
use Fcntl qw(SEEK_SET);
# avoid dependency
# use Carp::Assert;
......@@ -380,31 +381,56 @@ sub load_classifier_db($$) {
open($fh, '<', $fname) || die 'cant open '.$fname.' '.$!;
binmode($fh);
my $crypt_data;
{
my $b64;
# files starting with a # are considered un-encrypted
my $plainmode;
die 'error reading '.$fname.' '.$!
unless (defined(read ($fh, $plainmode, 1)));
die 'seek error on '.$fname.' '.$!
unless (seek($fh, 0, SEEK_SET));
undef $plainmode if ($plainmode ne "#");
my $plain;
my $hash_plain;
unless (defined($plainmode)) {
my $crypt_data;
{
my $b64;
my $sz = (-s $fh);
my $res = read ($fh, $b64, $sz);
die 'error reading '.$fname.' '.$! if (! defined($res));
die 'only read '.$res.' bytes of '.$sz if ($res < $sz);
$crypt_data = decode_base64($b64);
die 'no data after base64 decode'
unless (defined($crypt_data) && (length($crypt_data)) > 0);
}
$plain = RC4(compute_key($secret), substr($crypt_data, 32));
die 'no data after decrypt'
unless (defined($plain) && (length($plain)) > 0);
# check hash
my $h_file = substr($crypt_data, 0, 32);
$hash_plain = unpack('H*', md5($plain));
die 'checksum error: file '.$h_file.' decrypt '.$hash_plain
if ($h_file ne $hash_plain);
} else {
my $sz = (-s $fh);
my $res = read ($fh, $b64, $sz);
my $res = read ($fh, $plain, $sz);
die 'error reading '.$fname.' '.$! if (! defined($res));
die 'only read '.$res.' bytes of '.$sz if ($res < $sz);
$crypt_data = decode_base64($b64);
die 'no data after base64 decode' unless (defined($crypt_data) && (length($crypt_data)) > 0)
$hash_plain = unpack('H*', md5($plain));
die 'no data from plain file'
unless (defined($plain) && (length($plain)) > 0);
}
close($fh);
my $decrypt = RC4(compute_key($secret), substr($crypt_data, 32));
my $h_file;
{
$h_file = substr($crypt_data, 0, 32);
my $h_decrypt = unpack('H*', md5($decrypt));
die 'checksum error: file '.$h_file.' decrypt '.$h_decrypt
if ($h_file ne $h_decrypt);
}
return wantarray ? (\$decrypt, $h_file) : \$decrypt;
return wantarray ? (\$plain, $hash_plain) : \$plain;
}
sub enum_name($$) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment