Field-level encryption
- class couchbase.encryption.CryptoManager
The interface field-level encryption is built on.
A crypto manager encrypts and decrypts field values, and mangles the names of encrypted fields so a reader can tell them apart.
- abstract decrypt(encrypted: dict[str, Any]) bytes
Decrypt a value with the decrypter its
algkey names.- Parameters:
encrypted (dict[str, Any]) – An encrypted value, which must have an
algkey.- Returns:
The decrypted value.
- Return type:
- Raises:
DecryptionFailureException – If the value could not be decrypted.
- abstract encrypt(plaintext: str | bytes | bytearray, encrypter_alias: str | None = None) dict[str, Any]
Encrypt a value with the encrypter registered under an alias.
- Parameters:
- Returns:
An
EncryptionResultas a dict.- Return type:
- Raises:
EncryptionFailureException – If the value could not be encrypted.
- class couchbase.encryption.Decrypter(keyring: Keyring, alg: str | None = None)
The interface a decrypter for one algorithm implements.
- Parameters:
- algorithm() str | None
Get the name of the algorithm the decrypter handles.
- Returns:
The name of the algorithm, or None if none was given.
- Return type:
str | None
- abstract decrypt(encrypted: EncryptionResult) bytes
Decrypt an encrypted value.
The decrypter’s algorithm should match the value’s
alg.- Parameters:
encrypted (
EncryptionResult) – The value to decrypt.- Returns:
The decrypted value.
- Return type:
- Raises:
InvalidCryptoKeyException – If the key is not valid for the algorithm.
InvalidCipherTextException – If the ciphertext cannot be decrypted.
- class couchbase.encryption.Encrypter(keyring: Keyring, key: str)
The interface an encrypter for one algorithm and key implements.
- Parameters:
- abstract encrypt(plaintext: str | bytes | bytearray) EncryptionResult
Encrypt a value.
- Parameters:
- Returns:
The ciphertext and everything needed to decrypt it.
- Return type:
- Raises:
InvalidCryptoKeyException – If the encrypter’s key is not valid for its algorithm.
- class couchbase.encryption.EncryptionResult(alg: str = '', kid: str | None = None, ciphertext: str | None = None, **kwargs: Any)
An encrypted value and what is needed to decrypt it, stored in place of the plain field.
- Parameters:
alg (str) – The name of the algorithm that encrypted the value.
kid (str, optional) – The ID of the key that encrypted it.
ciphertext (str, optional) – The encrypted value, base64 encoded. A value that is not valid base64 is left out.
**kwargs – Any other entries the algorithm needs, stored as given.
- Raises:
InvalidArgumentException – If
algis empty.
- algorithm() str
Get the name of the algorithm that encrypted the value.
- Returns:
The
algentry.- Return type:
- get(key: str) Any
Get an entry.
- Parameters:
key (str) – The entry’s name.
- Returns:
The entry’s value.
- Return type:
Any
- Raises:
CryptoKeyNotFoundException – If the entry is missing or empty.
- get_with_base64_decode(key: str) bytes
Get an entry, base64 decoded.
- Parameters:
key (str) – The entry’s name.
- Returns:
The decoded value.
- Return type:
- Raises:
CryptoKeyNotFoundException – If the entry is missing or empty.
- classmethod new_encryption_result_from_dict(values: dict[str, Any]) EncryptionResult
Create an encrypted value from its dict form.