Skip to content

Instantly share code, notes, and snippets.

@bes-internal
Created October 12, 2023 19:53
Show Gist options
  • Save bes-internal/370c856130e76ba97ec8e959056c7fdf to your computer and use it in GitHub Desktop.
Save bes-internal/370c856130e76ba97ec8e959056c7fdf to your computer and use it in GitHub Desktop.
perl5 of exim srs_encode
# SUPPORT_SRS native exim 4.96
# doc: ${srs_encode {SECRET} {$from} {$aliastld}}
# shell$ exim -be '${srs_encode{SECRET}{localpart@example.tld}{my.tld}}'
# # exim -be '\SRS0=${l_4:${hmac{md5}{SECRET}{${lc:localpart@example.tld}}}}=${base32:${eval:$tod_epoch/86400&0x3ff}}=example.tld=localpart@my.tld'
my ($localpart, $domain) = split /\@/, $from;
require Digest::HMAC_MD5;
my $SECRET = 'SECRET';
my $HHHH = substr(Digest::HMAC_MD5::hmac_md5_hex(lc $from, $SECRET), 0, 4);
my $TT;
my @base32_chars = split //, "abcdefghijklmnopqrstuvwxyz234567";
my $i = time() / 86400 & 0x3ff; # got lower 10 bit of number of days
$TT .= $base32_chars[$i >> 5]; # char from first 5 bits
$TT .= $base32_chars[$i & 0x1f]; # char from last 5 bits
my $fromsrs=qq^SRS0=$HHHH=$TT=$domain=$localpart\@$aliastld^;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment