|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- # -*- coding: utf-8 -*-
- #
- # Copyright (C) 2009-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
- #
- # Permission is hereby granted, free of charge, to any person obtaining a copy
- # of this software and associated documentation files (the "Software"), to deal
- # in the Software without restriction, including without limitation the rights
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- # copies of the Software, and to permit persons to whom the Software is
- # furnished to do so, subject to the following conditions:
- #
- # The above copyright notice and this permission notice shall be included in
- # all copies or substantial portions of the Software.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- # SOFTWARE.
-
- import base64
- import hashlib
- import os
-
- from earwigbot import importer
- from earwigbot.commands import Command
-
- fernet = importer.new("cryptography.fernet")
- hashes = importer.new("cryptography.hazmat.primitives.hashes")
- pbkdf2 = importer.new("cryptography.hazmat.primitives.kdf.pbkdf2")
-
- class Crypt(Command):
- """Provides hash functions with !hash (!hash list for supported algorithms)
- and basic encryption with !encrypt and !decrypt."""
- name = "crypt"
- commands = ["crypt", "hash", "encrypt", "decrypt"]
-
- def process(self, data):
- if data.command == "crypt":
- msg = "Available commands are !hash, !encrypt, and !decrypt."
- self.reply(data, msg)
- return
-
- if not data.args:
- msg = "What do you want me to {0}?".format(data.command)
- self.reply(data, msg)
- return
-
- if data.command == "hash":
- algo = data.args[0]
- if algo == "list":
- algos = ', '.join(hashlib.algorithms_available)
- msg = algos.join(("Supported algorithms: ", "."))
- self.reply(data, msg)
- elif algo in hashlib.algorithms_available:
- string = ' '.join(data.args[1:])
- result = getattr(hashlib, algo)(string.encode()).hexdigest()
- self.reply(data, result)
- else:
- msg = "Unknown algorithm: '{0}'.".format(algo)
- self.reply(data, msg)
-
- else:
- key = data.args[0]
- text = " ".join(data.args[1:])
- saltlen = 16
-
- if not text:
- msg = "A key was provided, but text to {0} was not."
- self.reply(data, msg.format(data.command))
- return
-
- try:
- if data.command == "encrypt":
- salt = os.urandom(saltlen)
- kdf = pbkdf2.PBKDF2HMAC(
- algorithm=hashes.SHA256(),
- length=32,
- salt=salt,
- iterations=100000,
- )
- f = fernet.Fernet(base64.urlsafe_b64encode(kdf.derive(key.encode())))
- ciphertext = f.encrypt(text.encode())
- self.reply(data, base64.b64encode(salt + ciphertext).decode())
- else:
- if len(text) < saltlen:
- raise ValueError("Ciphertext is too short")
- raw = base64.b64decode(text)
- salt, ciphertext = raw[:saltlen], raw[saltlen:]
- kdf = pbkdf2.PBKDF2HMAC(
- algorithm=hashes.SHA256(),
- length=32,
- salt=salt,
- iterations=100000,
- )
- f = fernet.Fernet(base64.urlsafe_b64encode(kdf.derive(key.encode())))
- self.reply(data, f.decrypt(ciphertext).decode())
- except ImportError:
- self.reply(data, "This command requires the 'cryptography' package: https://cryptography.io/")
- except Exception as error:
- self.reply(data, "{}: {}".format(type(error).__name__, str(error)))
|