.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
hash_plugin.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2# pylint: disable=missing-module-docstring
3
4import hashlib
5import re
6
7from flask_babel import gettext
8
9name = "Hash plugin"
10description = gettext("Converts strings to different hash digests.")
11default_on = True
12preference_section = 'query'
13query_keywords = ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']
14query_examples = 'sha512 The quick brown fox jumps over the lazy dog'
15
16parser_re = re.compile('(md5|sha1|sha224|sha256|sha384|sha512) (.*)', re.I)
17
18
19def post_search(_request, search):
20 # process only on first page
21 if search.search_query.pageno > 1:
22 return True
23 m = parser_re.match(search.search_query.query)
24 if not m:
25 # wrong query
26 return True
27
28 function, string = m.groups()
29 if not string.strip():
30 # end if the string is empty
31 return True
32
33 # select hash function
34 f = hashlib.new(function.lower())
35
36 # make digest from the given string
37 f.update(string.encode('utf-8').strip())
38 answer = function + " " + gettext('hash digest') + ": " + f.hexdigest()
39
40 # print result
41 search.result_container.answers.clear()
42 search.result_container.answers['hash'] = {'answer': answer}
43 return True
post_search(_request, search)