hash_plugin.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. parser_re = re.compile('(md5|sha1|sha224|sha256|sha384|sha512) (.*)', re.I)
  22. def post_search(request, search):
  23. # process only on first page
  24. if search.search_query.pageno > 1:
  25. return True
  26. m = parser_re.match(search.search_query.query)
  27. if not m:
  28. # wrong query
  29. return True
  30. function, string = m.groups()
  31. if string.strip().__len__() == 0:
  32. # end if the string is empty
  33. return True
  34. # select hash function
  35. f = hashlib.new(function.lower())
  36. # make digest from the given string
  37. f.update(string.encode('utf-8').strip())
  38. answer = function + " " + gettext('hash digest') + ": " + f.hexdigest()
  39. # print result
  40. search.result_container.answers.clear()
  41. search.result_container.answers['hash'] = {'answer': answer}
  42. return True