瀏覽代碼

Merge dee04b2a21e75244ac42af6d163b1161b8b727ad into f82ead3e303d75ba63a370dc038311e172e1330d

Venca24 6 年之前
父節點
當前提交
30aa473d6a
沒有帳戶連結到提交者的電子郵件
共有 4 個文件被更改,包括 111 次插入0 次删除
  1. 1
    0
      AUTHORS.rst
  2. 2
    0
      searx/plugins/__init__.py
  3. 55
    0
      searx/plugins/hash_plugin.py
  4. 53
    0
      tests/unit/test_plugins.py

+ 1
- 0
AUTHORS.rst 查看文件

@@ -83,3 +83,4 @@ generally made searx better:
83 83
 - Joseph Nuthalapati @josephkiranbabu
84 84
 - @maiki
85 85
 - Richard Didier @zeph33
86
+- Václav Zouzalík @Venca24

+ 2
- 0
searx/plugins/__init__.py 查看文件

@@ -23,6 +23,7 @@ if version_info[0] == 3:
23 23
 logger = logger.getChild('plugins')
24 24
 
25 25
 from searx.plugins import (oa_doi_rewrite,
26
+                           hash_plugin,
26 27
                            https_rewrite,
27 28
                            infinite_scroll,
28 29
                            open_results_on_new_tab,
@@ -79,6 +80,7 @@ class PluginStore():
79 80
 
80 81
 plugins = PluginStore()
81 82
 plugins.register(oa_doi_rewrite)
83
+plugins.register(hash_plugin)
82 84
 plugins.register(https_rewrite)
83 85
 plugins.register(infinite_scroll)
84 86
 plugins.register(open_results_on_new_tab)

+ 55
- 0
searx/plugins/hash_plugin.py 查看文件

@@ -0,0 +1,55 @@
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
+
30
+def post_search(request, search):
31
+    # process only on first page
32
+    if search.search_query.pageno > 1:
33
+        return True
34
+    m = parser_re.match(search.search_query.query)
35
+    if not m:
36
+        # wrong query
37
+        return True
38
+
39
+    function, string = m.groups()
40
+    function = str(function.decode('UTF-8'))  # convert to string for python3
41
+    if string.strip().__len__() == 0:
42
+        # end if the string is empty
43
+        return True
44
+
45
+    # select hash function
46
+    f = hashlib.new(function.lower())
47
+
48
+    # make digest from the given string
49
+    f.update(string.strip())
50
+    digest = f.hexdigest()
51
+
52
+    # print result
53
+    search.result_container.answers.clear()
54
+    search.result_container.answers.add(function + " " + gettext('hash function') + ": " + digest)
55
+    return True

+ 53
- 0
tests/unit/test_plugins.py 查看文件

@@ -83,3 +83,56 @@ class SelfIPTest(SearxTestCase):
83 83
         search = get_search_mock(query=b'What is my User-Agent?', pageno=2)
84 84
         store.call(store.plugins, 'post_search', request, search)
85 85
         self.assertFalse('Mock' in search.result_container.answers)
86
+
87
+
88
+class HashPluginTest(SearxTestCase):
89
+
90
+    def test_PluginStore_init(self):
91
+        store = plugins.PluginStore()
92
+        store.register(plugins.hash_plugin)
93
+
94
+        self.assertTrue(len(store.plugins) == 1)
95
+
96
+        request = Mock(remote_addr='127.0.0.1')
97
+        request.headers.getlist.return_value = []
98
+
99
+        # MD5
100
+        search = get_search_mock(query=b'md5 test', pageno=1)
101
+        store.call(store.plugins, 'post_search', request, search)
102
+        self.assertTrue('md5 hash function: 098f6bcd4621d373cade4e832627b4f6' in search.result_container.answers)
103
+
104
+        search = get_search_mock(query=b'md5 test', pageno=2)
105
+        store.call(store.plugins, 'post_search', request, search)
106
+        self.assertFalse('md5 hash function: 098f6bcd4621d373cade4e832627b4f6' in search.result_container.answers)
107
+
108
+        # SHA1
109
+        search = get_search_mock(query=b'sha1 test', pageno=1)
110
+        store.call(store.plugins, 'post_search', request, search)
111
+        self.assertTrue('sha1 hash function: a94a8fe5ccb19ba61c4c0873d391e9879'
112
+                        '82fbbd3' in search.result_container.answers)
113
+
114
+        # SHA224
115
+        search = get_search_mock(query=b'sha224 test', pageno=1)
116
+        store.call(store.plugins, 'post_search', request, search)
117
+        self.assertTrue('sha224 hash function: 90a3ed9e32b2aaf4c61c410eb9254261'
118
+                        '19e1a9dc53d4286ade99a809' in search.result_container.answers)
119
+
120
+        # SHA256
121
+        search = get_search_mock(query=b'sha256 test', pageno=1)
122
+        store.call(store.plugins, 'post_search', request, search)
123
+        self.assertTrue('sha256 hash function: 9f86d081884c7d659a2feaa0c55ad015a'
124
+                        '3bf4f1b2b0b822cd15d6c15b0f00a08' in search.result_container.answers)
125
+
126
+        # SHA384
127
+        search = get_search_mock(query=b'sha384 test', pageno=1)
128
+        store.call(store.plugins, 'post_search', request, search)
129
+        self.assertTrue('sha384 hash function: 768412320f7b0aa5812fce428dc4706b3c'
130
+                        'ae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf1'
131
+                        '7a0a9' in search.result_container.answers)
132
+
133
+        # SHA512
134
+        search = get_search_mock(query=b'sha512 test', pageno=1)
135
+        store.call(store.plugins, 'post_search', request, search)
136
+        self.assertTrue('sha512 hash function: ee26b0dd4af7e749aa1a8ee3c10ae9923f6'
137
+                        '18980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5'
138
+                        'fa9ad8e6f57f50028a8ff' in search.result_container.answers)