This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #501 from gdrooid/md5
Added MD5 IA and it's tests.
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |