diff options
-rw-r--r-- | Echolot/Tools.pm | 63 |
1 files changed, 52 insertions, 11 deletions
diff --git a/Echolot/Tools.pm b/Echolot/Tools.pm index 06c6638..2c82ac7 100644 --- a/Echolot/Tools.pm +++ b/Echolot/Tools.pm @@ -1,7 +1,7 @@ package Echolot::Tools; # (c) 2002 Peter Palfrader <peter@palfrader.org> -# $Id: Tools.pm,v 1.1 2002/06/05 04:05:40 weasel Exp $ +# $Id: Tools.pm,v 1.2 2002/06/11 09:54:17 weasel Exp $ # =pod @@ -49,6 +49,55 @@ sub make_random($;%) { return $random; }; +sub make_mac($) { + my ($token) = @_; + + my $mac = hash($token . Echolot::Globals::get()->{'storage'}->get_secret() ); + return $mac; +}; + +sub verify_mac($$) { + my ($token, $mac) = @_; + + return (hash($token . Echolot::Globals::get()->{'storage'}->get_secret() ) eq $mac); +}; + +sub make_address($) { + my ($subsystem) = @_; + + my $token = $subsystem.'='.time(); + my $hash = hash($token . Echolot::Globals::get()->{'storage'}->get_secret() ); + my $cut_hash = substr($hash, 0, Echolot::Config::get()->{'hash_len'}); + my $complete_token = $token.'='.$cut_hash; + my $address = Echolot::Config::get()->{'my_localpart'}. + Echolot::Config::get()->{'recipient_delimiter'}. + $complete_token. + '@'. + Echolot::Config::get()->{'my_domain'}; + + return $address; +}; + +sub verify_address_tokens($) { + my ($address) = @_; + + my $delimiter = quotemeta( Echolot::Config::get()->{'recipient_delimiter'}); + my ($type, $timestamp, $received_hash) = $address =~ /$delimiter (.*) = (\d+) = ([0-9a-f]+) @/x or + cluck("Could not parse to header '$address'"), + return undef; + + my $token = $type.'='.$timestamp; + my $hash = Echolot::Tools::hash($token . Echolot::Globals::get()->{'storage'}->get_secret() ); + my $cut_hash = substr($hash, 0, Echolot::Config::get()->{'hash_len'}); + + ($cut_hash eq $received_hash) or + cluck("Hash mismatch in '$address'"), + return undef; + + return + { timestamp => $timestamp, + token => $type }; +}; sub send_message(%) { my (%args) = @_; @@ -59,17 +108,9 @@ sub send_message(%) { $args{'Subject'} = '' unless (defined $args{'Subject'}); $args{'Body'} = '' unless (defined $args{'Body'}); if (defined $args{'Token'}) { - my $token = $args{'Token'}.'='.time(); - my $hash = hash($token . Echolot::Globals::get()->{'storage'}->get_secret() ); - my $cut_hash = substr($hash, 0, Echolot::Config::get()->{'hash_len'}); - my $complete_token = $token.'='.$cut_hash; - $args{'From'} = - Echolot::Config::get()->{'my_localpart'}. - Echolot::Config::get()->{'recipient_delimiter'}. - $complete_token. - '@'. - Echolot::Config::get()->{'my_domain'}; + $args{'From'} = make_address( $args{'Token'} ); } else { + cluck ("Sending messages without tokens?"); $args{'From'} = Echolot::Config::get()->{'my_localpart'}. '@'. |