scripts: support both pycryptodome and pycryptodomex
Current actual Python library for cryptography is being distributed in
two flavors: pycroptodome and pycroptodomex. They are basically the same
library, but with different import names:
- pycryptodome provides 'Crypto' module and indented to directly replace
old pycrypto library
- pycryptodomex provides 'Cryptodome' module and is intended for old
distributions, where pycrypto is still present
Most of the modern Linux distributions provide both of the libraries, so
there is no difference which one is to use. But some of them (like
Yocto/Poky) provide only one.
This patches makes scripts agnostic to a crypto library flavor being used
by trying to import Cryptodome first and then Crypto if first import fails.
Signed-off-by: Volodymyr Babchuk <volodymyr_babchuk@epam.com>
Reviewed-by: Jerome Forissier <jerome@forissier.org>
diff --git a/scripts/sign_encrypt.py b/scripts/sign_encrypt.py
index 15a1458..33deeb8 100755
--- a/scripts/sign_encrypt.py
+++ b/scripts/sign_encrypt.py
@@ -147,10 +147,16 @@
def main():
- from Cryptodome.Signature import pss
- from Cryptodome.Signature import pkcs1_15
- from Cryptodome.Hash import SHA256
- from Cryptodome.PublicKey import RSA
+ try:
+ from Cryptodome.Signature import pss
+ from Cryptodome.Signature import pkcs1_15
+ from Cryptodome.Hash import SHA256
+ from Cryptodome.PublicKey import RSA
+ except ImportError:
+ from Crypto.Signature import pss
+ from Crypto.Signature import pkcs1_15
+ from Crypto.Hash import SHA256
+ from Crypto.PublicKey import RSA
import base64
import logging
import os