Exemple #1
0
    def encrypt(self, plaintext, salt=None, binary=False):
        if not isinstance(plaintext, six.text_type) and not binary:
            raise ValueError("plaintext cannot be bytestring and not binary")

        if isinstance(plaintext, six.text_type):
            plaintext = plaintext.encode('utf-8')

        if salt is None:
            saltchars = string.ascii_letters + string.digits + "./"
            salt = bytes("$1$" + Utils.random_string(8, saltchars))
        elif isinstance(salt, six.text_type):
            salt = bytes(salt)

        return crypt.crypt(plaintext, salt).decode()
Exemple #2
0
    def encrypt(self, plaintext, salt=None, binary=False):
        """
        Salted triple-DES.

        Added by fhl, 2019-05-15, copied from an older UiT copy of
        Cerebrum.Account, as triple-DES was removed from UiO code.
        """
        if not isinstance(plaintext, six.text_type) and not binary:
            raise ValueError("plaintext cannot be bytestring and not binary")

        if isinstance(plaintext, six.text_type):
            plaintext = plaintext.encode('utf-8')

        if salt is None:
            saltchars = string.ascii_letters + string.digits + "./"
            salt = bytes(Utils.random_string(2, saltchars))
        elif isinstance(salt, six.text_type):
            # should be ascii only
            salt = bytes(salt)

        return six.text_type(crypt.crypt(plaintext, salt))