Skip to content
Snippets Groups Projects
Commit aacbcc85 authored by Moul's avatar Moul Committed by matograine
Browse files

[enh] crypto: define regex patterns on top of the files

- Pubkey pattern defined in constants
- Merge search and compline on the same line
parent 5870c5b7
No related branches found
No related tags found
2 merge requests!146Merge dev into master branch to complete v0.8.0 development cycle,!128[fix] #278: changing regex for PubSec authfile authentication
Pipeline #8197 passed
......@@ -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):
......
......@@ -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}"
......@@ -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(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment