A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Cryptography functions (hashing and cyphers).
  3. import hashlib
  4. from irc.base_command import BaseCommand
  5. from lib import blowfish
  6. class Cryptography(BaseCommand):
  7. def get_hooks(self):
  8. return ["msg"]
  9. def get_help(self, command):
  10. if command == "hash":
  11. return ("Return the hash of a string using a given algorithm, " +
  12. "e.g. '!hash sha512 Hello world!'. Use '!hash list' for " +
  13. "a list of supported algorithms.")
  14. elif command == "encrypt":
  15. return ("Encrypt any string with a given key using an " +
  16. "implementation of Blowfish, e.g. '!encrypt some_key " +
  17. "Hello world!'.")
  18. else:
  19. return ("Decrypt a string with a given key using a given " +
  20. "algorithm, e.g. '!decrypt blowfish some_key Hello " +
  21. "world!'.")
  22. def check(self, data):
  23. if data.is_command and data.command in ["hash", "encrypt", "decrypt"]:
  24. return True
  25. return False
  26. def process(self, data):
  27. if not data.args:
  28. self.connection.reply(data, "what do you want me to {0}?".format(
  29. data.command))
  30. return
  31. if data.command == "hash":
  32. algo = data.args[0]
  33. if algo == "list":
  34. algos = ', '.join(hashlib.algorithms)
  35. self.connection.reply(data, "supported algorithms: " + algos +
  36. ".")
  37. elif algo in hashlib.algorithms:
  38. string = ' '.join(data.args[1:])
  39. result = eval("hashlib.{0}(string)".format(algo)).hexdigest()
  40. self.connection.reply(data, result)
  41. else:
  42. self.connection.reply(data, "unknown algorithm: '{0}'.".format(
  43. algo))
  44. else:
  45. key = data.args[0]
  46. text = ' '.join(data.args[1:])
  47. if not text:
  48. self.connection.reply(data, "that's a key, yes, but what do " +
  49. " you want me to {0}?".format(data.command))
  50. return
  51. if data.command == "encrypt":
  52. self.connection.reply(data, blowfish.encrypt(key, text))
  53. else:
  54. self.connection.reply(data, blowfish.decrypt(key, text))