Add cryptography fallback when PyCryptodome (Crypto) is unavailable#1725
Merged
Conversation
Home Assistant OS environments may not provide a working `Crypto.*` module (PyCryptodome), causing SonoffLAN to fail at startup with: "cannot import name 'AES' from 'Crypto.Cipher'". This change wraps the PyCryptodome imports in a try/except and provides a drop-in fallback implementation using the `cryptography` package (already bundled with Home Assistant) plus `hashlib`/`os`: - AES-CBC cipher wrapper compatible with `AES.new(..., AES.MODE_CBC, iv=...)` - `AES.block_size` constant - `MD5.new()` compatible helper backed by `hashlib.md5` - `get_random_bytes()` backed by `os.urandom` The existing local `pad()`/`unpad()` functions are kept unchanged.
AlexxIT
approved these changes
Mar 3, 2026
Owner
|
Thanks! I don't think there's any point in supporting pycryptodome if cryptography is already included in HA by default. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On Home Assistant OS (and some other HA Python environments), SonoffLAN can fail to load due to missing/incompatible PyCryptodome imports:
from Crypto.Cipher import AESfrom Crypto.Hash import MD5from Crypto.Random import get_random_bytesThis results in a startup error such as:
cannot import name 'AES' from 'Crypto.Cipher'What changed
Crypto.*) imports intry/except.cryptography(HA already ships it)hashlibfor MD5os.urandomfor random bytesThe fallback preserves the existing API used by
local.py:AES.new(key, AES.MODE_CBC, iv=iv)returning an object withencrypt()/decrypt()AES.block_sizeMD5.new().update(...).digest()get_random_bytes(n)No behavior changes when PyCryptodome is available.
Why
HAOS users cannot install Python dependencies with pip inside the HA container,
so SonoffLAN should degrade gracefully when PyCryptodome is not present.
Testing