From aacbcc854a10b2e54a42d0e32925106bf3e30b10 Mon Sep 17 00:00:00 2001 From: Moul <moul@moul.re> Date: Sun, 23 Feb 2020 12:58:05 +0100 Subject: [PATCH] [enh] crypto: define regex patterns on top of the files - Pubkey pattern defined in constants - Merge search and compline on the same line --- silkaj/auth.py | 16 ++++++++++------ silkaj/constants.py | 1 + silkaj/crypto_tools.py | 14 ++++++++------ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/silkaj/auth.py b/silkaj/auth.py index d80fc729..b2161a7d 100644 --- a/silkaj/auth.py +++ b/silkaj/auth.py @@ -24,6 +24,11 @@ from duniterpy.key import SigningKey from duniterpy.key.scrypt_params import ScryptParams from silkaj.tools import message_exit +from silkaj.constants import PUBKEY_PATTERN + +SEED_HEX_PATTERN = "^[0-9a-fA-F]{64}$" +PUBSEC_PUBKEY_PATTERN = "pub: ({0})".format(PUBKEY_PATTERN) +PUBSEC_SIGNKEY_PATTERN = "sec: ([1-9A-HJ-NP-Za-km-z]{87,90})" @pass_context @@ -73,14 +78,13 @@ def auth_by_auth_file(ctx): if not authfile.is_file(): message_exit('Error: the file "' + file + '" does not exist') filetxt = authfile.open("r").read() - # regex for seed (hexadecimal) - regex_seed = re.compile("^[0-9a-fA-F]{64}$") + # two regural expressions for the PubSec format - regex_pubkey = re.compile("pub: ([1-9A-HJ-NP-Za-km-z]{43,44})", re.MULTILINE) - regex_signkey = re.compile("sec: ([1-9A-HJ-NP-Za-km-z]{87,90})", re.MULTILINE) + regex_pubkey = re.compile(PUBSEC_PUBKEY_PATTERN, re.MULTILINE) + regex_signkey = re.compile(PUBSEC_SIGNKEY_PATTERN, re.MULTILINE) - # Seed format - if re.search(regex_seed, filetxt): + # Seed hexadecimal format + if re.search(re.compile(SEED_HEX_PATTERN), filetxt): return SigningKey.from_seedhex_file(file) # PubSec format elif re.search(regex_pubkey, filetxt) and re.search(regex_signkey, filetxt): diff --git a/silkaj/constants.py b/silkaj/constants.py index 17185490..1d4c3574 100644 --- a/silkaj/constants.py +++ b/silkaj/constants.py @@ -26,3 +26,4 @@ SOURCES_PER_TX = 40 SUCCESS_EXIT_STATUS = 0 FAILURE_EXIT_STATUS = 1 BMA_MAX_BLOCKS_CHUNK_SIZE = 5000 +PUBKEY_PATTERN = "[1-9A-HJ-NP-Za-km-z]{43,44}" diff --git a/silkaj/crypto_tools.py b/silkaj/crypto_tools.py index af8726f5..ae28606e 100644 --- a/silkaj/crypto_tools.py +++ b/silkaj/crypto_tools.py @@ -18,6 +18,12 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. import re from nacl import encoding, hash +from silkaj.constants import PUBKEY_PATTERN + +PUBKEY_DELIMITED_PATTERN = "^{0}$".format(PUBKEY_PATTERN) +CHECKSUM_PATTERN = "[1-9A-HJ-NP-Za-km-z]{3}" +PUBKEY_CHECKSUM_PATTERN = "^{0}!{1}$".format(PUBKEY_PATTERN, CHECKSUM_PATTERN) + def check_public_key(pubkey, display_error): """ @@ -25,13 +31,9 @@ def check_public_key(pubkey, display_error): Check pubkey checksum which could be append after the pubkey If check pass: return pubkey """ - regex = re.compile("^[1-9A-HJ-NP-Za-km-z]{43,44}$") - regex_checksum = re.compile( - "^[1-9A-HJ-NP-Za-km-z]{43,44}" + "![1-9A-HJ-NP-Za-km-z]{3}$" - ) - if re.search(regex, pubkey): + if re.search(re.compile(PUBKEY_DELIMITED_PATTERN), pubkey): return pubkey - elif re.search(regex_checksum, pubkey): + elif re.search(re.compile(PUBKEY_CHECKSUM_PATTERN), pubkey): pubkey, checksum = pubkey.split("!") pubkey_byte = b58_decode(pubkey) checksum_calculed = b58_encode( -- GitLab