coinex-api 0.0.89__py3-none-any.whl → 0.0.110__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of coinex-api might be problematic. Click here for more details.
- coinex/ccxt/__init__.py +1 -1
- coinex/ccxt/async_support/__init__.py +1 -1
- coinex/ccxt/async_support/base/exchange.py +139 -10
- coinex/ccxt/async_support/base/throttler.py +1 -1
- coinex/ccxt/async_support/base/ws/cache.py +1 -0
- coinex/ccxt/async_support/base/ws/client.py +26 -4
- coinex/ccxt/async_support/coinex.py +2 -2
- coinex/ccxt/base/exchange.py +587 -91
- coinex/ccxt/base/types.py +11 -2
- coinex/ccxt/coinex.py +2 -2
- coinex/ccxt/pro/__init__.py +1 -1
- coinex/ccxt/pro/coinex.py +10 -7
- coinex/ccxt/static_dependencies/bip/__init__.py +6 -0
- coinex/ccxt/static_dependencies/bip/addr/P2PKH_addr.py +205 -0
- coinex/ccxt/static_dependencies/bip/addr/__init__.py +5 -0
- coinex/ccxt/static_dependencies/bip/addr/addr_dec_utils.py +125 -0
- coinex/ccxt/static_dependencies/bip/addr/addr_key_validator.py +162 -0
- coinex/ccxt/static_dependencies/bip/addr/iaddr_decoder.py +48 -0
- coinex/ccxt/static_dependencies/bip/addr/iaddr_encoder.py +50 -0
- coinex/ccxt/static_dependencies/bip/base58/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/base58/base58.py +207 -0
- coinex/ccxt/static_dependencies/bip/base58/base58_ex.py +25 -0
- coinex/ccxt/static_dependencies/bip/base58/base58_xmr.py +155 -0
- coinex/ccxt/static_dependencies/bip/bech32/__init__.py +4 -0
- coinex/ccxt/static_dependencies/bip/bech32/bch_bech32.py +220 -0
- coinex/ccxt/static_dependencies/bip/bech32/bech32.py +235 -0
- coinex/ccxt/static_dependencies/bip/bech32/bech32_base.py +246 -0
- coinex/ccxt/static_dependencies/bip/bech32/bech32_ex.py +25 -0
- coinex/ccxt/static_dependencies/bip/bech32/segwit_bech32.py +173 -0
- coinex/ccxt/static_dependencies/bip/bip32/__init__.py +14 -0
- coinex/ccxt/static_dependencies/bip/bip32/base/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/bip32/base/bip32_base.py +581 -0
- coinex/ccxt/static_dependencies/bip/bip32/base/ibip32_key_derivator.py +83 -0
- coinex/ccxt/static_dependencies/bip/bip32/base/ibip32_mst_key_generator.py +47 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_const.py +35 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_ex.py +29 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_key_data.py +500 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_key_net_ver.py +83 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_key_ser.py +294 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_keys.py +457 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_path.py +247 -0
- coinex/ccxt/static_dependencies/bip/bip32/bip32_utils.py +72 -0
- coinex/ccxt/static_dependencies/bip/bip32/kholaw/__init__.py +4 -0
- coinex/ccxt/static_dependencies/bip/bip32/kholaw/bip32_kholaw_ed25519.py +82 -0
- coinex/ccxt/static_dependencies/bip/bip32/kholaw/bip32_kholaw_ed25519_key_derivator.py +118 -0
- coinex/ccxt/static_dependencies/bip/bip32/kholaw/bip32_kholaw_key_derivator_base.py +204 -0
- coinex/ccxt/static_dependencies/bip/bip32/kholaw/bip32_kholaw_mst_key_generator.py +119 -0
- coinex/ccxt/static_dependencies/bip/bip32/slip10/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/bip32/slip10/bip32_slip10_key_derivator.py +200 -0
- coinex/ccxt/static_dependencies/bip/bip32/slip10/bip32_slip10_mst_key_generator.py +168 -0
- coinex/ccxt/static_dependencies/bip/bip32/slip10/bip32_slip10_secp256k1.py +82 -0
- coinex/ccxt/static_dependencies/bip/bip44/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/bip44/bip44.py +265 -0
- coinex/ccxt/static_dependencies/bip/bip44_base/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/bip44_base/bip44_base.py +624 -0
- coinex/ccxt/static_dependencies/bip/bip44_base/bip44_base_ex.py +25 -0
- coinex/ccxt/static_dependencies/bip/bip44_base/bip44_keys.py +225 -0
- coinex/ccxt/static_dependencies/bip/coin_conf/__init__.py +2 -0
- coinex/ccxt/static_dependencies/bip/coin_conf/coin_conf.py +68 -0
- coinex/ccxt/static_dependencies/bip/coin_conf/coins_conf.py +890 -0
- coinex/ccxt/static_dependencies/bip/conf/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/conf/bip44/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/conf/bip44/bip44_coins.py +126 -0
- coinex/ccxt/static_dependencies/bip/conf/bip44/bip44_conf.py +1360 -0
- coinex/ccxt/static_dependencies/bip/conf/bip44/bip44_conf_getter.py +153 -0
- coinex/ccxt/static_dependencies/bip/conf/bip49/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/conf/bip49/bip49_coins.py +53 -0
- coinex/ccxt/static_dependencies/bip/conf/bip49/bip49_conf.py +366 -0
- coinex/ccxt/static_dependencies/bip/conf/bip49/bip49_conf_getter.py +80 -0
- coinex/ccxt/static_dependencies/bip/conf/bip84/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/conf/bip84/bip84_coins.py +39 -0
- coinex/ccxt/static_dependencies/bip/conf/bip84/bip84_conf.py +113 -0
- coinex/ccxt/static_dependencies/bip/conf/bip84/bip84_conf_getter.py +66 -0
- coinex/ccxt/static_dependencies/bip/conf/bip86/__init__.py +3 -0
- coinex/ccxt/static_dependencies/bip/conf/bip86/bip86_coins.py +37 -0
- coinex/ccxt/static_dependencies/bip/conf/bip86/bip86_conf.py +83 -0
- coinex/ccxt/static_dependencies/bip/conf/bip86/bip86_conf_getter.py +64 -0
- coinex/ccxt/static_dependencies/bip/conf/common/__init__.py +8 -0
- coinex/ccxt/static_dependencies/bip/conf/common/atom_addr.py +104 -0
- coinex/ccxt/static_dependencies/bip/conf/common/bip_bitcoin_cash_conf.py +106 -0
- coinex/ccxt/static_dependencies/bip/conf/common/bip_coin_conf.py +217 -0
- coinex/ccxt/static_dependencies/bip/conf/common/bip_coins.py +28 -0
- coinex/ccxt/static_dependencies/bip/conf/common/bip_conf_const.py +30 -0
- coinex/ccxt/static_dependencies/bip/conf/common/bip_litecoin_conf.py +121 -0
- coinex/ccxt/static_dependencies/bip/ecc/__init__.py +42 -0
- coinex/ccxt/static_dependencies/bip/ecc/common/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/ecc/common/dummy_point.py +219 -0
- coinex/ccxt/static_dependencies/bip/ecc/common/ikeys.py +263 -0
- coinex/ccxt/static_dependencies/bip/ecc/common/ipoint.py +190 -0
- coinex/ccxt/static_dependencies/bip/ecc/conf.py +28 -0
- coinex/ccxt/static_dependencies/bip/ecc/curve/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/ecc/curve/elliptic_curve.py +121 -0
- coinex/ccxt/static_dependencies/bip/ecc/curve/elliptic_curve_getter.py +74 -0
- coinex/ccxt/static_dependencies/bip/ecc/curve/elliptic_curve_types.py +37 -0
- coinex/ccxt/static_dependencies/bip/ecc/ecdsa/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/ecc/ecdsa/ecdsa_keys.py +36 -0
- coinex/ccxt/static_dependencies/bip/ecc/secp256k1/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/ecc/secp256k1/secp256k1.py +36 -0
- coinex/ccxt/static_dependencies/bip/ecc/secp256k1/secp256k1_const.py +59 -0
- coinex/ccxt/static_dependencies/bip/ecc/secp256k1/secp256k1_keys_ecdsa.py +248 -0
- coinex/ccxt/static_dependencies/bip/ecc/secp256k1/secp256k1_point_ecdsa.py +234 -0
- coinex/ccxt/static_dependencies/bip/slip/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/slip/slip173/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/slip/slip173/slip173.py +60 -0
- coinex/ccxt/static_dependencies/bip/slip/slip32/__init__.py +4 -0
- coinex/ccxt/static_dependencies/bip/slip/slip32/slip32.py +322 -0
- coinex/ccxt/static_dependencies/bip/slip/slip32/slip32_key_net_ver.py +62 -0
- coinex/ccxt/static_dependencies/bip/slip/slip44/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/slip/slip44/slip44.py +81 -0
- coinex/ccxt/static_dependencies/bip/utils/__init__.py +0 -0
- coinex/ccxt/static_dependencies/bip/utils/conf/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/utils/conf/coin_names.py +59 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/__init__.py +10 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/aes_ecb.py +152 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/blake2.py +191 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/chacha20_poly1305.py +101 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/hash160.py +57 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/hmac.py +118 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/pbkdf2.py +66 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/ripemd.py +58 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/scrypt.py +66 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/sha2.py +182 -0
- coinex/ccxt/static_dependencies/bip/utils/crypto/sha3.py +99 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/__init__.py +7 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/algo.py +108 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/base32.py +151 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/bit.py +115 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/bytes.py +200 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/data_bytes.py +181 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/integer.py +97 -0
- coinex/ccxt/static_dependencies/bip/utils/misc/string.py +54 -0
- coinex/ccxt/static_dependencies/bip/utils/typing/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/utils/typing/literal.py +27 -0
- coinex/ccxt/static_dependencies/bip/wif/__init__.py +1 -0
- coinex/ccxt/static_dependencies/bip/wif/wif.py +144 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/amino/amino_pb2.py +31 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/base/v1beta1/coin_pb2.py +47 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/crypto/multisig/keys_pb2.py +33 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/crypto/multisig/v1beta1/multisig_pb2.py +33 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/crypto/secp256k1/keys_pb2.py +34 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/msg/v1/msg_pb2.py +27 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/tx/signing/v1beta1/signing_pb2.py +38 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos/tx/v1beta1/tx_pb2.py +75 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/cosmos_proto/cosmos_pb2.py +36 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/accountplus_pb2.py +31 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/genesis_pb2.py +40 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/models_pb2.py +26 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/params_pb2.py +29 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/query_pb2.py +57 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/accountplus/tx_pb2.py +51 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/block_rate_limit_config_pb2.py +37 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/clob_pair_pb2.py +41 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/equity_tier_limit_config_pb2.py +35 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/finalize_block_pb2.py +27 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/liquidations_config_pb2.py +39 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/liquidations_pb2.py +38 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/matches_pb2.py +55 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/mev_pb2.py +49 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/operation_pb2.py +32 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/order_pb2.py +86 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/order_removals_pb2.py +32 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/process_proposer_matches_events_pb2.py +42 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/query_pb2.py +124 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/streaming_pb2.py +29 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/clob/tx_pb2.py +117 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/sending/genesis_pb2.py +26 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/sending/query_pb2.py +26 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/sending/transfer_pb2.py +61 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/sending/tx_pb2.py +37 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/asset_position_pb2.py +29 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/genesis_pb2.py +30 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/perpetual_position_pb2.py +33 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/query_pb2.py +63 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/streaming_pb2.py +31 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/dydxprotocol/subaccounts/subaccount_pb2.py +33 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/gogoproto/gogo_pb2.py +102 -0
- coinex/ccxt/static_dependencies/dydx_v4_client/registry.py +38 -0
- coinex/ccxt/static_dependencies/ecdsa/ellipticcurve.py +842 -0
- coinex/ccxt/static_dependencies/ecdsa/keys.py +15 -4
- coinex/ccxt/static_dependencies/mnemonic/__init__.py +4 -0
- coinex/ccxt/static_dependencies/mnemonic/mnemonic.py +282 -0
- coinex/ccxt/static_dependencies/mnemonic/py.typed +1 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/chinese_simplified.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/chinese_traditional.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/czech.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/english.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/french.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/italian.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/japanese.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/korean.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/portuguese.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/russian.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/spanish.txt +2048 -0
- coinex/ccxt/static_dependencies/mnemonic/wordlist/turkish.txt +2048 -0
- {coinex_api-0.0.89.dist-info → coinex_api-0.0.110.dist-info}/METADATA +3 -3
- {coinex_api-0.0.89.dist-info → coinex_api-0.0.110.dist-info}/RECORD +197 -17
- {coinex_api-0.0.89.dist-info → coinex_api-0.0.110.dist-info}/WHEEL +1 -1
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# Copyright (c) 2021 Emanuele Bellocchia
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
# furnished to do so, subject to the following conditions:
|
|
9
|
+
#
|
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
|
11
|
+
# all copies or substantial portions of the Software.
|
|
12
|
+
#
|
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
# THE SOFTWARE.
|
|
20
|
+
|
|
21
|
+
"""Module for AES-ECB encryption/decryption."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
#
|
|
25
|
+
# Imports
|
|
26
|
+
#
|
|
27
|
+
from typing import Any, Union
|
|
28
|
+
|
|
29
|
+
from Crypto.Cipher import AES
|
|
30
|
+
from Crypto.Util.Padding import pad, unpad
|
|
31
|
+
|
|
32
|
+
from ..misc.algo import AlgoUtils
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
#
|
|
36
|
+
# Classes
|
|
37
|
+
#
|
|
38
|
+
|
|
39
|
+
class AesEcbEncrypter:
|
|
40
|
+
"""
|
|
41
|
+
AES-ECB encrypter class.
|
|
42
|
+
It encrypts data using AES-ECB algorithm.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
aes: Any
|
|
46
|
+
auto_pad: bool
|
|
47
|
+
|
|
48
|
+
# Constructor
|
|
49
|
+
def __init__(self,
|
|
50
|
+
key: Union[str, bytes]) -> None:
|
|
51
|
+
"""
|
|
52
|
+
Construct class.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
key (str or bytes): AES key
|
|
56
|
+
"""
|
|
57
|
+
self.aes = AES.new(AlgoUtils.Encode(key), AES.MODE_ECB)
|
|
58
|
+
self.auto_pad = True
|
|
59
|
+
|
|
60
|
+
def AutoPad(self,
|
|
61
|
+
value: bool) -> None:
|
|
62
|
+
"""
|
|
63
|
+
Set the auto-pad flag.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
value (bool): Flag value
|
|
67
|
+
"""
|
|
68
|
+
self.auto_pad = value
|
|
69
|
+
|
|
70
|
+
def Encrypt(self,
|
|
71
|
+
data: Union[str, bytes]) -> bytes:
|
|
72
|
+
"""
|
|
73
|
+
Encrypt data using AES-ECB algorithm.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
data (str or bytes): Data to be encrypted
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
bytes: Encrypted data
|
|
80
|
+
"""
|
|
81
|
+
padded_data = self.Pad(data) if self.auto_pad else AlgoUtils.Encode(data)
|
|
82
|
+
return self.aes.encrypt(padded_data)
|
|
83
|
+
|
|
84
|
+
@staticmethod
|
|
85
|
+
def Pad(data: Union[str, bytes]) -> bytes:
|
|
86
|
+
"""
|
|
87
|
+
Pad data using PKCS7 algorithm.
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
data (str or bytes): Data to be padded
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
bytes: Padded data
|
|
94
|
+
"""
|
|
95
|
+
return pad(AlgoUtils.Encode(data), AES.block_size)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class AesEcbDecrypter:
|
|
99
|
+
"""
|
|
100
|
+
AES-ECB decrypter class.
|
|
101
|
+
It decrypts data using AES-ECB algorithm.
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
aes: Any
|
|
105
|
+
|
|
106
|
+
def __init__(self,
|
|
107
|
+
key: Union[str, bytes]) -> None:
|
|
108
|
+
"""
|
|
109
|
+
Construct class.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
key (str or bytes): AES key
|
|
113
|
+
"""
|
|
114
|
+
self.aes = AES.new(AlgoUtils.Encode(key), AES.MODE_ECB)
|
|
115
|
+
self.auto_unpad = True
|
|
116
|
+
|
|
117
|
+
def AutoUnPad(self,
|
|
118
|
+
value: bool) -> None:
|
|
119
|
+
"""
|
|
120
|
+
Set the auto-unpad flag.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
value (bool): Flag value
|
|
124
|
+
"""
|
|
125
|
+
self.auto_unpad = value
|
|
126
|
+
|
|
127
|
+
def Decrypt(self,
|
|
128
|
+
data: bytes) -> bytes:
|
|
129
|
+
"""
|
|
130
|
+
Decrypt data using AES-ECB algorithm.
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
data (bytes): Data to be decrypted
|
|
134
|
+
|
|
135
|
+
Returns:
|
|
136
|
+
bytes: Decrypted data
|
|
137
|
+
"""
|
|
138
|
+
dec = self.aes.decrypt(data)
|
|
139
|
+
return self.UnPad(dec) if self.auto_unpad else dec
|
|
140
|
+
|
|
141
|
+
@staticmethod
|
|
142
|
+
def UnPad(data: bytes) -> bytes:
|
|
143
|
+
"""
|
|
144
|
+
Unpad data using PKCS7 algorithm.
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
data (bytes): Data to be unpadded
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
bytes: Unpadded data
|
|
151
|
+
"""
|
|
152
|
+
return unpad(data, AES.block_size)
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# Copyright (c) 2022 Emanuele Bellocchia
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
# furnished to do so, subject to the following conditions:
|
|
9
|
+
#
|
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
|
11
|
+
# all copies or substantial portions of the Software.
|
|
12
|
+
#
|
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
# THE SOFTWARE.
|
|
20
|
+
|
|
21
|
+
"""Module for BLAKE-2 algorithms."""
|
|
22
|
+
|
|
23
|
+
# Imports
|
|
24
|
+
import hashlib
|
|
25
|
+
from abc import ABC, abstractmethod
|
|
26
|
+
from typing import Union
|
|
27
|
+
|
|
28
|
+
from ..misc import AlgoUtils
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class Blake2b:
|
|
32
|
+
"""
|
|
33
|
+
BLAKE2b class.
|
|
34
|
+
It computes digests using BLAKE2b algorithm.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def QuickDigest(data: Union[bytes, str],
|
|
39
|
+
digest_size: int,
|
|
40
|
+
key: Union[bytes, str] = b"",
|
|
41
|
+
salt: Union[bytes, str] = b"") -> bytes:
|
|
42
|
+
"""
|
|
43
|
+
Compute the digest (quick version).
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
data (str or bytes) : Data
|
|
47
|
+
digest_size (int) : Digest size
|
|
48
|
+
key ((str or bytes, optional) : Key (default: empty)
|
|
49
|
+
salt ((str or bytes, optional): Salt (default: empty)
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
bytes: Computed digest
|
|
53
|
+
"""
|
|
54
|
+
return hashlib.blake2b(AlgoUtils.Encode(data),
|
|
55
|
+
digest_size=digest_size,
|
|
56
|
+
key=AlgoUtils.Encode(key),
|
|
57
|
+
salt=AlgoUtils.Encode(salt)).digest()
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class _Blake2bWithSpecificSize(ABC):
|
|
61
|
+
"""Abstract class for Blake2b with specific digest size."""
|
|
62
|
+
|
|
63
|
+
@classmethod
|
|
64
|
+
def QuickDigest(cls,
|
|
65
|
+
data: Union[bytes, str],
|
|
66
|
+
key: Union[bytes, str] = b"",
|
|
67
|
+
salt: Union[bytes, str] = b"") -> bytes:
|
|
68
|
+
"""
|
|
69
|
+
Compute the digest (quick version).
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
data (str or bytes) : Data
|
|
73
|
+
key (str or bytes, optional) : Key bytes (default: empty)
|
|
74
|
+
salt (str or bytes, optional): Salt bytes (default: empty)
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
bytes: Computed digest
|
|
78
|
+
"""
|
|
79
|
+
return Blake2b.QuickDigest(data, cls.DigestSize(), key, salt)
|
|
80
|
+
|
|
81
|
+
@staticmethod
|
|
82
|
+
@abstractmethod
|
|
83
|
+
def DigestSize() -> int:
|
|
84
|
+
"""
|
|
85
|
+
Get the digest size in bytes.
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
int: Digest size in bytes
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class Blake2b32(_Blake2bWithSpecificSize):
|
|
93
|
+
"""
|
|
94
|
+
BLAKE2b-32 class.
|
|
95
|
+
It computes digests using BLAKE2b-32 algorithm.
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
@staticmethod
|
|
99
|
+
def DigestSize() -> int:
|
|
100
|
+
"""
|
|
101
|
+
Get the digest size in bytes.
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
int: Digest size in bytes
|
|
105
|
+
"""
|
|
106
|
+
return 4
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class Blake2b40(_Blake2bWithSpecificSize):
|
|
110
|
+
"""
|
|
111
|
+
BLAKE2b-40 class.
|
|
112
|
+
It computes digests using BLAKE2b-40 algorithm.
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
@staticmethod
|
|
116
|
+
def DigestSize() -> int:
|
|
117
|
+
"""
|
|
118
|
+
Get the digest size in bytes.
|
|
119
|
+
|
|
120
|
+
Returns:
|
|
121
|
+
int: Digest size in bytes
|
|
122
|
+
"""
|
|
123
|
+
return 5
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class Blake2b160(_Blake2bWithSpecificSize):
|
|
127
|
+
"""
|
|
128
|
+
BLAKE2b-160 class.
|
|
129
|
+
It computes digests using BLAKE2b-160 algorithm.
|
|
130
|
+
"""
|
|
131
|
+
|
|
132
|
+
@staticmethod
|
|
133
|
+
def DigestSize() -> int:
|
|
134
|
+
"""
|
|
135
|
+
Get the digest size in bytes.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
int: Digest size in bytes
|
|
139
|
+
"""
|
|
140
|
+
return 20
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
class Blake2b224(_Blake2bWithSpecificSize):
|
|
144
|
+
"""
|
|
145
|
+
BLAKE2b-224 class.
|
|
146
|
+
It computes digests using BLAKE2b-224 algorithm.
|
|
147
|
+
"""
|
|
148
|
+
|
|
149
|
+
@staticmethod
|
|
150
|
+
def DigestSize() -> int:
|
|
151
|
+
"""
|
|
152
|
+
Get the digest size in bytes.
|
|
153
|
+
|
|
154
|
+
Returns:
|
|
155
|
+
int: Digest size in bytes
|
|
156
|
+
"""
|
|
157
|
+
return 28
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class Blake2b256(_Blake2bWithSpecificSize):
|
|
161
|
+
"""
|
|
162
|
+
BLAKE2b-256 class.
|
|
163
|
+
It computes digests using BLAKE2b-256 algorithm.
|
|
164
|
+
"""
|
|
165
|
+
|
|
166
|
+
@staticmethod
|
|
167
|
+
def DigestSize() -> int:
|
|
168
|
+
"""
|
|
169
|
+
Get the digest size in bytes.
|
|
170
|
+
|
|
171
|
+
Returns:
|
|
172
|
+
int: Digest size in bytes
|
|
173
|
+
"""
|
|
174
|
+
return 32
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class Blake2b512(_Blake2bWithSpecificSize):
|
|
178
|
+
"""
|
|
179
|
+
BLAKE2b-512 class.
|
|
180
|
+
It computes digests using BLAKE2b-512 algorithm.
|
|
181
|
+
"""
|
|
182
|
+
|
|
183
|
+
@staticmethod
|
|
184
|
+
def DigestSize() -> int:
|
|
185
|
+
"""
|
|
186
|
+
Get the digest size in bytes.
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
int: Digest size in bytes
|
|
190
|
+
"""
|
|
191
|
+
return 64
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# Copyright (c) 2022 Emanuele Bellocchia
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
# furnished to do so, subject to the following conditions:
|
|
9
|
+
#
|
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
|
11
|
+
# all copies or substantial portions of the Software.
|
|
12
|
+
#
|
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
# THE SOFTWARE.
|
|
20
|
+
|
|
21
|
+
"""Module for ChaCha20-Poly1305 algorithm."""
|
|
22
|
+
|
|
23
|
+
# Imports
|
|
24
|
+
from typing import Tuple, Union
|
|
25
|
+
|
|
26
|
+
from Crypto.Cipher import ChaCha20_Poly1305
|
|
27
|
+
|
|
28
|
+
from ..misc import AlgoUtils
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ChaCha20Poly1305:
|
|
32
|
+
"""
|
|
33
|
+
ChaCha20-Poly1305 class.
|
|
34
|
+
It decrypts/encrypts data using ChaCha20-Poly1305 algorithm.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def Decrypt(key: Union[bytes, str],
|
|
39
|
+
nonce: Union[bytes, str],
|
|
40
|
+
assoc_data: Union[bytes, str],
|
|
41
|
+
cipher_text: Union[bytes, str],
|
|
42
|
+
tag: Union[bytes, str]) -> bytes:
|
|
43
|
+
"""
|
|
44
|
+
Decrypt data.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
key (str or bytes) : Key
|
|
48
|
+
nonce (str or bytes) : Nonce
|
|
49
|
+
assoc_data (str or bytes): Associated data
|
|
50
|
+
cipher_text (bytes) : Cipher text
|
|
51
|
+
tag (bytes) : Tag
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
bytes: Decrypted data
|
|
55
|
+
"""
|
|
56
|
+
cipher = ChaCha20_Poly1305.new(key=AlgoUtils.Encode(key),
|
|
57
|
+
nonce=AlgoUtils.Encode(nonce))
|
|
58
|
+
cipher.update(AlgoUtils.Encode(assoc_data))
|
|
59
|
+
return cipher.decrypt_and_verify(AlgoUtils.Encode(cipher_text), AlgoUtils.Encode(tag))
|
|
60
|
+
|
|
61
|
+
@staticmethod
|
|
62
|
+
def Encrypt(key: Union[bytes, str],
|
|
63
|
+
nonce: Union[bytes, str],
|
|
64
|
+
assoc_data: Union[bytes, str],
|
|
65
|
+
plain_text: Union[bytes, str]) -> Tuple[bytes, bytes]:
|
|
66
|
+
"""
|
|
67
|
+
Encrypt data.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
key (str or bytes) : Key
|
|
71
|
+
nonce (str or bytes) : Nonce
|
|
72
|
+
assoc_data (str or bytes): Associated data
|
|
73
|
+
plain_text (str or bytes): Plain text
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
tuple[bytes, bytes]: Cipher text bytes (index 0) and tag bytes (index 1)
|
|
77
|
+
"""
|
|
78
|
+
cipher = ChaCha20_Poly1305.new(key=AlgoUtils.Encode(key),
|
|
79
|
+
nonce=AlgoUtils.Encode(nonce))
|
|
80
|
+
cipher.update(AlgoUtils.Encode(assoc_data))
|
|
81
|
+
return cipher.encrypt_and_digest(AlgoUtils.Encode(plain_text))
|
|
82
|
+
|
|
83
|
+
@staticmethod
|
|
84
|
+
def KeySize() -> int:
|
|
85
|
+
"""
|
|
86
|
+
Get the key size.
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
int: Key size
|
|
90
|
+
"""
|
|
91
|
+
return ChaCha20_Poly1305.key_size
|
|
92
|
+
|
|
93
|
+
@staticmethod
|
|
94
|
+
def TagSize() -> int:
|
|
95
|
+
"""
|
|
96
|
+
Get the tag size.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
int: Tag size
|
|
100
|
+
"""
|
|
101
|
+
return 16
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Copyright (c) 2022 Emanuele Bellocchia
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
# furnished to do so, subject to the following conditions:
|
|
9
|
+
#
|
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
|
11
|
+
# all copies or substantial portions of the Software.
|
|
12
|
+
#
|
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
# THE SOFTWARE.
|
|
20
|
+
|
|
21
|
+
"""Module for HASH160 algorithm."""
|
|
22
|
+
|
|
23
|
+
# Imports
|
|
24
|
+
from typing import Union
|
|
25
|
+
|
|
26
|
+
from .ripemd import Ripemd160
|
|
27
|
+
from .sha2 import Sha256
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class Hash160:
|
|
31
|
+
"""
|
|
32
|
+
HASH160 class.
|
|
33
|
+
It computes digests using HASH160 algorithm.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
@staticmethod
|
|
37
|
+
def QuickDigest(data: Union[bytes, str]) -> bytes:
|
|
38
|
+
"""
|
|
39
|
+
Compute the digest (quick version).
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
data (str or bytes): Data
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
bytes: Computed digest
|
|
46
|
+
"""
|
|
47
|
+
return Ripemd160.QuickDigest(Sha256.QuickDigest(data))
|
|
48
|
+
|
|
49
|
+
@staticmethod
|
|
50
|
+
def DigestSize() -> int:
|
|
51
|
+
"""
|
|
52
|
+
Get the digest size in bytes.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
int: Digest size in bytes
|
|
56
|
+
"""
|
|
57
|
+
return Ripemd160.DigestSize()
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# Copyright (c) 2022 Emanuele Bellocchia
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
# furnished to do so, subject to the following conditions:
|
|
9
|
+
#
|
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
|
11
|
+
# all copies or substantial portions of the Software.
|
|
12
|
+
#
|
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
# THE SOFTWARE.
|
|
20
|
+
|
|
21
|
+
"""Module for SHA-2 algorithms."""
|
|
22
|
+
|
|
23
|
+
# Imports
|
|
24
|
+
import hashlib
|
|
25
|
+
import hmac
|
|
26
|
+
from typing import Tuple, Union
|
|
27
|
+
|
|
28
|
+
from ..misc import AlgoUtils
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
HMAC_USE_DIGEST: bool = hasattr(hmac, "digest")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class HmacSha256:
|
|
35
|
+
"""
|
|
36
|
+
HMAC-SHA256 class.
|
|
37
|
+
It computes digests using HMAC-SHA256 algorithm.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
@staticmethod
|
|
41
|
+
def QuickDigest(key: Union[bytes, str],
|
|
42
|
+
data: Union[bytes, str]) -> bytes:
|
|
43
|
+
"""
|
|
44
|
+
Compute the digest (quick version).
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
key (str or bytes) : Key
|
|
48
|
+
data (str or bytes): Data
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
bytes: Computed digest
|
|
52
|
+
"""
|
|
53
|
+
# Use digest if available
|
|
54
|
+
if HMAC_USE_DIGEST:
|
|
55
|
+
return hmac.digest(AlgoUtils.Encode(key), AlgoUtils.Encode(data), "sha256")
|
|
56
|
+
return hmac.new(AlgoUtils.Encode(key), AlgoUtils.Encode(data), hashlib.sha256).digest()
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def DigestSize() -> int:
|
|
60
|
+
"""
|
|
61
|
+
Get the digest size in bytes.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
int: Digest size in bytes
|
|
65
|
+
"""
|
|
66
|
+
return hashlib.sha256().digest_size
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class HmacSha512:
|
|
70
|
+
"""
|
|
71
|
+
HMAC-SHA512 class.
|
|
72
|
+
It computes digests using HMAC-SHA512 algorithm.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
@staticmethod
|
|
76
|
+
def QuickDigest(key: Union[bytes, str],
|
|
77
|
+
data: Union[bytes, str]) -> bytes:
|
|
78
|
+
"""
|
|
79
|
+
Compute the digest (quick version).
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
key (str or bytes) : Key
|
|
83
|
+
data (str or bytes): Data
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
bytes: Computed digest
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
# Use digest if available
|
|
90
|
+
if HMAC_USE_DIGEST:
|
|
91
|
+
return hmac.digest(AlgoUtils.Encode(key), AlgoUtils.Encode(data), "sha512")
|
|
92
|
+
return hmac.new(AlgoUtils.Encode(key), AlgoUtils.Encode(data), hashlib.sha512).digest()
|
|
93
|
+
|
|
94
|
+
@staticmethod
|
|
95
|
+
def QuickDigestHalves(key: Union[bytes, str],
|
|
96
|
+
data: Union[bytes, str]) -> Tuple[bytes, bytes]:
|
|
97
|
+
"""
|
|
98
|
+
Compute the digest and return it split into two halves (quick version).
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
key (str or bytes) : Key
|
|
102
|
+
data (str or bytes): Data
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
tuple[bytes, bytes]: Computed digest left part (index 0) and right part (index 1)
|
|
106
|
+
"""
|
|
107
|
+
digest_bytes = HmacSha512.QuickDigest(key, data)
|
|
108
|
+
return digest_bytes[:HmacSha512.DigestSize() // 2], digest_bytes[HmacSha512.DigestSize() // 2:]
|
|
109
|
+
|
|
110
|
+
@staticmethod
|
|
111
|
+
def DigestSize() -> int:
|
|
112
|
+
"""
|
|
113
|
+
Get the digest size in bytes.
|
|
114
|
+
|
|
115
|
+
Returns:
|
|
116
|
+
int: Digest size in bytes
|
|
117
|
+
"""
|
|
118
|
+
return hashlib.sha512().digest_size
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Copyright (c) 2022 Emanuele Bellocchia
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
# furnished to do so, subject to the following conditions:
|
|
9
|
+
#
|
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
|
11
|
+
# all copies or substantial portions of the Software.
|
|
12
|
+
#
|
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
# THE SOFTWARE.
|
|
20
|
+
|
|
21
|
+
"""Module for PBKDF2 algorithm."""
|
|
22
|
+
|
|
23
|
+
# Imports
|
|
24
|
+
import hashlib
|
|
25
|
+
from typing import Optional, Union
|
|
26
|
+
|
|
27
|
+
from Crypto.Hash import SHA512
|
|
28
|
+
from Crypto.Protocol.KDF import PBKDF2
|
|
29
|
+
|
|
30
|
+
from ..misc import AlgoUtils
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
HASHLIB_USE_PBKDF2_SHA512: bool = hasattr(hashlib, "pbkdf2_hmac") # For future changes
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class Pbkdf2HmacSha512:
|
|
37
|
+
"""
|
|
38
|
+
PBKDF2 HMAC-SHA512 class.
|
|
39
|
+
It derives keys using PBKDF2 HMAC-SHA512 algorithm.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
@staticmethod
|
|
43
|
+
def DeriveKey(password: Union[bytes, str],
|
|
44
|
+
salt: Union[bytes, str],
|
|
45
|
+
itr_num: int,
|
|
46
|
+
dklen: Optional[int] = None) -> bytes:
|
|
47
|
+
"""
|
|
48
|
+
Derive a key.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
password (str or bytes): Password
|
|
52
|
+
salt (str or bytes) : Salt
|
|
53
|
+
itr_num (int) : Iteration number
|
|
54
|
+
dklen (int, optional) : Length of the derived key (default: SHA-512 output length)
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
bytes: Computed result
|
|
58
|
+
"""
|
|
59
|
+
if HASHLIB_USE_PBKDF2_SHA512:
|
|
60
|
+
return hashlib.pbkdf2_hmac("sha512", AlgoUtils.Encode(password), AlgoUtils.Encode(salt), itr_num, dklen)
|
|
61
|
+
# Use Cryptodome if not implemented in hashlib
|
|
62
|
+
return PBKDF2(AlgoUtils.Encode(password), # type: ignore [arg-type]
|
|
63
|
+
AlgoUtils.Encode(salt),
|
|
64
|
+
dklen or SHA512.digest_size,
|
|
65
|
+
count=itr_num,
|
|
66
|
+
hmac_hash_module=SHA512)
|