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,58 @@
|
|
|
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 RIPEMD algorithm."""
|
|
22
|
+
|
|
23
|
+
# Imports
|
|
24
|
+
from typing import Union
|
|
25
|
+
|
|
26
|
+
from Crypto.Hash import RIPEMD160
|
|
27
|
+
|
|
28
|
+
from ..misc import AlgoUtils
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class Ripemd160:
|
|
32
|
+
"""
|
|
33
|
+
RIPEMD160 class.
|
|
34
|
+
It computes digests using RIPEMD160 algorithm.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def QuickDigest(data: Union[bytes, str]) -> bytes:
|
|
39
|
+
"""
|
|
40
|
+
Compute the digest (quick version).
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
data (str or bytes): Data
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
bytes: Computed digest
|
|
47
|
+
"""
|
|
48
|
+
return RIPEMD160.new(AlgoUtils.Encode(data)).digest()
|
|
49
|
+
|
|
50
|
+
@staticmethod
|
|
51
|
+
def DigestSize() -> int:
|
|
52
|
+
"""
|
|
53
|
+
Get the digest size in bytes.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
int: Digest size in bytes
|
|
57
|
+
"""
|
|
58
|
+
return RIPEMD160.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 Scrypt algorithm."""
|
|
22
|
+
|
|
23
|
+
# Imports
|
|
24
|
+
from typing import Union
|
|
25
|
+
|
|
26
|
+
from Crypto.Protocol.KDF import scrypt
|
|
27
|
+
|
|
28
|
+
from ..misc import AlgoUtils
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class Scrypt:
|
|
32
|
+
"""
|
|
33
|
+
Scrypt class.
|
|
34
|
+
It derives key using Scrypt algorithm.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def DeriveKey(password: Union[bytes, str], # pylint: disable=too-many-arguments
|
|
39
|
+
salt: Union[bytes, str],
|
|
40
|
+
key_len: int,
|
|
41
|
+
n: int,
|
|
42
|
+
r: int,
|
|
43
|
+
p: int) -> bytes:
|
|
44
|
+
"""
|
|
45
|
+
Derive a key.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
password (str or bytes): Password
|
|
49
|
+
salt (str or bytes) : Salt
|
|
50
|
+
key_len (int) : Length of the derived key
|
|
51
|
+
n (int) : CPU/Memory cost parameter
|
|
52
|
+
r (int) : Block size parameter
|
|
53
|
+
p (int) : Parallelization parameter
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
bytes: Computed result
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
# Type for password and salt should be Union[bytes, str] in pycryptodome, but it's only str
|
|
60
|
+
# So, we ignore the mypy warning
|
|
61
|
+
return scrypt(AlgoUtils.Encode(password), # type: ignore [arg-type, return-value]
|
|
62
|
+
AlgoUtils.Encode(salt), # type: ignore [arg-type]
|
|
63
|
+
key_len=key_len,
|
|
64
|
+
N=n,
|
|
65
|
+
r=r,
|
|
66
|
+
p=p)
|
|
@@ -0,0 +1,182 @@
|
|
|
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
|
+
from typing import Any, Union
|
|
26
|
+
|
|
27
|
+
from Crypto.Hash import SHA512
|
|
28
|
+
|
|
29
|
+
from ..misc import AlgoUtils
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
HASHLIB_USE_SHA512_256: bool = "sha512_256" in hashlib.algorithms_available
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Sha256:
|
|
36
|
+
"""
|
|
37
|
+
SHA256 class.
|
|
38
|
+
It computes digests using SHA256 algorithm.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
handle: Any
|
|
42
|
+
|
|
43
|
+
def __init__(self) -> None:
|
|
44
|
+
"""Construct class."""
|
|
45
|
+
self.handle = hashlib.sha256()
|
|
46
|
+
|
|
47
|
+
def Update(self,
|
|
48
|
+
data_bytes: bytes) -> None:
|
|
49
|
+
"""
|
|
50
|
+
Update digest.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
data_bytes (bytes): Data bytes
|
|
54
|
+
"""
|
|
55
|
+
self.handle.update(data_bytes)
|
|
56
|
+
|
|
57
|
+
def Digest(self) -> bytes:
|
|
58
|
+
"""
|
|
59
|
+
Get the computed digest.
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
bytes: Computed digest
|
|
63
|
+
"""
|
|
64
|
+
return self.handle.digest()
|
|
65
|
+
|
|
66
|
+
@staticmethod
|
|
67
|
+
def QuickDigest(data: Union[bytes, str]) -> bytes:
|
|
68
|
+
"""
|
|
69
|
+
Compute the digest (quick version).
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
data (str or bytes): Data
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
bytes: Computed digest
|
|
76
|
+
"""
|
|
77
|
+
return hashlib.sha256(AlgoUtils.Encode(data)).digest()
|
|
78
|
+
|
|
79
|
+
@staticmethod
|
|
80
|
+
def DigestSize() -> int:
|
|
81
|
+
"""
|
|
82
|
+
Get the digest size in bytes.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
int: Digest size in bytes
|
|
86
|
+
"""
|
|
87
|
+
return hashlib.sha256().digest_size
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class DoubleSha256:
|
|
91
|
+
"""
|
|
92
|
+
Double SHA256 class.
|
|
93
|
+
It computes digests using SHA256 algorithm twice.
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
@staticmethod
|
|
97
|
+
def QuickDigest(data: Union[bytes, str]) -> bytes:
|
|
98
|
+
"""
|
|
99
|
+
Compute the digest (quick version).
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
data (str or bytes): Data
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
bytes: Computed digest
|
|
106
|
+
"""
|
|
107
|
+
return Sha256.QuickDigest(Sha256.QuickDigest(data))
|
|
108
|
+
|
|
109
|
+
@staticmethod
|
|
110
|
+
def DigestSize() -> int:
|
|
111
|
+
"""
|
|
112
|
+
Get the digest size in bytes.
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
int: Digest size in bytes
|
|
116
|
+
"""
|
|
117
|
+
return Sha256.DigestSize()
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class Sha512:
|
|
121
|
+
"""
|
|
122
|
+
SHA512 class.
|
|
123
|
+
It computes digests using SHA512 algorithm.
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
@staticmethod
|
|
127
|
+
def QuickDigest(data: Union[bytes, str]) -> bytes:
|
|
128
|
+
"""
|
|
129
|
+
Compute the digest (quick version).
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
data (str or bytes): Data
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
bytes: Computed digest
|
|
136
|
+
"""
|
|
137
|
+
return hashlib.sha512(AlgoUtils.Encode(data)).digest()
|
|
138
|
+
|
|
139
|
+
@staticmethod
|
|
140
|
+
def DigestSize() -> int:
|
|
141
|
+
"""
|
|
142
|
+
Get the digest size in bytes.
|
|
143
|
+
|
|
144
|
+
Returns:
|
|
145
|
+
int: Digest size in bytes
|
|
146
|
+
"""
|
|
147
|
+
return hashlib.sha512().digest_size
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class Sha512_256: # noqa: N801
|
|
151
|
+
"""
|
|
152
|
+
SHA512/256 class.
|
|
153
|
+
It computes digests using SHA512/256 algorithm.
|
|
154
|
+
"""
|
|
155
|
+
|
|
156
|
+
@staticmethod
|
|
157
|
+
def QuickDigest(data: Union[bytes, str]) -> bytes:
|
|
158
|
+
"""
|
|
159
|
+
Compute the digest (quick version).
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
data (str or bytes): Data
|
|
163
|
+
|
|
164
|
+
Returns:
|
|
165
|
+
bytes: Computed digest
|
|
166
|
+
"""
|
|
167
|
+
if HASHLIB_USE_SHA512_256:
|
|
168
|
+
return hashlib.new("sha512_256", AlgoUtils.Encode(data)).digest()
|
|
169
|
+
# Use Cryptodome if not implemented in hashlib
|
|
170
|
+
return SHA512.new(AlgoUtils.Encode(data), truncate="256").digest()
|
|
171
|
+
|
|
172
|
+
@staticmethod
|
|
173
|
+
def DigestSize() -> int:
|
|
174
|
+
"""
|
|
175
|
+
Get the digest size in bytes.
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
int: Digest size in bytes
|
|
179
|
+
"""
|
|
180
|
+
return (hashlib.new("sha512_256").digest_size
|
|
181
|
+
if HASHLIB_USE_SHA512_256
|
|
182
|
+
else SHA512.new(truncate="256").digest_size)
|
|
@@ -0,0 +1,99 @@
|
|
|
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-3 algorithms."""
|
|
22
|
+
|
|
23
|
+
# Imports
|
|
24
|
+
import hashlib
|
|
25
|
+
from typing import Union
|
|
26
|
+
|
|
27
|
+
from Crypto.Hash import SHA3_256, keccak
|
|
28
|
+
|
|
29
|
+
from ..misc import AlgoUtils
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
HASHLIB_USE_SHA3_256: bool = "sha3_256" in hashlib.algorithms_available
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Kekkak256:
|
|
36
|
+
"""
|
|
37
|
+
Kekkak-256 class.
|
|
38
|
+
It computes digests using Kekkak-256 algorithm.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
@staticmethod
|
|
42
|
+
def QuickDigest(data: Union[bytes, str]) -> bytes:
|
|
43
|
+
"""
|
|
44
|
+
Compute the digest (quick version).
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
data (str or bytes): Data
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
bytes: Computed digest
|
|
51
|
+
"""
|
|
52
|
+
return keccak.new(data=AlgoUtils.Encode(data), digest_bits=256).digest()
|
|
53
|
+
|
|
54
|
+
@staticmethod
|
|
55
|
+
def DigestSize() -> int:
|
|
56
|
+
"""
|
|
57
|
+
Get the digest size in bytes.
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
int: Digest size in bytes
|
|
61
|
+
"""
|
|
62
|
+
return (hashlib.new("sha3_256").digest_size
|
|
63
|
+
if HASHLIB_USE_SHA3_256
|
|
64
|
+
else keccak.new(digest_bits=256).digest_size)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class Sha3_256: # noqa: N801
|
|
68
|
+
"""
|
|
69
|
+
SHA3-256 class.
|
|
70
|
+
It computes digests using SHA3-256 algorithm.
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
@staticmethod
|
|
74
|
+
def QuickDigest(data: Union[bytes, str]) -> bytes:
|
|
75
|
+
"""
|
|
76
|
+
Compute the digest (quick version).
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
data (str or bytes): Data
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
bytes: Computed digest
|
|
83
|
+
"""
|
|
84
|
+
if HASHLIB_USE_SHA3_256:
|
|
85
|
+
return hashlib.new("sha3_256", AlgoUtils.Encode(data)).digest()
|
|
86
|
+
# Use Cryptodome if not implemented in hashlib
|
|
87
|
+
return SHA3_256.new(AlgoUtils.Encode(data)).digest()
|
|
88
|
+
|
|
89
|
+
@staticmethod
|
|
90
|
+
def DigestSize() -> int:
|
|
91
|
+
"""
|
|
92
|
+
Get the digest size in bytes.
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
int: Digest size in bytes
|
|
96
|
+
"""
|
|
97
|
+
return (hashlib.new("sha3_256").digest_size
|
|
98
|
+
if HASHLIB_USE_SHA3_256
|
|
99
|
+
else SHA3_256.digest_size)
|
|
@@ -0,0 +1,108 @@
|
|
|
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 with some algorithm utility functions."""
|
|
22
|
+
|
|
23
|
+
# Imports
|
|
24
|
+
from bisect import bisect_left
|
|
25
|
+
from typing import Any, List, Union
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class AlgoUtils:
|
|
29
|
+
"""Class container for algorithm utility functions."""
|
|
30
|
+
|
|
31
|
+
@staticmethod
|
|
32
|
+
def BinarySearch(arr: List,
|
|
33
|
+
elem: Any) -> int:
|
|
34
|
+
"""
|
|
35
|
+
Binary search algorithm simply implemented by using the bisect library.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
arr (list): list of elements
|
|
39
|
+
elem (any): element to be searched
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
int: First index of the element, -1 if not found
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
invalid_idx = -1
|
|
46
|
+
|
|
47
|
+
i = bisect_left(arr, elem)
|
|
48
|
+
if i != len(arr) and arr[i] == elem:
|
|
49
|
+
return i
|
|
50
|
+
|
|
51
|
+
return invalid_idx
|
|
52
|
+
|
|
53
|
+
@staticmethod
|
|
54
|
+
def Decode(data: Union[bytes, str],
|
|
55
|
+
encoding: str = "utf-8") -> str:
|
|
56
|
+
"""
|
|
57
|
+
Decode from bytes.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
data (str or bytes): Data
|
|
61
|
+
encoding (str) : Encoding type
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
str: String encoded to bytes
|
|
65
|
+
|
|
66
|
+
Raises:
|
|
67
|
+
TypeError: If the data is neither string nor bytes
|
|
68
|
+
"""
|
|
69
|
+
if isinstance(data, str):
|
|
70
|
+
return data
|
|
71
|
+
if isinstance(data, bytes):
|
|
72
|
+
return data.decode(encoding)
|
|
73
|
+
raise TypeError("Invalid data type")
|
|
74
|
+
|
|
75
|
+
@staticmethod
|
|
76
|
+
def Encode(data: Union[bytes, str],
|
|
77
|
+
encoding: str = "utf-8") -> bytes:
|
|
78
|
+
"""
|
|
79
|
+
Encode to bytes.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
data (str or bytes): Data
|
|
83
|
+
encoding (str) : Encoding type
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
bytes: String encoded to bytes
|
|
87
|
+
|
|
88
|
+
Raises:
|
|
89
|
+
TypeError: If the data is neither string nor bytes
|
|
90
|
+
"""
|
|
91
|
+
if isinstance(data, str):
|
|
92
|
+
return data.encode(encoding)
|
|
93
|
+
if isinstance(data, bytes):
|
|
94
|
+
return data
|
|
95
|
+
raise TypeError("Invalid data type")
|
|
96
|
+
|
|
97
|
+
@staticmethod
|
|
98
|
+
def IsStringMixed(data_str: str) -> bool:
|
|
99
|
+
"""
|
|
100
|
+
Get if the specified string is in mixed case.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
data_str (str): string
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
bool: True if mixed case, false otherwise
|
|
107
|
+
"""
|
|
108
|
+
return any(c.islower() for c in data_str) and any(c.isupper() for c in data_str)
|
|
@@ -0,0 +1,151 @@
|
|
|
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 with helper class for Base32."""
|
|
22
|
+
|
|
23
|
+
# Imports
|
|
24
|
+
import base64
|
|
25
|
+
import binascii
|
|
26
|
+
from typing import Optional, Union
|
|
27
|
+
|
|
28
|
+
from .algo import AlgoUtils
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class Base32Const:
|
|
32
|
+
"""Class container for Base32 constants."""
|
|
33
|
+
|
|
34
|
+
# Alphabet
|
|
35
|
+
ALPHABET: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
|
|
36
|
+
# Padding character
|
|
37
|
+
PADDING_CHAR: str = "="
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class _Base32Utils:
|
|
41
|
+
"""
|
|
42
|
+
Base32 utility class.
|
|
43
|
+
It provides some helper methods for decoding/encoding Base32 format.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
@staticmethod
|
|
47
|
+
def AddPadding(data: str) -> str:
|
|
48
|
+
"""
|
|
49
|
+
Add padding to an encoded Base32 string.
|
|
50
|
+
Used if the string was encoded with Base32Encoder.EncodeNoPadding
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
data (str): Data
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
str: Padded string
|
|
57
|
+
"""
|
|
58
|
+
last_block_width = len(data) % 8
|
|
59
|
+
if last_block_width != 0:
|
|
60
|
+
data += (8 - last_block_width) * Base32Const.PADDING_CHAR
|
|
61
|
+
return data
|
|
62
|
+
|
|
63
|
+
@staticmethod
|
|
64
|
+
def TranslateAlphabet(data: str,
|
|
65
|
+
from_alphabet: str,
|
|
66
|
+
to_alphabet: str) -> str:
|
|
67
|
+
"""
|
|
68
|
+
Translate the standard Base32 alphabet to a custom one.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
data (str) : Data
|
|
72
|
+
from_alphabet (str): Starting alphabet string
|
|
73
|
+
to_alphabet (str) : Final alphabet string
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
str: String with translated alphabet
|
|
77
|
+
"""
|
|
78
|
+
return data.translate(str.maketrans(from_alphabet, to_alphabet))
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class Base32Decoder:
|
|
82
|
+
"""
|
|
83
|
+
Base32 decoder class.
|
|
84
|
+
It provides methods for decoding to Base32 format.
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
@staticmethod
|
|
88
|
+
def Decode(data: str,
|
|
89
|
+
custom_alphabet: Optional[str] = None) -> bytes:
|
|
90
|
+
"""
|
|
91
|
+
Decode from Base32.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
data (str) : Data
|
|
95
|
+
custom_alphabet (str, optional): Custom alphabet string
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
bytes: Decoded bytes
|
|
99
|
+
|
|
100
|
+
Raises:
|
|
101
|
+
ValueError: If the Base32 string is not valid
|
|
102
|
+
"""
|
|
103
|
+
try:
|
|
104
|
+
data_dec = _Base32Utils.AddPadding(data)
|
|
105
|
+
if custom_alphabet is not None:
|
|
106
|
+
data_dec = _Base32Utils.TranslateAlphabet(data_dec, custom_alphabet, Base32Const.ALPHABET)
|
|
107
|
+
|
|
108
|
+
return base64.b32decode(data_dec)
|
|
109
|
+
except binascii.Error as ex:
|
|
110
|
+
raise ValueError("Invalid Base32 string") from ex
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class Base32Encoder:
|
|
114
|
+
"""
|
|
115
|
+
Base32 encoder class.
|
|
116
|
+
It provides methods for encoding to Base32 format.
|
|
117
|
+
"""
|
|
118
|
+
|
|
119
|
+
@staticmethod
|
|
120
|
+
def Encode(data: Union[bytes, str],
|
|
121
|
+
custom_alphabet: Optional[str] = None) -> str:
|
|
122
|
+
"""
|
|
123
|
+
Encode to Base32.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
data (str or bytes) : Data
|
|
127
|
+
custom_alphabet (str, optional): Custom alphabet string
|
|
128
|
+
|
|
129
|
+
Returns:
|
|
130
|
+
str: Encoded string
|
|
131
|
+
"""
|
|
132
|
+
b32_enc = AlgoUtils.Decode(base64.b32encode(AlgoUtils.Encode(data)))
|
|
133
|
+
if custom_alphabet is not None:
|
|
134
|
+
b32_enc = _Base32Utils.TranslateAlphabet(b32_enc, Base32Const.ALPHABET, custom_alphabet)
|
|
135
|
+
|
|
136
|
+
return b32_enc
|
|
137
|
+
|
|
138
|
+
@staticmethod
|
|
139
|
+
def EncodeNoPadding(data: Union[bytes, str],
|
|
140
|
+
custom_alphabet: Optional[str] = None) -> str:
|
|
141
|
+
"""
|
|
142
|
+
Encode to Base32 by removing the final padding.
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
data (str or bytes) : Data
|
|
146
|
+
custom_alphabet (str, optional): Custom alphabet string
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
str: Encoded string
|
|
150
|
+
"""
|
|
151
|
+
return Base32Encoder.Encode(data, custom_alphabet).rstrip(Base32Const.PADDING_CHAR)
|