Pro.Crypto
— Crypto API¶
Overview¶
The Pro.Crypto
module contains the core API for hashing and encryption/decryption.
Hashing¶
The following code example demonstrates how to succinctly compute a cryptographic hash.
from Pro.Crypto import *
print(NTCryptoSHA1(b"Hello, World!").finalHexString())
Of course, data can also be computed in chunks.
from Pro.Crypto import *
h = NTCryptoSHA1()
h.update(b"chunk1")
h.update(b"chunk2")
print(h.finalHexString())
Additionally, hashing implementations provide convenience methods to process containers and files.
AES Encryption/Decryption¶
The following code example shows how to encrypt and decrypt data using AES.
from Pro.Crypto import *
aes = NTCryptoAES(b"A" * 16)
data = aes.encrypt(b"Hello, World!")
print(data)
aes.reset()
data = aes.decrypt(data)
print(data)
Module API¶
Pro.Crypto module API.
Classes:
NTCryptoAES
(key, iv, mode)This class provides AES encryption and decryption.
NTCryptoAdler32
(data)This class implements the Adler32 hashing method.
NTCryptoCRC32
(data)This class implements the CRC32 hashing method.
NTCryptoHMAC_SHA1
(data)This class implements the HMAC-SHA1 hashing method.
The base class inherited by all hashing implementations.
NTCryptoMD5
(data)This class implements the MD5 hashing method.
NTCryptoRMD128
(data)This class implements the RMD128 hashing method.
NTCryptoRMD160
(data)This class implements the RMD160 hashing method.
NTCryptoSHA1
(data)This class implements the SHA1 hashing method.
NTCryptoSHA256
(data)This class implements the SHA2-256 hashing method.
NTCryptoSHA3
(hashlen, data)This class implements the SHA3 hashing method.
NTCryptoSHA384
(data)This class implements the SHA2-384 hashing method.
NTCryptoSHA3_224
(data)This class implements the SHA3-224 hashing method.
NTCryptoSHA3_256
(data)This class implements the SHA3-256 hashing method.
NTCryptoSHA3_384
(data)This class implements the SHA3-384 hashing method.
NTCryptoSHA3_512
(data)This class implements the SHA3-512 hashing method.
NTCryptoSHA512
(data)This class implements the SHA2-512 hashing method.
NTCryptoWPool
(data)This class implements the Whirlpool hashing method.
This class provides humanization for hashes.
- class NTCryptoAES(key: bytes, iv: bytes = bytes(), mode: int = CBC)¶
This class provides AES encryption and decryption.
- Parameters
key (bytes) – The key.
iv (bytes) – The initialization vector.
mode (int) – The AES mode (e.g.,
CBC
).See also
encrypt()
anddecrypt()
.Attributes:
CBC mode.
CFB mode.
CTR mode.
ECB mode.
OFB mode.
Methods:
decrypt
(data)Decrypts the input data.
encrypt
(data)Encrypts the input data.
reset
()Resets the state.
- CBC: Final[int]¶
CBC mode.
- CFB: Final[int]¶
CFB mode.
- CTR: Final[int]¶
CTR mode.
- ECB: Final[int]¶
ECB mode.
- OFB: Final[int]¶
OFB mode.
- decrypt(data: bytes) → bytes¶
Decrypts the input data.
- Parameters
data (bytes) – The input data.
- Returns
Returns the decrypted data if successful; otherwise returns an empty
bytes
object.- Return type
bytes
See also
encrypt()
.
- encrypt(data: bytes) → bytes¶
Encrypts the input data.
- Parameters
data (bytes) – The input data.
- Returns
Returns the encrypted data if successful; otherwise returns an empty
bytes
object.- Return type
bytes
See also
decrypt()
.
- reset() → None¶
Resets the state.
- class NTCryptoAdler32(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoHash
This class implements the Adler32 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
Methods:
adler32
()Returns the hash as a 32-bit integer.
- adler32() → int¶
- Returns
Returns the hash as a 32-bit integer.
- Return type
int
- class NTCryptoCRC32(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoHash
This class implements the CRC32 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
Methods:
crc32
()Returns the hash as a 32-bit integer.
- crc32() → int¶
- Returns
Returns the hash as a 32-bit integer.
- Return type
int
- class NTCryptoHMAC_SHA1(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoHash
This class implements the HMAC-SHA1 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
Methods:
updkey
(data)Updates the key with the input data.
- updkey(data: bytes) → None¶
Updates the key with the input data.
- Parameters
data (bytes) – The input data.
- class NTCryptoHash¶
The base class inherited by all hashing implementations.
Methods:
final
()Returns the final hash.
Returns the final hash as hex string.
init
()Initializes the hash.
update
(data)Updates the hash status with the input data.
updc
(c[, wo])Updates the hash status with the input container.
updfile
(file_or_fileName[, wo])Updates the hash status with the input file.
- final() → bytes¶
- Returns
Returns the final hash.
- Return type
bytes
See also
finalHexString()
,update()
,updc()
andupdfile()
.
- finalHexString() → str¶
- Returns
Returns the final hash as hex string.
- Return type
str
- update(data: bytes) → None¶
Updates the hash status with the input data.
- Parameters
data (bytes) – The input data.
See also
final()
,finalHexString()
,updc()
andupdfile()
.
- updc(c: Pro.Core.NTContainer, wo: Optional[Pro.Core.NTIWait] = None) → bool¶
Updates the hash status with the input container.
- Parameters
c (NTContainer) – The input container.
wo (Optional[NTIWait]) – An optional wait object.
- Returns
Returns
True
if successful; otherwise returnsFalse
.- Return type
bool
See also
final()
,finalHexString()
,update()
andupdfile()
.
- updfile(file_or_fileName: Union[NT_FILE, str], wo: NTIWait = None) → bool¶
Updates the hash status with the input file.
- Parameters
file_or_fileName (Union[NT_FILE, str]) – Either the name of the input file or its instance.
wo (Optional[NTIWait]) – An optional wait object.
- Returns
Returns
True
if successful; otherwise returnsFalse
.- Return type
bool
See also
final()
,finalHexString()
,update()
andupdc()
.
- class NTCryptoMD5(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoHash
This class implements the MD5 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
- class NTCryptoRMD128(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoHash
This class implements the RMD128 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
- class NTCryptoRMD160(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoHash
This class implements the RMD160 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
- class NTCryptoSHA1(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoHash
This class implements the SHA1 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
- class NTCryptoSHA256(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoHash
This class implements the SHA2-256 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
- class NTCryptoSHA3(hashlen: Optional[int] = None, data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoHash
This class implements the SHA3 hashing method.
- Parameters
hashlen (Optional[int]) – Optional hash length.
data (Optional[bytes]) – Optional input data.
- class NTCryptoSHA384(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoHash
This class implements the SHA2-384 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
- class NTCryptoSHA3_224(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoSHA3
This class implements the SHA3-224 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
- class NTCryptoSHA3_256(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoSHA3
This class implements the SHA3-256 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
- class NTCryptoSHA3_384(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoSHA3
This class implements the SHA3-384 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
- class NTCryptoSHA3_512(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoSHA3
This class implements the SHA3-512 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
- class NTCryptoSHA512(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoHash
This class implements the SHA2-512 hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
- class NTCryptoWPool(data: Optional[bytes] = None)¶
Bases:
Pro.Crypto.NTCryptoHash
This class implements the Whirlpool hashing method.
- Parameters
data (Optional[bytes]) – Optional input data.
- class NTHumanHash¶
This class provides humanization for hashes.
Methods:
humanize
(hexdigest[, words, separator])Humanizes the input hash.
setWordList
(wl)Sets a custom world list to be used to create the humanized hash.
- humanize(hexdigest: str, words: int = 4, separator: int = '-') → str¶
Humanizes the input hash.
- Parameters
hexdigest (str) – The input hash as hex string.
words (int) – The number of words to be used.
separator (int) – The separator.
- Returns
Returns the humanized hash string.
- Return type
str
- setWordList(wl: Pro.Core.NTStringList) → bool¶
Sets a custom world list to be used to create the humanized hash.
Important
The word list must contain 256 words.
- Parameters
wl (NTStringList) – The custom word list.
- Returns
Returns
True
if successful; otherwise returnsFalse
.- Return type
bool