on master help sign in
[enh] add plugin converting strings into hash digests
commitc9593c8ffd6c015d8a453287af9bbb7862342492 authorVenca24 <Vaclav.Zouzalik@seznam.cz> dateSat 17 Mar 2018, 13:54 (9 years ago) parent5707fba
4 files changed+104−1
AUTHORS.rst+1−1 searx/plugins/__init__.py+2 searx/plugins/hash_plugin.py+54 tests/unit/test_plugins.py+47
AUTHORS.rst+1−1
@@ -96,7 +96,7 @@ generally made searx better:
9696- Alice Ferrazzi @aliceinwire
9797- @LiquidLemon
9898- @dadosch
99−- @Venca24
99+- Václav Zouzalík @Venca24
100100- @ZEROF
101101- Ivan Skytte Jørgensen @isj-privacore
102102- @miicha
searx/plugins/__init__.py+2
@@ -28,6 +28,7 @@ from searx import logger, settings, static_path
2828logger = logger.getChild('plugins')
2929
3030from searx.plugins import (oa_doi_rewrite,
31+ hash_plugin,
3132 https_rewrite,
3233 infinite_scroll,
3334 self_info,
@@ -163,6 +164,7 @@ def sha_sum(filename):
163164
164165plugins = PluginStore()
165166plugins.register(oa_doi_rewrite)
167+plugins.register(hash_plugin)
166168plugins.register(https_rewrite)
167169plugins.register(infinite_scroll)
168170plugins.register(self_info)
searx/plugins/hash_plugin.pynew file+54
@@ -0,0 +1,54 @@
1+'''
2+searx is free software: you can redistribute it and/or modify
3+it under the terms of the GNU Affero General Public License as published by
4+the Free Software Foundation, either version 3 of the License, or
5+(at your option) any later version.
6+
7+searx is distributed in the hope that it will be useful,
8+but WITHOUT ANY WARRANTY; without even the implied warranty of
9+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+GNU Affero General Public License for more details.
11+
12+You should have received a copy of the GNU Affero General Public License
13+along with searx. If not, see < http://www.gnu.org/licenses/ >.
14+
15+(C) 2015 by Adam Tauber, <asciimoo@gmail.com>
16+(C) 2018 by Vaclav Zouzalik
17+'''
18+
19+from flask_babel import gettext
20+import hashlib
21+import re
22+
23+name = "Hash plugin"
24+description = gettext("Converts strings to different hash digests.")
25+default_on = True
26+
27+parser_re = re.compile(b'(md5|sha1|sha224|sha256|sha384|sha512) (.*)', re.I)
28+
29+def post_search(request, search):
30+ # process only on first page
31+ if search.search_query.pageno > 1:
32+ return True
33+ m = parser_re.match(search.search_query.query)
34+ if not m:
35+ # wrong query
36+ return True
37+
38+ # end if the string is empty
39+ function, string = m.groups()
40+ if string.strip().__len__() == 0:
41+ return True
42+
43+ # select hash function
44+ f = hashlib.new(function.lower())
45+ #f = hash_function[function.lower()]
46+
47+ # make digest from the given string
48+ f.update(string.strip())
49+ digest = f.hexdigest()
50+
51+ # print result
52+ search.result_container.answers.clear()
53+ search.result_container.answers.add(function + " " + gettext('hash function') + ": " + digest)
54+ return True
tests/unit/test_plugins.py+47
@@ -83,3 +83,50 @@ class SelfIPTest(SearxTestCase):
8383 search = get_search_mock(query='What is my User-Agent?', pageno=2)
8484 store.call(store.plugins, 'post_search', request, search)
8585 self.assertFalse('user-agent' in search.result_container.answers)
86+
87+class HashPluginTest(SearxTestCase):
88+
89+ def test_PluginStore_init(self):
90+ store = plugins.PluginStore()
91+ store.register(plugins.hash_plugin)
92+
93+ self.assertTrue(len(store.plugins) == 1)
94+
95+ request = Mock(remote_addr='127.0.0.1')
96+ request.headers.getlist.return_value = []
97+
98+ # MD5
99+ search = get_search_mock(query=b'md5 test', pageno=1)
100+ store.call(store.plugins, 'post_search', request, search)
101+ self.assertTrue('md5 hash function: 098f6bcd4621d373cade4e832627b4f6' in search.result_container.answers)
102+
103+ search = get_search_mock(query=b'md5 test', pageno=2)
104+ store.call(store.plugins, 'post_search', request, search)
105+ self.assertFalse('md5 hash function: 098f6bcd4621d373cade4e832627b4f6' in search.result_container.answers)
106+
107+ # SHA1
108+ search = get_search_mock(query=b'sha1 test', pageno=1)
109+ store.call(store.plugins, 'post_search', request, search)
110+ self.assertTrue('sha1 hash function: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3' in search.result_container.answers)
111+
112+ # SHA224
113+ search = get_search_mock(query=b'sha224 test', pageno=1)
114+ store.call(store.plugins, 'post_search', request, search)
115+ self.assertTrue('sha224 hash function: 90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809' in search.result_container.answers)
116+
117+ # SHA256
118+ search = get_search_mock(query=b'sha256 test', pageno=1)
119+ store.call(store.plugins, 'post_search', request, search)
120+ self.assertTrue('sha256 hash function: 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08' in search.result_container.answers)
121+
122+ # SHA384
123+ search = get_search_mock(query=b'sha384 test', pageno=1)
124+ store.call(store.plugins, 'post_search', request, search)
125+ self.assertTrue('sha384 hash function: 768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b' \
126+ '7ef1ccb126255d196047dfedf17a0a9' in search.result_container.answers)
127+
128+ # SHA512
129+ search = get_search_mock(query=b'sha512 test', pageno=1)
130+ store.call(store.plugins, 'post_search', request, search)
131+ self.assertTrue('sha512 hash function: ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27a' \
132+'c185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff' in search.result_container.answers)