Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #501 from gdrooid/md5
Browse files Browse the repository at this point in the history
Added MD5 IA and it's tests.
  • Loading branch information
Jag Talon committed Jul 1, 2014
2 parents a30edc6 + 93213dd commit 320a607
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Email::Valid = 1.192
Net::Domain::TLD = 1.70
Convert::Pluggable = 0.021
YAML = 0
Encode = 2.62
; ParseCron
Schedule::Cron::Events = 0
Convert::Color = 0.08
Expand All @@ -51,6 +52,7 @@ Acme::rafl::Everywhere = 0.008
Lingua::EN::Numbers::Ordinate = 1.02
; Hashes
Digest::SHA = 5.82
Digest::MD5 = 2.53
; Factors
Math::Prime::Util = 0.34
Games::Sudoku::Component = 0.02
Expand Down
45 changes: 45 additions & 0 deletions lib/DDG/Goodie/MD5.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package DDG::Goodie::MD5;
# ABSTRACT: Calculate the MD5 digest of a string.

use DDG::Goodie;
use Digest::MD5 qw(md5_base64 md5_hex);
use Encode qw(encode);

zci answer_type => 'md5';
zci is_cached => 1;

primary_example_queries 'md5 digest this!';
secondary_example_queries 'duckduckgo md5',
'md5sum the sum of a string';

name 'MD5';
description 'Calculate the MD5 digest of a string.';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/MD5.pm';
category 'transformations';

triggers startend => 'md5', 'md5sum';

my $css = share('style.css')->slurp;

sub html_output {
my ($md5, $str) = @_;
return "<style type='text/css'>$css</style>"
."<div class='zci--md5'>"
."<span class='text--secondary'>MD5 of \"$str\"</span><br/>"
."<span class='text--primary'>$md5</span>"
."</div>";
}

handle remainder => sub {
if (/^\s*(.*)\s*$/) {
# Exit unless a string is found
return unless $1;
# 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 = md5_hex (encode "utf8", $1);
return $str, html => html_output ($str, $1);
}
};

1;
13 changes: 13 additions & 0 deletions share/goodie/md5/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.zci--answer .zci--md5 {
font-weight: 300;
padding: .25em 0;
}

.zci--md5 .text--primary {
font-size: 1.5em;
word-wrap: break-word;
}

.zci--md5 .text--secondary {
font-size: 1.1em;
}
20 changes: 20 additions & 0 deletions t/MD5.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;

zci answer_type => 'md5';
zci is_cached => 1;

ddg_goodie_test(
[qw(
DDG::Goodie::MD5
)],
'md5 digest this!' => test_zci('3838c8fb10a114e6d21203359ef147ad', html=>qr/.*/),
'duckduckgo md5' => test_zci('96898bb8544fa56b03c08cdc09886c6c', html=>qr/.*/),
'md5sum the sum of a string' => test_zci('a704c8833f9850cd342ead27207ca1a1', html=>qr/.*/),
);

done_testing;

0 comments on commit 320a607

Please sign in to comment.