A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

87 linhas
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import hashlib
  23. from earwigbot import importer
  24. from earwigbot.commands import Command
  25. Blowfish = importer.new("Crypto.Cipher.Blowfish")
  26. class Crypt(Command):
  27. """Provides hash functions with !hash (!hash list for supported algorithms)
  28. and Blowfish encryption with !encrypt and !decrypt."""
  29. name = "crypt"
  30. commands = ["crypt", "hash", "encrypt", "decrypt"]
  31. def process(self, data):
  32. if data.command == "crypt":
  33. msg = "Available commands are !hash, !encrypt, and !decrypt."
  34. self.reply(data, msg)
  35. return
  36. if not data.args:
  37. msg = "What do you want me to {0}?".format(data.command)
  38. self.reply(data, msg)
  39. return
  40. if data.command == "hash":
  41. algo = data.args[0]
  42. if algo == "list":
  43. algos = ', '.join(hashlib.algorithms)
  44. msg = algos.join(("Supported algorithms: ", "."))
  45. self.reply(data, msg)
  46. elif algo in hashlib.algorithms:
  47. string = ' '.join(data.args[1:])
  48. result = getattr(hashlib, algo)(string).hexdigest()
  49. self.reply(data, result)
  50. else:
  51. msg = "Unknown algorithm: '{0}'.".format(algo)
  52. self.reply(data, msg)
  53. else:
  54. key = data.args[0]
  55. text = " ".join(data.args[1:])
  56. if not text:
  57. msg = "A key was provided, but text to {0} was not."
  58. self.reply(data, msg.format(data.command))
  59. return
  60. try:
  61. cipher = Blowfish.new(hashlib.sha256(key).digest())
  62. except ImportError:
  63. msg = "This command requires the 'pycrypto' package: https://www.dlitz.net/software/pycrypto/"
  64. self.reply(data, msg)
  65. return
  66. try:
  67. if data.command == "encrypt":
  68. if len(text) % 8:
  69. pad = 8 - len(text) % 8
  70. text = text.ljust(len(text) + pad, "\x00")
  71. self.reply(data, cipher.encrypt(text).encode("hex"))
  72. else:
  73. self.reply(data, cipher.decrypt(text.decode("hex")))
  74. except (ValueError, TypeError) as error:
  75. self.reply(data, error.message)