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