Commit abe48c03 authored by Vincent Texier's avatar Vincent Texier

[fix] #78 Fix unit tests (Break BC)

Refactor scrypt constants in a new key.constants module
parent 717f2247
......@@ -7,12 +7,12 @@ HASH_REGEX = "[A-F0-9]{64}"
BLOCK_ID_REGEX = "[0-9]+"
BLOCK_UID_REGEX = "{block_id_regex}-{block_hash_regex}".format(block_id_regex=BLOCK_ID_REGEX,
block_hash_regex=BLOCK_HASH_REGEX)
CONDITIONS_REGEX = "(&&|\|\|| |[()]|(SIG\({pubkey_regex}\)|(XHX\({hash_regex}\))))*" \
CONDITIONS_REGEX = "(&&|\\|\\|| |[()]|(SIG\\({pubkey_regex}\\)|(XHX\\({hash_regex}\\))))*" \
.format(pubkey_regex=PUBKEY_REGEX, hash_regex=HASH_REGEX)
IPV4_REGEX = '(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][' \
IPV4_REGEX = '(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][' \
'0-9]|25[0-5])'
IPV6_REGEX = '(?:(?:[0-9A-Fa-f]{1,4}:){6}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[' \
'0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|::(?:[0-9A-Fa-f]{1,' \
'0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|::(?:[0-9A-Fa-f]{1,' \
'4}:){5}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){' \
'3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,' \
'4}:){4}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){' \
......@@ -28,7 +28,7 @@ IPV6_REGEX = '(?:(?:[0-9A-Fa-f]{1,4}:){6}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?
'0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,' \
'4}:){,6}[0-9A-Fa-f]{1,4})?::)(?:%.+)?'
HOST_REGEX = "[a-z0-9-_.]*(?:.[a-zA-Z])?"
PATH_REGEX = "[/\w \.-]*/?"
PATH_REGEX = "[/\\w \\.-]*/?"
WS2PID_REGEX = "[0-9a-f]{8}"
WS2P_PRIVATE_PREFIX_REGEX = "O[CT][SAM]"
WS2P_PUBLIC_PREFIX_REGEX = "I[CT]"
......
from .signing_key import SigningKey
from .verifying_key import VerifyingKey
from .encryption_key import SecretKey, PublicKey, SCRYPT_PARAMS, SEED_LENGTH
from .encryption_key import SecretKey, PublicKey
from .ascii_armor import AsciiArmor
......@@ -3,7 +3,8 @@ import libnacl
from re import compile
from typing import Optional, List, Dict, Any
from duniterpy.key import SigningKey, PublicKey, VerifyingKey, SCRYPT_PARAMS, SEED_LENGTH
from duniterpy.key import SigningKey, PublicKey, VerifyingKey
from .constants import SCRYPT_PARAMS, SEED_LENGTH
# Headers constants
BEGIN_MESSAGE_HEADER = "-----BEGIN DUNITER MESSAGE-----"
......@@ -158,7 +159,18 @@ Scrypt: {script_params}
def parse(ascii_armor_block: str, signing_key: Optional[SigningKey] = None,
sender_pubkeys: Optional[List[str]] = None) -> dict:
"""
Return a dict with parsed content
Return a dict with parsed content (decrypted message, signature validation)
{
'message':
{
'fields': {},
'content': str,
},
'signatures': [
{'pubkey': str, 'valid': bool, fields: {}}
]
}
:param ascii_armor_block: The Ascii Armor Message Block including BEGIN and END headers
:param signing_key: Optional Libnacl SigningKey instance to decrypt message
......
# Libnacl
crypto_sign_BYTES = 64
# Scrypt
SEED_LENGTH = 32
SCRYPT_PARAMS = {'N': 4096,
'r': 16,
'p': 1
}
......@@ -10,13 +10,7 @@ from pylibscrypt import scrypt
from .base58 import Base58Encoder
from ..helpers import ensure_bytes
SEED_LENGTH = 32 # Length of the key
crypto_sign_BYTES = 64
SCRYPT_PARAMS = {'N': 4096,
'r': 16,
'p': 1
}
from .constants import SEED_LENGTH, SCRYPT_PARAMS
class SecretKey(libnacl.public.SecretKey):
......
......@@ -4,19 +4,17 @@ duniter public and private keys
@author: inso
"""
from re import compile, MULTILINE, search
from typing import Optional, Union, TypeVar, Type
from typing import Optional, Union, TypeVar, Type, Dict
import libnacl.sign
import pyaes
from libnacl.utils import load_key
from pylibscrypt import scrypt
from duniterpy.key.constants import SEED_LENGTH, SCRYPT_PARAMS
from .base58 import Base58Encoder
from ..helpers import ensure_bytes, xor_bytes, convert_seedhex_to_seed, convert_seed_to_seedhex
SEED_LENGTH = 32 # Length of the key
crypto_sign_BYTES = 64
class ScryptParams:
def __init__(self, n: int, r: int, p: int) -> None:
......@@ -49,7 +47,7 @@ class SigningKey(libnacl.sign.Signer):
@classmethod
def from_credentials(cls: Type[SigningKeyType], salt: Union[str, bytes], password: Union[str, bytes],
scrypt_params: Optional[ScryptParams] = None) -> SigningKeyType:
scrypt_params: Optional[Dict[str, int]] = None) -> SigningKeyType:
"""
Create a SigningKey object from credentials
......@@ -58,12 +56,12 @@ class SigningKey(libnacl.sign.Signer):
:param scrypt_params: ScryptParams instance
"""
if scrypt_params is None:
scrypt_params = ScryptParams(4096, 16, 1)
scrypt_params = SCRYPT_PARAMS
salt = ensure_bytes(salt)
password = ensure_bytes(password)
seed = scrypt(password, salt,
scrypt_params.N, scrypt_params.r, scrypt_params.p,
scrypt_params['N'], scrypt_params['r'], scrypt_params['p'],
SEED_LENGTH)
return cls(seed)
......
from duniterpy.key import VerifyingKey, SigningKey, ScryptParams
from duniterpy.key import VerifyingKey, SigningKey
from duniterpy.key.constants import SCRYPT_PARAMS
from duniterpy.documents.peer import Peer
from duniterpy.documents.ws2p.heads import *
import unittest
......@@ -6,7 +7,7 @@ import unittest
class TestVerifyingKey(unittest.TestCase):
def test_from_sign_to_verify(self):
sign_key = SigningKey.from_credentials("saltsalt", "passwordpassword", ScryptParams(4096, 16, 1))
sign_key = SigningKey.from_credentials("saltsalt", "passwordpassword", SCRYPT_PARAMS)
verify_key = VerifyingKey(sign_key.pubkey)
self.assertEqual(verify_key.vk, sign_key.vk)
......@@ -27,26 +28,29 @@ BASIC_MERKLED_API testnet.duniter.inso.ovh 80
def test_ws2p_headv0(self):
headv0, _ = HeadV0.from_inline("WS2P:HEAD:3dnbnYY9i2bHMQUGyFp5GVvJ2wBkVpus31cDJA5cfRpj:"
"54813-00000A24802B33B71A91B6E990038C145A4815A45C71E57B2F2EF393183C7E2C",
"a1vAAM666kPsMCFTbkgkcCsqHf8nmXR+Lh3D3u+BaXzmArj7kwlItbdGUs4fc9QUG5Lp4TwPS7nhOM5t1Kt6CA==")
"54813-00000A24802B33B71A91B6E990038C145A4815A45C71E57B2F2EF393183C7E2C",
"a1vAAM666kPsMCFTbkgkcCsqHf8nmXR+Lh3D3u"
"+BaXzmArj7kwlItbdGUs4fc9QUG5Lp4TwPS7nhOM5t1Kt6CA==")
verifying_key = VerifyingKey(headv0.pubkey)
self.assertTrue(verifying_key.verify_ws2p_head(headv0))
def test_ws2p_headv1(self):
headv1, _ = HeadV1.from_inline("WS2POCAIC:HEAD:1:HbTqJ1Ts3RhJ8Rx4XkNyh1oSKmoZL1kY5U7t9mKTSjAB:"
"102131-0000066028B991BDFE3FF6DBA84EF519F76B62EA3787BC29D9A05557675B1F16:1152e46e:"
"duniter:1.6.21:1",
"ZGpT8HG4uX5Hc96gqhzIkkELVjGQKDp2/L+7BTFG5ODxGYWd2VX/H+hdZRqf0iUWRNuhxlequ68kkwMaE6ymBw==")
"102131-0000066028B991BDFE3FF6DBA84EF519F76B62EA3787BC29D9A05557675B1F16"
":1152e46e:duniter:1.6.21:1",
"ZGpT8HG4uX5Hc96gqhzIkkELVjGQKDp2/L+7BTFG5ODxGYWd2VX/H"
"+hdZRqf0iUWRNuhxlequ68kkwMaE6ymBw==")
verifying_key = VerifyingKey(headv1.pubkey)
self.assertTrue(verifying_key.verify_ws2p_head(headv1))
def test_ws2p_headv2(self):
headv2, _ = HeadV2.from_inline("WS2POCA:HEAD:2:D3krfq6J9AmfpKnS3gQVYoy7NzGCc61vokteTS8LJ4YH:"
"99393-0000017256006BFA979565F1280488D5831DD66054069E46A3EDEB1AECDBBF13:cb36b021:"
"duniter:1.6.21:1:20:19",
"CgD1vaImPWZUCDFt5HDHUdjCTFcIwW5ndiCx6kXioFLZoz1a4WhCFYXvjI2N8+jEwQdWtf5+yNoHonqBSqirAQ==")
"99393-0000017256006BFA979565F1280488D5831DD66054069E46A3EDEB1AECDBBF13"
":cb36b021:duniter:1.6.21:1:20:19",
"CgD1vaImPWZUCDFt5HDHUdjCTFcIwW5ndiCx6kXioFLZoz1a4WhCFYXvjI2N8+jEwQdWtf5"
"+yNoHonqBSqirAQ==")
verifying_key = VerifyingKey(headv2.pubkey)
self.assertTrue(verifying_key.verify_ws2p_head(headv2))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment