Keyrings Cryptfile

1.3.9 · active · verified Thu Apr 16

keyrings.cryptfile is a Python library that provides an encrypted file keyring backend for the standard `keyring` package. It enables secure storage of plaintext passwords in a portable encrypted file, particularly useful when typical desktop environment keyring implementations are unsuitable. The project encrypts data using Argon2 for key derivation and authenticated AES encryption (GCM by default). The latest version is 1.3.9, released on November 20, 2022. While there isn't a strict release cadence, updates appear to be infrequent.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `CryptFileKeyring`, set a master password (either interactively or via an environment variable for non-interactive use), store a service password, and then retrieve it. It also shows how to optionally integrate it with the main `keyring` library.

import keyring
from keyrings.cryptfile.cryptfile import CryptFileKeyring
import os

# Create an instance of the CryptFileKeyring
kr = CryptFileKeyring()

# Optional: Set a keyring key programmatically (e.g., from an environment variable)
# If not set, the library will prompt interactively for the master password.
# Hardcoding the keyring_key is insecure and should be avoided in production.
keyring_master_password = os.environ.get('KEYRING_CRYPTFILE_MASTER_PASSWORD', '')
if keyring_master_password:
    kr.keyring_key = keyring_master_password # type: ignore

# Set a password for a service and user
service_name = "my_application"
username = "api_user"
secret_password = "my_super_secret_password"

# This will prompt for the master password if not set programmatically
kr.set_password(service_name, username, secret_password)
print(f"Password for service '{service_name}' and user '{username}' set successfully.")

# Retrieve the password
# This will also prompt for the master password if not set programmatically
retrieved_password = kr.get_password(service_name, username)

if retrieved_password:
    print(f"Retrieved password for '{service_name}' user '{username}': {retrieved_password}")
else:
    print(f"No password found for '{service_name}' user '{username}'.")

# To make CryptFileKeyring the default for the standard `keyring` library:
# keyring.set_keyring(kr)
# # Now, calls to keyring.set_password() or keyring.get_password() will use CryptFileKeyring
# keyring.set_password("another_service", "another_user", "another_secret")
# print(keyring.get_password("another_service", "another_user"))

view raw JSON →