Restructure ChaosCrypt from a single-file script into a complete, installable Python package with three interfaces: a typed Python API, an argument-driven CLI, and a bilingual interactive TUI. The algorithm itself is unchanged. Ciphertexts and key files remain fully interoperable with the original implementation in both directions, which is enforced by dedicated tests rather than merely documented. Added - Library API: ChaosCrypt facade plus ChaosCryptCore for the raw algorithm - Unified error hierarchy: 16 named types, all ChaosCryptError subclasses, rendering as ChaosCrypt_ERR_<Detail>_<Code> - CLI: keygen, encrypt, decrypt, file, info, chpwd, tui, with --json output, stdin/stdout piping, and three password sources per secret - Bilingual TUI: English default, runtime switch to Chinese, getpass password entry with a non-TTY fallback that keeps it scriptable - Binary and file support: encrypt_bytes/decrypt_bytes and encrypt_file/decrypt_file - Key management: change_key_password rewraps without altering key material; export_key_text/load_from_text move a key without touching disk - Documentation: SECURITY.md with the full design and threat analysis, plus docs/ covering the API, CLI, architecture and build process - Test suite: 92 tests across round-trips, binary safety, key management, error types, CLI, TUI, i18n and legacy interoperability - Packaging: pyproject.toml, setup.py compatibility shim, MANIFEST.in, py.typed, examples, MIT licence Fixed - Conflicting --pwd option between the -p and -P groups prevented the CLI from starting at all - Wrong key file password reported as file corruption (E002); structural validation now distinguishes it from a damaged file (E003) - export_key_text produced key material without loading it into the instance - Non-hexadecimal input was accepted as ciphertext and silently decrypted into garbage, making corruption indistinguishable from success; malformed input now raises E010 before any processing - TUI failed under piped input because getpass requires a TTY - TUI kept reading the default key file name after generating a new one Changed - Source tree reorganised: the package lives at chaoscrypt/, with tests, examples and legacy alongside it; documentation and build artefacts at the repository root - Code comments unified in English; public docstrings document parameters, return values and raised exceptions - English is now the default UI language - ChaosCryptChat removed from the repository - Attribution updated to Dvs (DvsXT); project URLs point to Gitea
146 lines
5.5 KiB
Python
146 lines
5.5 KiB
Python
"""
|
|
Unified error types for ChaosCrypt.
|
|
|
|
Every failure raised by this library is a subclass of :class:`ChaosCryptError`,
|
|
and every instance renders as::
|
|
|
|
ChaosCrypt_ERR_<short description>_<code>
|
|
|
|
The machine-readable ``code`` attribute (``E001`` … ``E016``) is stable and is
|
|
safe to branch on; the human-readable portion is documentation only and may be
|
|
reworded between minor releases.
|
|
|
|
Catching :class:`ChaosCryptError` is sufficient to handle every error the
|
|
library can raise.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
__all__ = [
|
|
"ChaosCryptError",
|
|
"ChaosCrypt_ERR_KeyFileNotFound_E001",
|
|
"ChaosCrypt_ERR_KeyFileCorrupted_E002",
|
|
"ChaosCrypt_ERR_KeyPasswordIncorrect_E003",
|
|
"ChaosCrypt_ERR_KeyNotLoaded_E004",
|
|
"ChaosCrypt_ERR_KeyFileExists_E005",
|
|
"ChaosCrypt_ERR_EmptyPassword_E006",
|
|
"ChaosCrypt_ERR_PasswordMismatch_E007",
|
|
"ChaosCrypt_ERR_EmptyPlaintext_E008",
|
|
"ChaosCrypt_ERR_EmptyCiphertext_E009",
|
|
"ChaosCrypt_ERR_MalformedCiphertext_E010",
|
|
"ChaosCrypt_ERR_DecryptionFailed_E011",
|
|
"ChaosCrypt_ERR_EncodingFailed_E012",
|
|
"ChaosCrypt_ERR_InvalidArgument_E013",
|
|
"ChaosCrypt_ERR_FileReadFailed_E014",
|
|
"ChaosCrypt_ERR_FileWriteFailed_E015",
|
|
"ChaosCrypt_ERR_UnsupportedKeyVersion_E016",
|
|
]
|
|
|
|
|
|
class ChaosCryptError(Exception):
|
|
"""Base class for every error raised by ChaosCrypt.
|
|
|
|
Attributes:
|
|
detail: Human-readable description of what went wrong.
|
|
code: Stable machine-readable code, e.g. ``"E003"``.
|
|
message: The fully rendered ``ChaosCrypt_ERR_<detail>_<code>`` string.
|
|
"""
|
|
|
|
#: Default machine-readable code; overridden by subclasses.
|
|
CODE = "E000"
|
|
#: Default human-readable detail; overridden by subclasses.
|
|
DETAIL = "UnknownError"
|
|
|
|
def __init__(self, detail: str | None = None, code: str | None = None):
|
|
self.detail = detail if detail is not None else self.DETAIL
|
|
self.code = code if code is not None else self.CODE
|
|
self.message = f"ChaosCrypt_ERR_{self.detail}_{self.code}"
|
|
super().__init__(self.message)
|
|
|
|
def __str__(self) -> str: # pragma: no cover - trivial
|
|
return self.message
|
|
|
|
def __repr__(self) -> str: # pragma: no cover - trivial
|
|
return f"{type(self).__name__}({self.message!r})"
|
|
|
|
|
|
def _define(name: str, detail: str, code: str, doc: str) -> type:
|
|
"""Create a named ``ChaosCryptError`` subclass."""
|
|
return type(
|
|
name,
|
|
(ChaosCryptError,),
|
|
{"DETAIL": detail, "CODE": code, "__doc__": doc},
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Named error types
|
|
# ---------------------------------------------------------------------------
|
|
ChaosCrypt_ERR_KeyFileNotFound_E001 = _define(
|
|
"ChaosCrypt_ERR_KeyFileNotFound_E001", "KeyFileNotFound", "E001",
|
|
"The requested key file does not exist.",
|
|
)
|
|
ChaosCrypt_ERR_KeyFileCorrupted_E002 = _define(
|
|
"ChaosCrypt_ERR_KeyFileCorrupted_E002", "KeyFileCorrupted", "E002",
|
|
"The key file could not be parsed (truncated, altered, or not a "
|
|
"ChaosCrypt key file).",
|
|
)
|
|
ChaosCrypt_ERR_KeyPasswordIncorrect_E003 = _define(
|
|
"ChaosCrypt_ERR_KeyPasswordIncorrect_E003", "KeyPasswordIncorrect", "E003",
|
|
"The supplied key file password does not unlock this key file.",
|
|
)
|
|
ChaosCrypt_ERR_KeyNotLoaded_E004 = _define(
|
|
"ChaosCrypt_ERR_KeyNotLoaded_E004", "KeyNotLoaded", "E004",
|
|
"An operation requiring key material was attempted before any key "
|
|
"was loaded.",
|
|
)
|
|
ChaosCrypt_ERR_KeyFileExists_E005 = _define(
|
|
"ChaosCrypt_ERR_KeyFileExists_E005", "KeyFileExists", "E005",
|
|
"The target key file already exists and overwriting was not permitted.",
|
|
)
|
|
ChaosCrypt_ERR_EmptyPassword_E006 = _define(
|
|
"ChaosCrypt_ERR_EmptyPassword_E006", "EmptyPassword", "E006",
|
|
"A password was required but none was supplied.",
|
|
)
|
|
ChaosCrypt_ERR_PasswordMismatch_E007 = _define(
|
|
"ChaosCrypt_ERR_PasswordMismatch_E007", "PasswordMismatch", "E007",
|
|
"The two password entries did not match.",
|
|
)
|
|
ChaosCrypt_ERR_EmptyPlaintext_E008 = _define(
|
|
"ChaosCrypt_ERR_EmptyPlaintext_E008", "EmptyPlaintext", "E008",
|
|
"The plaintext to encrypt was empty.",
|
|
)
|
|
ChaosCrypt_ERR_EmptyCiphertext_E009 = _define(
|
|
"ChaosCrypt_ERR_EmptyCiphertext_E009", "EmptyCiphertext", "E009",
|
|
"The ciphertext to decrypt was empty.",
|
|
)
|
|
ChaosCrypt_ERR_MalformedCiphertext_E010 = _define(
|
|
"ChaosCrypt_ERR_MalformedCiphertext_E010", "MalformedCiphertext", "E010",
|
|
"The ciphertext is not valid hexadecimal or has an odd length.",
|
|
)
|
|
ChaosCrypt_ERR_DecryptionFailed_E011 = _define(
|
|
"ChaosCrypt_ERR_DecryptionFailed_E011", "DecryptionFailed", "E011",
|
|
"Decryption did not produce valid data: the password is wrong, the key "
|
|
"file does not match, or the ciphertext was altered.",
|
|
)
|
|
ChaosCrypt_ERR_EncodingFailed_E012 = _define(
|
|
"ChaosCrypt_ERR_EncodingFailed_E012", "EncodingFailed", "E012",
|
|
"A Base64 or UTF-8 encode/decode step failed.",
|
|
)
|
|
ChaosCrypt_ERR_InvalidArgument_E013 = _define(
|
|
"ChaosCrypt_ERR_InvalidArgument_E013", "InvalidArgument", "E013",
|
|
"An argument had an invalid type or value.",
|
|
)
|
|
ChaosCrypt_ERR_FileReadFailed_E014 = _define(
|
|
"ChaosCrypt_ERR_FileReadFailed_E014", "FileReadFailed", "E014",
|
|
"Reading a file failed with an I/O error.",
|
|
)
|
|
ChaosCrypt_ERR_FileWriteFailed_E015 = _define(
|
|
"ChaosCrypt_ERR_FileWriteFailed_E015", "FileWriteFailed", "E015",
|
|
"Writing a file failed with an I/O error.",
|
|
)
|
|
ChaosCrypt_ERR_UnsupportedKeyVersion_E016 = _define(
|
|
"ChaosCrypt_ERR_UnsupportedKeyVersion_E016", "UnsupportedKeyVersion", "E016",
|
|
"The key file declares a format version this build cannot read.",
|
|
)
|