Skip to content

Instantly share code, notes, and snippets.

@alikins
Created February 19, 2018 16:24
Show Gist options
  • Save alikins/bb505a260e2eff1ad16b7649672fb964 to your computer and use it in GitHub Desktop.
Save alikins/bb505a260e2eff1ad16b7649672fb964 to your computer and use it in GitHub Desktop.
test-crypto.py
# code based on lib/ansible/parsing/vault/__init__.py
#
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import sys
import warnings
HAS_CRYPTOGRAPHY = False
HAS_PYCRYPTO = False
HAS_SOME_PYCRYPTO = False
CRYPTOGRAPHY_BACKEND = None
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, padding
from cryptography.hazmat.primitives.hmac import HMAC
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives.ciphers import (
Cipher as C_Cipher, algorithms, modes
)
CRYPTOGRAPHY_BACKEND = default_backend()
HAS_CRYPTOGRAPHY = True
except ImportError:
pass
try:
from Crypto.Cipher import AES as AES_pycrypto
HAS_SOME_PYCRYPTO = True
# Note: Only used for loading obsolete VaultAES files. All files are written
# using the newer VaultAES256 which does not require md5
from Crypto.Hash import SHA256 as SHA256_pycrypto
from Crypto.Hash import HMAC as HMAC_pycrypto
# Counter import fails for 2.0.1, requires >= 2.6.1 from pip
from Crypto.Util import Counter as Counter_pycrypto
# KDF import fails for 2.0.1, requires >= 2.6.1 from pip
from Crypto.Protocol.KDF import PBKDF2 as PBKDF2_pycrypto
HAS_PYCRYPTO = True
except ImportError:
pass
b_HEADER = b'$ANSIBLE_VAULT'
CIPHER_WHITELIST = frozenset((u'AES', u'AES256'))
CIPHER_WRITE_WHITELIST = frozenset((u'AES256',))
# See also CIPHER_MAPPING at the bottom of the file which maps cipher strings
# (used in VaultFile header) to a cipher class
NEED_CRYPTO_LIBRARY = "ansible-vault requires either the cryptography library (preferred) or"
if HAS_SOME_PYCRYPTO:
NEED_CRYPTO_LIBRARY += " a newer version of"
NEED_CRYPTO_LIBRARY += " pycrypto in order to function."
def main(args):
print('HAS_CRYPTOGRAPHY: %s' % HAS_CRYPTOGRAPHY)
print('HAS_PYCRYPTO: %s' % HAS_PYCRYPTO)
print('HAS_SOME_PYCRYPTO: %s' % HAS_SOME_PYCRYPTO)
print('CRYPTOGRAPHY_BACKEND: %s' % CRYPTOGRAPHY_BACKEND)
if not HAS_CRYPTOGRAPHY and not HAS_PYCRYPTO:
print("No crypto modules found")
return 1
try:
import Crypto
print('Crypto.__file__ = %s' % Crypto.__file__)
print('Crypto.__path__ = %s' % Crypto.__path__)
print('Crypto.__version__ = %s' % Crypto.__version__)
except ImportError as e:
print('Could not import %s' % e)
try:
import cryptography
print('cryptography.__file__ = %s' % cryptography.__file__)
print('cryptography.__path__ = %s' % cryptography.__path__)
print('cryptography.__version__ = %s' % cryptography.__version__)
except ImportError as e:
print('Could not import %s' % e)
try:
import setuptools
print('setuptools.__file__ = %s' % setuptools.__file__)
print('setuptools.__path__ = %s' % setuptools.__path__)
print('setuptools.__version__ = %s' % setuptools.__version__)
except ImportError as e:
print('Could not import %s' % e)
if __name__ == "__main__":
sys.exit(main(sys.argv[:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment