add hex/base64 formatting option to md5 goodie

master
Jarmo Kivekas 2014-10-19 17:41:01 +03:00
parent 5bd74a54da
commit a5bf4b4f9a
2 changed files with 17 additions and 4 deletions

View File

@ -10,7 +10,8 @@ zci is_cached => 1;
primary_example_queries 'md5 digest this!';
secondary_example_queries 'duckduckgo md5',
'md5sum the sum of a string';
'md5sum the sum of a string',
'md5 base64 this string';
name 'MD5';
description 'Calculate the MD5 digest of a string.';
@ -32,6 +33,9 @@ sub html_output {
}
handle remainder => sub {
#Remove format specifier from e.g 'md5 base64 this'
s/^(hex|base64)\s+(.*\S+)/$2/;
my $format = $1 || '';
s/^hash\s+(.*\S+)/$1/; # Remove 'hash' in queries like 'md5 hash this'
s/^of\s+(.*\S+)/$1/; # Remove 'of' in queries like 'md5 hash of this'
s/^"(.*)"$/$1/; # Remove quotes
@ -39,8 +43,10 @@ handle remainder => sub {
# The string is encoded to get the utf8 representation instead of
# perls internal representation of strings, before it's passed to
# the md5 subroutine.
my $str = $1;
my $md5 = md5_hex(encode "utf8", $str);
my $str = encode("utf8",$1);
my $md5;
#use approprite output format, default to hex
$md5 = $format eq 'base64' ? md5_base64($str) : md5_hex($str);
return $md5, html => html_output($str, $md5);
} else {
# Exit unless a string is found

View File

@ -22,6 +22,13 @@ ddg_goodie_test(
'md5 hash ' => test_zci('0800fc577294c34e0b28ad2839435945', html=>qr/.*/),
'md5 hash of' => test_zci('8bf8854bebe108183caeb845c7676ae4', html=>qr/.*/),
'md5 hash of password ' => test_zci('5f4dcc3b5aa765d61d8327deb882cf99', html=>qr/.*/),
'md5 base64 duckduckgo' => test_zci('lomLuFRPpWsDwIzcCYhsbA', html=>qr/.*/),
'md5 base64 "duckduckgo"' => test_zci('lomLuFRPpWsDwIzcCYhsbA', html=>qr/.*/),
'md5 base64 hex' => test_zci('uNG0Pq5zWHula671dHCeyw', html=>qr/.*/),
'md5 hex duckduckgo' => test_zci('96898bb8544fa56b03c08cdc09886c6c', html=>qr/.*/),
'md5 hex "duckduckgo"' => test_zci('96898bb8544fa56b03c08cdc09886c6c', html=>qr/.*/),
'md5 hex base64' => test_zci('95a1446a7120e4af5c0c8878abb7e6d2', html=>qr/.*/),
'md5 base64 hash of duckduckgo' => test_zci('lomLuFRPpWsDwIzcCYhsbA', html=>qr/.*/),
);
done_testing;