.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, missing-class-docstring
3import typing
4
5import re
6import hashlib
7
8from flask_babel import gettext
9
10from searx.plugins import Plugin, PluginInfo
11from searx.result_types import EngineResults
12
13if typing.TYPE_CHECKING:
14 from searx.search import SearchWithPlugins
15 from searx.extended_types import SXNG_Request
16 from searx.plugins import PluginCfg
17
18
20 """Plugin converts strings to different hash digests. The results are
21 displayed in area for the "answers".
22 """
23
24 id = "hash_plugin"
25 keywords = ["md5", "sha1", "sha224", "sha256", "sha384", "sha512"]
26
27 def __init__(self, plg_cfg: "PluginCfg") -> None:
28 super().__init__(plg_cfg)
29
30 self.parser_re = re.compile(f"({'|'.join(self.keywords)}) (.*)", re.I)
32 id=self.id,
33 name=gettext("Hash plugin"),
34 description=gettext(
35 "Converts strings to different hash digests. Available functions: md5, sha1, sha224, sha256, sha384, sha512." # pylint:disable=line-too-long
36 ),
37 examples=["sha512 The quick brown fox jumps over the lazy dog"],
38 preference_section="query",
39 )
40
41 def post_search(self, request: "SXNG_Request", search: "SearchWithPlugins") -> EngineResults:
42 """Returns a result list only for the first page."""
43 results = EngineResults()
44
45 if search.search_query.pageno > 1:
46 return results
47
48 m = self.parser_re.match(search.search_query.query)
49 if not m:
50 # wrong query
51 return results
52
53 function, string = m.groups()
54 if not string.strip():
55 # end if the string is empty
56 return results
57
58 # select hash function
59 f = hashlib.new(function.lower())
60
61 # make digest from the given string
62 f.update(string.encode("utf-8").strip())
63 answer = function + " " + gettext("hash digest") + ": " + f.hexdigest()
64
65 results.add(results.types.Answer(answer=answer))
66
67 return results
None __init__(self, "PluginCfg" plg_cfg)
EngineResults post_search(self, "SXNG_Request" request, "SearchWithPlugins" search)