hash_plugin.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. searx is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Affero General Public License for more details.
  10. You should have received a copy of the GNU Affero General Public License
  11. along with searx. If not, see < http://www.gnu.org/licenses/ >.
  12. (C) 2015 by Adam Tauber, <asciimoo@gmail.com>
  13. (C) 2018, 2020 by Vaclav Zouzalik
  14. '''
  15. from flask_babel import gettext
  16. import hashlib
  17. import re
  18. name = "Hash plugin"
  19. description = gettext("Converts strings to different hash digests.")
  20. default_on = True
  21. preference_section = 'query'
  22. query_keywords = ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']
  23. query_examples = 'sha512 The quick brown fox jumps over the lazy dog'
  24. parser_re = re.compile('(md5|sha1|sha224|sha256|sha384|sha512) (.*)', re.I)
  25. def post_search(request, search):
  26. # process only on first page
  27. if search.search_query.pageno > 1:
  28. return True
  29. m = parser_re.match(search.search_query.query)
  30. if not m:
  31. # wrong query
  32. return True
  33. function, string = m.groups()
  34. if string.strip().__len__() == 0:
  35. # end if the string is empty
  36. return True
  37. # select hash function
  38. f = hashlib.new(function.lower())
  39. # make digest from the given string
  40. f.update(string.encode('utf-8').strip())
  41. answer = function + " " + gettext('hash digest') + ": " + f.hexdigest()
  42. # print result
  43. search.result_container.answers.clear()
  44. search.result_container.answers['hash'] = {'answer': answer}
  45. return True