<filename>ykvault/cipher.py
#!/usr/bin/env python3

from typing import NamedTuple
from secrets import token_bytes

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding


class CBCCipherText(NamedTuple):
    iv: bytes
    ciphertext: bytes


def load(key: bytes, ciphertext: CBCCipherText) -> bytes:
    """
    Decrypt and load the ciphertext given.
    """
    cipher = Cipher(
            algorithms.AES(key),
            modes.CBC(ciphertext.iv)
    ).decryptor()

    cleartext = cipher.update(ciphertext.ciphertext) + cipher.finalize()

    unpadder = padding.PKCS7(128).unpadder()
    return unpadder.update(cleartext) \
            + unpadder.finalize()


def dump(key: bytes, data: bytes) -> CBCCipherText:
    """
    Encrypt the given payload with the given key and return the IV and
    ciphertext.
    """
    iv = token_bytes(16)
    cipher = Cipher(
            algorithms.AES(key),
            modes.CBC(iv)
    ).encryptor()

    padder = padding.PKCS7(128).padder()

    cleartext = padder.update(data) + padder.finalize()

    return CBCCipherText(
            iv=iv,
            ciphertext=cipher.update(cleartext) + cipher.finalize()
    )

<filename>ykvault/cipher.py
#!/usr/bin/env python3

from typing import NamedTuple
from secrets import token_bytes

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding


class CBCCipherText(NamedTuple):
    iv: bytes
    ciphertext: bytes


def load(key: bytes, ciphertext: CBCCipherText) -> bytes:
    """
    Decrypt and load the ciphertext given.
    """
    cipher = Cipher(
            algorithms.AES(key),
            modes.CBC(ciphertext.iv)
    ).decryptor()

    cleartext = cipher.update(ciphertext.ciphertext) + cipher.finalize()

    unpadder = padding.PKCS7(128).unpadder()
    return unpadder.update(cleartext) \
            + unpadder.finalize()


def dump(key: bytes, data: bytes) -> CBCCipherText:
    """
    Encrypt the given payload with the given key and return the IV and
    ciphertext.
    """
    iv = token_bytes(16)
    cipher = Cipher(
            algorithms.AES(key),
            modes.CBC(iv)
    ).encryptor()

    padder = padding.PKCS7(128).padder()

    cleartext = padder.update(data) + padder.finalize()

    return CBCCipherText(
            iv=iv,
            ciphertext=cipher.update(cleartext) + cipher.finalize()
    )

#!/usr/bin/env python3 Decrypt and load the ciphertext given. Encrypt the given payload with the given key and return the IV and     ciphertext.