on master help sign in
fix hmac python3 compatibility
commite73cb14889d29082c3a585644d547e6798fdb6fc authorNoémi Ványi <sitbackandwait@gmail.com> dateThu 20 Jul 2017, 15:44 (9 years ago) parent9804ab7
2 files changed+13−3
searx/utils.py+9 searx/webapp.py+4−3
searx/utils.py+9
@@ -1,4 +1,6 @@
11import csv
2+import hashlib
3+import hmac
24import os
35import re
46
@@ -321,3 +323,10 @@ def load_module(filename, module_dir):
321323 module = load_source(modname, filepath)
322324 module.name = modname
323325 return module
326+
327+
328+def new_hmac(secret_key, url):
329+ if sys.version_info[0] == 2:
330+ return hmac.new(bytes(secret_key), url, hashlib.sha256).hexdigest()
331+ else:
332+ return hmac.new(bytes(secret_key, 'utf-8'), url, hashlib.sha256).hexdigest()
searx/webapp.py+4−3
@@ -69,6 +69,7 @@ from searx.plugins import plugins
6969from searx.preferences import Preferences, ValidationException
7070from searx.answerers import answerers
7171from searx.url_utils import urlencode, urlparse, urljoin
72+from searx.utils import new_hmac
7273
7374# check if the pyopenssl package is installed.
7475# It is needed for SSL connection without trouble, see #298
@@ -290,7 +291,7 @@ def image_proxify(url):
290291 if settings.get('result_proxy'):
291292 return proxify(url)
292293
293− h = hmac.new(settings['server']['secret_key'], url.encode('utf-8'), hashlib.sha256).hexdigest()
294+ h = new_hmac(settings['server']['secret_key'], url.encode('utf-8'))
294295
295296 return '{0}?{1}'.format(url_for('image_proxy'),
296297 urlencode(dict(url=url.encode('utf-8'), h=h)))
@@ -704,7 +705,7 @@ def image_proxy():
704705 if not url:
705706 return '', 400
706707
707− h = hmac.new(settings['server']['secret_key'], url, hashlib.sha256).hexdigest()
708+ h = new_hmac(settings['server']['secret_key'], url)
708709
709710 if h != request.args.get('h'):
710711 return '', 400
@@ -731,7 +732,7 @@ def image_proxy():
731732 logger.debug('image-proxy: wrong content-type: {0}'.format(resp.headers.get('content-type')))
732733 return '', 400
733734
734− img = ''
735+ img = b''
735736 chunk_counter = 0
736737
737738 for chunk in resp.iter_content(1024 * 1024):