hash_plugin.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. import hashlib
  4. import re
  5. from flask_babel import gettext
  6. name = "Hash plugin"
  7. description = gettext("Converts strings to different hash digests.")
  8. default_on = True
  9. preference_section = 'query'
  10. query_keywords = ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']
  11. query_examples = 'sha512 The quick brown fox jumps over the lazy dog'
  12. parser_re = re.compile('(md5|sha1|sha224|sha256|sha384|sha512) (.*)', re.I)
  13. def post_search(_request, search):
  14. # process only on first page
  15. if search.search_query.pageno > 1:
  16. return True
  17. m = parser_re.match(search.search_query.query)
  18. if not m:
  19. # wrong query
  20. return True
  21. function, string = m.groups()
  22. if not string.strip():
  23. # end if the string is empty
  24. return True
  25. # select hash function
  26. f = hashlib.new(function.lower())
  27. # make digest from the given string
  28. f.update(string.encode('utf-8').strip())
  29. answer = function + " " + gettext('hash digest') + ": " + f.hexdigest()
  30. # print result
  31. search.result_container.answers.clear()
  32. search.result_container.answers['hash'] = {'answer': answer}
  33. return True