wasp_general.crypto package¶
Submodules¶
wasp_general.crypto.aes module¶
-
class
wasp_general.crypto.aes.WAES(mode)[source]¶ Bases:
objectPyCrypto AES-encryption wrapper
-
decrypt(data, decode=False)[source]¶ Decrypt the given data with cipher that is got from AES.cipher call.
Parameters: - data – data to decrypt
- decode – whether to decode bytes to str or not
Returns: bytes or str (depends on decode flag)
-
-
class
wasp_general.crypto.aes.WAESMode(key_size, block_cipher_mode, init_sequence, padding=None)[source]¶ Bases:
objectThis class specifies modes of AES encryption. It describes secret key (size and value), block cipher mode of operation, padding object (
WBlockPaddinginstance), required initialization values. Note, padding is required if source data isn’t aligned to block size.For byte-sequence generation (that is used as secret key and initialization values) it is possible to use
wasp_general.crypto.kdf.WPBKDF2.wasp_general.crypto.kdf.WPBKDF2is a wrapper for PBKDF2 function (KDF function that safely generates byte-sequence from the given password and salt)Currently, only two cipher mode of operation are implemented: ‘CBC’ and ‘CTR’
see also: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
-
class
SequenceChopper(key_size, block_cipher_mode, sequence)[source]¶ Bases:
objectHelper, that chops the given byte-sequence into several separate objects (like secret key, initialization vector or initialization counter values). The exact values depend on AES key size and block cipher mode of operation.
If length of the given byte-sequence is greater then it is required, then extra bytes discard and this extra-bytes don’t take part in any calculation
-
initialization_counter_value()[source]¶ Return initialization counter value generated from the initial byte-sequence if it is required by the current block cipher mode of operation. If it doesn’t require - then None is returned
Returns: int or None
-
initialization_vector()[source]¶ Return initialization vector generated from the initial byte-sequence if it is required by the current block cipher mode of operation. If it doesn’t require - then None is returned
Returns: bytes or None
-
-
classmethod
init_sequence_length(key_size, block_cipher_mode)[source]¶ Return required byte-sequence length
Parameters: - key_size – secret size
- block_cipher_mode – name of block cipher mode of operation
Returns: int
-
initialization_counter_value()[source]¶ Return currently used initialization counter value or None if counter is not used
Returns: int or None
-
initialization_vector()[source]¶ Return currently used initialization vector or None if vector is not used
Returns: bytes or None
-
classmethod
parse_cipher_name(name)[source]¶ Parse cipher name (name like ‘aes_256_cbc’ or ‘AES-128-CTR’). Also this method validates If the cipher is supported by this class. If no - exception is raised
Parameters: name – name to parse Returns: tuple where the first element is a key size in bytes (int) and the second element - block cipher mode of operation (str) (for example: (16, ‘AES-CTR’) or (24, ‘AES-CBC’))
-
class
-
class
wasp_general.crypto.aes.WBlockPadding[source]¶ Bases:
objectPadding/reverse padding class prototype
-
class
wasp_general.crypto.aes.WPKCS7Padding[source]¶ Bases:
wasp_general.crypto.aes.WBlockPaddingPKCS7 Padding implementation
see also: https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7
-
pad(data, block_size)[source]¶ WBlockPadding.pad()method implementation
-
reverse_pad(data, block_size)[source]¶ WBlockPadding.reverse_pad()method implementation
-
-
class
wasp_general.crypto.aes.WShiftPadding(padding=None)[source]¶ Bases:
wasp_general.crypto.aes.WSimplePaddingSame as
WSimplePaddingclass, but also randomly shifts original data.-
reverse_pad(data, block_size)[source]¶ WBlockPadding.reverse_pad()method implementation
-
-
class
wasp_general.crypto.aes.WSimplePadding(padding=None)[source]¶ Bases:
wasp_general.crypto.aes.WBlockPaddingClass that pads given data with specified ASCII character
-
pad(data, block_size)[source]¶ WBlockPadding.pad()method implementation
-
reverse_pad(data, block_size)[source]¶ WBlockPadding.reverse_pad()method implementation
-
-
class
wasp_general.crypto.aes.WZeroPadding[source]¶ Bases:
wasp_general.crypto.aes.WSimplePaddingZero padding implementation (just alias for WSimplePadding() object)
see also: https://en.wikipedia.org/wiki/Padding_(cryptography)#Zero_padding
wasp_general.crypto.hash module¶
-
class
wasp_general.crypto.hash.WHash[source]¶ Bases:
objectClass that aggregates different hash-generators. This class is should be used if there is a need to address digest generator by its name. As a result - generator (
WHashGeneratorProto) is returned.-
static
available_digests(family=None, name=None)[source]¶ Return names of available generators
Parameters: - family – name of hash-generator family to select
- name – name of hash-generator to select
Returns: set of int
-
static
available_generators(family=None, name=None)[source]¶ Return names of available generators
Parameters: - family – name of hash-generator family to select
- name – name of hash-generator to select (parameter may be used for availability check)
Returns: tuple of str
-
static
-
class
wasp_general.crypto.hash.WHashGeneratorProto[source]¶ Bases:
objectPrototype for hash-generator.
note: there is commonly used feature that most hash generator objects have - digest_size attribute. So it is better to create this attribute.
-
classmethod
generator_family()[source]¶ Return name of hash-function family (like: ‘SHA’)
Returns: str or None (if no available)
-
classmethod
-
class
wasp_general.crypto.hash.WMD5[source]¶ Bases:
wasp_general.crypto.hash.WPyCryptoHashAdapterMD5 hash-generator
-
class
wasp_general.crypto.hash.WPyCryptoHashAdapter[source]¶ Bases:
wasp_general.crypto.hash.WHashGeneratorProtoClass that adapts the specified PyCrypto hashing class to WHashGeneratorProto implementation
-
digest()[source]¶ WHashGeneratorProto.digest()implementation
-
classmethod
generator_digest_size()[source]¶ WHashGeneratorProto.generator_digest_size()implementation
-
classmethod
generator_family()[source]¶ WHashGeneratorProto.generator_family()implementation
-
classmethod
generator_name()[source]¶ WHashGeneratorProto.generator_name()implementation
-
classmethod
new(data=None)[source]¶ WHashGeneratorProto.new()implementation
-
pycrypto()[source]¶ In rare cases original PyCrypto object is required. In most cases this method should be avoided, as it is can be removed at any time.
One of an example of this method usage is PyCrypto HMAC (and so
WHMAC). They require, that hash-generator object must have “copy” method to be implemented. But I have not found a way to make HMAC work.Returns: PyCrypto Hash object
-
update(data)[source]¶ WHashGeneratorProto.update()implementation
-
-
class
wasp_general.crypto.hash.WSHA1[source]¶ Bases:
wasp_general.crypto.hash.WSHAFamilySHA1 hash-generator
-
class
wasp_general.crypto.hash.WSHA224[source]¶ Bases:
wasp_general.crypto.hash.WSHAFamilySHA224 hash-generator
-
class
wasp_general.crypto.hash.WSHA256[source]¶ Bases:
wasp_general.crypto.hash.WSHAFamilySHA256 hash-generator
-
class
wasp_general.crypto.hash.WSHA384[source]¶ Bases:
wasp_general.crypto.hash.WSHAFamilySHA384 hash-generator
-
class
wasp_general.crypto.hash.WSHA512[source]¶ Bases:
wasp_general.crypto.hash.WSHAFamilySHA512 hash-generator
-
class
wasp_general.crypto.hash.WSHAFamily[source]¶ Bases:
wasp_general.crypto.hash.WPyCryptoHashAdapterClass that represent SHA-family hash-generators
wasp_general.crypto.hex module¶
wasp_general.crypto.hmac module¶
-
class
wasp_general.crypto.hmac.WHMAC(digest_generator_name=None)[source]¶ Bases:
objectClass that wraps PyCrypto HMAC implementation
see also https://en.wikipedia.org/wiki/Hash-based_message_authentication_code
wasp_general.crypto.kdf module¶
-
class
wasp_general.crypto.kdf.WPBKDF2(key, salt=None, derived_key_length=None, iterations_count=None, hmac=None)[source]¶ Bases:
objectWrapper for PyCrypto PBKDF2 implementation with NIST recommendation and HMAC is used as pseudorandom function
NIST recommendation can be read here: http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf (Recommendation for Password-Based Key Derivation)
wasp_general.crypto.random module¶
-
wasp_general.crypto.random.random_bits(bits_count)[source]¶ Random generator (PyCrypto getrandbits wrapper). The result is a non-negative value.
Parameters: bits_count – random bits to generate Returns: int
wasp_general.crypto.rsa module¶
-
class
wasp_general.crypto.rsa.WRSA[source]¶ Bases:
objectPyCrypto RSA-encryption wrapper
-
static
decrypt(binary_chain, private_key, sha_digest_size=32)[source]¶ Decrypt data with key and PKCS1 OAEP protocol
Parameters: - binary_chain – data to decrypt
- private_key – private key
- sha_digest_size – SHA digest size
Returns: bytes
-
static
encrypt(binary_chain, key, sha_digest_size=32)[source]¶ Encrypt data with key and PKCS1 OAEP protocol
Parameters: - binary_chain – data to encrypt
- key – must be public key or private key with builtin public
- sha_digest_size – SHA digest size
Returns: bytes
-
static
export_key(key, password=None)[source]¶ Export key in PEM-format
Parameters: - key – key to export
- password – If it is not None, then result will be encrypt with given password. Suitable only for private key. With public keys this argument does nothing
Returns: bytes
-
static
generate_private(key_length=2048)[source]¶ Generate new private key (corresponding public key is included)
Parameters: key_length – same as bits argument in Crypto.PublicKey.RSA.generate function (it must be a multiple of 256, and no smaller than 1024) Returns: WRSA.wrapped_class
-
static
generate_public(private_key)[source]¶ Get public key from private one
Parameters: private_key – source private key Returns: RSA.wrapper_class
-
static
import_key(pem_text, password=None)[source]¶ Import key written in PEM-format
Parameters: - pem_text – PEM data
- password – Password with witch PEM-data is encrypted
Returns: RSA.wrapped_class
-
static
string_decrypt(binary_data, private_key, text_encoding=None, sha_digest_size=32)[source]¶ Decrypt binary data with given private key and PKCS1 OAEP protocol
Parameters: - binary_data – data to decrypt
- private_key – private key
- text_encoding – source string encoding
- sha_digest_size – SHA digest size
Returns: str
-
static
string_encrypt(text, key, sha_digest_size=32)[source]¶ Encrypt text with given public key and PKCS1 OAEP protocol
Parameters: - text – text to encrypt
- key – public key or private key with builtin public
- sha_digest_size – SHA digest size
Returns: bytes
-
wrapped_class¶ RSA wrapped class
alias of
_RSAobj
-
static