API reference¶
The everyday API is the lock() / unlock() pair,
which encrypt and decrypt a file in place. The lower-level
encrypt() / decrypt() functions return the
envelope or plaintext bytes without touching the file, and the prompt helpers
read a password from the terminal. Everything below is imported directly from
the top-level pydlock package.
Locking and unlocking files¶
Byte-level encryption¶
- pydlock.encrypt(path, encoding='utf-8', password=None)[source]¶
Encrypts the contents of a file and returns the v2 envelope bytes: the magic prefix, a JSON header carrying the KDF parameters and per-file salt, a newline, and the Fernet token.
The
passwordmay be passed asstrorbytes(or omitted, to prompt): astris encoded to bytes withencodingat this public API boundary, since scrypt operates on bytes.
- pydlock.decrypt(path, encoding='utf-8', password=None)[source]¶
Decrypts a pydlock file and returns its plaintext bytes. A v2 file (magic prefix) is read via the envelope header (key re-derived from the stored salt and parameters); a non-magic file is treated as a legacy v1 raw Fernet token and read with the old scheme, so no existing file is stranded (re-locking rewrites it as v2). Fernet verifies the token, so a wrong password or a tampered header/token fails cleanly rather than yielding wrong plaintext.
The
passwordmay be passed asstrorbytes(or omitted, to prompt): astris encoded to bytes withencodingat this public API boundary, covering both the v2 scrypt path and the v1 legacy path.