.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
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 default_on = True
26 keywords = ["md5", "sha1", "sha224", "sha256", "sha384", "sha512"]
27
28 def __init__(self):
29 super().__init__()
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("Converts strings to different hash digests."),
36 examples=["sha512 The quick brown fox jumps over the lazy dog"],
37 preference_section="query",
38 )
39
40 def post_search(self, request: "SXNG_Request", search: "SearchWithPlugins") -> EngineResults:
41 """Returns a result list only for the first page."""
42 results = EngineResults()
43
44 if search.search_query.pageno > 1:
45 return results
46
47 m = self.parser_re.match(search.search_query.query)
48 if not m:
49 # wrong query
50 return results
51
52 function, string = m.groups()
53 if not string.strip():
54 # end if the string is empty
55 return results
56
57 # select hash function
58 f = hashlib.new(function.lower())
59
60 # make digest from the given string
61 f.update(string.encode("utf-8").strip())
62 answer = function + " " + gettext("hash digest") + ": " + f.hexdigest()
63
64 results.add(results.types.Answer(answer=answer))
65
66 return results
EngineResults post_search(self, "SXNG_Request" request, "SearchWithPlugins" search)