A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

crypt.py 2.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. import hashlib
  3. from classes import BaseCommand
  4. import blowfish
  5. class Command(BaseCommand):
  6. """Provides hash functions with !hash (!hash list for supported algorithms)
  7. and blowfish encryption with !encrypt and !decrypt."""
  8. name = "crypt"
  9. def check(self, data):
  10. commands = ["crypt", "hash", "encrypt", "decrypt"]
  11. if data.is_command and data.command in commands:
  12. return True
  13. return False
  14. def process(self, data):
  15. if data.command == "crypt":
  16. msg = "available commands are !hash, !encrypt, and !decrypt."
  17. self.connection.reply(data, msg)
  18. return
  19. if not data.args:
  20. msg = "what do you want me to {0}?".format(data.command)
  21. self.connection.reply(data, msg)
  22. return
  23. if data.command == "hash":
  24. algo = data.args[0]
  25. if algo == "list":
  26. algos = ', '.join(hashlib.algorithms)
  27. msg = algos.join(("supported algorithms: ", "."))
  28. self.connection.reply(data, msg)
  29. elif algo in hashlib.algorithms:
  30. string = ' '.join(data.args[1:])
  31. result = getattr(hashlib, algo)(string).hexdigest()
  32. self.connection.reply(data, result)
  33. else:
  34. msg = "unknown algorithm: '{0}'.".format(algo)
  35. self.connection.reply(data, msg)
  36. else:
  37. key = data.args[0]
  38. text = ' '.join(data.args[1:])
  39. if not text:
  40. msg = "a key was provided, but text to {0} was not."
  41. self.connection.reply(data, msg.format(data.command))
  42. return
  43. try:
  44. if data.command == "encrypt":
  45. self.connection.reply(data, blowfish.encrypt(key, text))
  46. else:
  47. self.connection.reply(data, blowfish.decrypt(key, text))
  48. except blowfish.BlowfishError as error:
  49. msg = "{0}: {1}.".format(error.__class__.__name__, error)
  50. self.connection.reply(data, msg)