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

Commit 320a607

Browse files
author
Jag Talon
committed
Merge pull request #501 from gdrooid/md5
Added MD5 IA and it's tests.
2 parents a30edc6 + 93213dd commit 320a607

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

dist.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Email::Valid = 1.192
3838
Net::Domain::TLD = 1.70
3939
Convert::Pluggable = 0.021
4040
YAML = 0
41+
Encode = 2.62
4142
; ParseCron
4243
Schedule::Cron::Events = 0
4344
Convert::Color = 0.08
@@ -51,6 +52,7 @@ Acme::rafl::Everywhere = 0.008
5152
Lingua::EN::Numbers::Ordinate = 1.02
5253
; Hashes
5354
Digest::SHA = 5.82
55+
Digest::MD5 = 2.53
5456
; Factors
5557
Math::Prime::Util = 0.34
5658
Games::Sudoku::Component = 0.02

lib/DDG/Goodie/MD5.pm

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package DDG::Goodie::MD5;
2+
# ABSTRACT: Calculate the MD5 digest of a string.
3+
4+
use DDG::Goodie;
5+
use Digest::MD5 qw(md5_base64 md5_hex);
6+
use Encode qw(encode);
7+
8+
zci answer_type => 'md5';
9+
zci is_cached => 1;
10+
11+
primary_example_queries 'md5 digest this!';
12+
secondary_example_queries 'duckduckgo md5',
13+
'md5sum the sum of a string';
14+
15+
name 'MD5';
16+
description 'Calculate the MD5 digest of a string.';
17+
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/MD5.pm';
18+
category 'transformations';
19+
20+
triggers startend => 'md5', 'md5sum';
21+
22+
my $css = share('style.css')->slurp;
23+
24+
sub html_output {
25+
my ($md5, $str) = @_;
26+
return "<style type='text/css'>$css</style>"
27+
."<div class='zci--md5'>"
28+
."<span class='text--secondary'>MD5 of \"$str\"</span><br/>"
29+
."<span class='text--primary'>$md5</span>"
30+
."</div>";
31+
}
32+
33+
handle remainder => sub {
34+
if (/^\s*(.*)\s*$/) {
35+
# Exit unless a string is found
36+
return unless $1;
37+
# The string is encoded to get the utf8 representation instead of
38+
# perls internal representation of strings, before it's passed to
39+
# the md5 subroutine.
40+
my $str = md5_hex (encode "utf8", $1);
41+
return $str, html => html_output ($str, $1);
42+
}
43+
};
44+
45+
1;

share/goodie/md5/style.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.zci--answer .zci--md5 {
2+
font-weight: 300;
3+
padding: .25em 0;
4+
}
5+
6+
.zci--md5 .text--primary {
7+
font-size: 1.5em;
8+
word-wrap: break-word;
9+
}
10+
11+
.zci--md5 .text--secondary {
12+
font-size: 1.1em;
13+
}

t/MD5.t

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
use Test::More;
6+
use DDG::Test::Goodie;
7+
8+
zci answer_type => 'md5';
9+
zci is_cached => 1;
10+
11+
ddg_goodie_test(
12+
[qw(
13+
DDG::Goodie::MD5
14+
)],
15+
'md5 digest this!' => test_zci('3838c8fb10a114e6d21203359ef147ad', html=>qr/.*/),
16+
'duckduckgo md5' => test_zci('96898bb8544fa56b03c08cdc09886c6c', html=>qr/.*/),
17+
'md5sum the sum of a string' => test_zci('a704c8833f9850cd342ead27207ca1a1', html=>qr/.*/),
18+
);
19+
20+
done_testing;

0 commit comments

Comments
 (0)