Refactor using of crypto functions #1796

Closed
g1itch wants to merge 14 commits from crypto into v0.6
3 changed files with 56 additions and 16 deletions
Showing only changes of commit 4b3c387634 - Show all commits

View File

@ -36,9 +36,23 @@ sample_deterministic_addr4 = 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK'
sample_daddr3_512 = 18875720106589866286514488037355423395410802084648916523381 sample_daddr3_512 = 18875720106589866286514488037355423395410802084648916523381
sample_daddr4_512 = 25152821841976547050350277460563089811513157529113201589004 sample_daddr4_512 = 25152821841976547050350277460563089811513157529113201589004
sample_statusbar_msg = "new status bar message" sample_statusbar_msg = 'new status bar message'
sample_inbox_msg_ids = ['27e644765a3e4b2e973ee7ccf958ea20', '51fc5531-3989-4d69-bbb5-68d64b756f5b', sample_inbox_msg_ids = [
'27e644765a3e4b2e973ee7ccf958ea20', '51fc5531-3989-4d69-bbb5-68d64b756f5b',
'2c975c515f8b414db5eea60ba57ba455', 'bc1f2d8a-681c-4cc0-9a12-6067c7e1ac24'] '2c975c515f8b414db5eea60ba57ba455', 'bc1f2d8a-681c-4cc0-9a12-6067c7e1ac24']
# second address in sample_test_subscription_address is for the announcement broadcast # second address in sample_subscription_addresses
sample_test_subscription_address = ['BM-2cWQLCBGorT9pUGkYSuGGVr9LzE4mRnQaq', 'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw'] # is for the announcement broadcast
sample_subscription_addresses = [
'BM-2cWQLCBGorT9pUGkYSuGGVr9LzE4mRnQaq',
'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw']
sample_subscription_name = 'test sub' sample_subscription_name = 'test sub'
sample_msg = unhexlify(
'0592a10584ffabf96539f3d780d776828c67da1ab5b169e9e8aed838aaecc9ed36d49ff'
'1423c55f019e050c66c6324f53588be88894fef4dcffdb74b98e2b200')
sample_sig = unhexlify(
'304402202302475351db6b822de15d922e29397541f10d8a19780ba2ca4a920b1035f075'
'02205e5bba40d5f07a24c23a89ba5f01a3828371dfbb685dd5375fa1c29095fd232b')
sample_sig_sha1 = unhexlify(
'304502203b50123af78b4e40f5f819ae5b8786f48826e56d0f3e65744708a493f5b65de1'
'0221009ddce2981ea143c0ac70404a535327e774adce8eebbae2d35104f1d326255f9a')

View File

@ -12,8 +12,10 @@ from six.moves import xmlrpc_client # nosec
import psutil import psutil
from .samples import ( from .samples import (
sample_seed, sample_deterministic_addr3, sample_deterministic_addr4, sample_statusbar_msg, sample_seed, sample_deterministic_addr3, sample_deterministic_addr4,
sample_inbox_msg_ids, sample_test_subscription_address, sample_subscription_name) sample_statusbar_msg, sample_inbox_msg_ids, sample_subscription_addresses,
sample_subscription_name
)
from .test_process import TestProcessProto from .test_process import TestProcessProto
@ -263,9 +265,10 @@ class TestAPI(TestAPIProto):
def test_subscriptions(self): def test_subscriptions(self):
"""Testing the API commands related to subscriptions""" """Testing the API commands related to subscriptions"""
self.assertEqual( self.assertEqual(
self.api.addSubscription(sample_test_subscription_address[0], sample_subscription_name.encode('base64')), self.api.addSubscription(
sample_subscription_addresses[0],
sample_subscription_name.encode('base64')),
'Added subscription.' 'Added subscription.'
) )
@ -273,18 +276,19 @@ class TestAPI(TestAPIProto):
# check_address # check_address
for sub in json.loads(self.api.listSubscriptions())['subscriptions']: for sub in json.loads(self.api.listSubscriptions())['subscriptions']:
# special address, added when sqlThread starts # special address, added when sqlThread starts
if sub['address'] == sample_test_subscription_address[0]: if sub['address'] == sample_subscription_addresses[0]:
added_subscription = sub added_subscription = sub
break break
self.assertEqual( self.assertEqual(
base64.decodestring(added_subscription['label']) if added_subscription['label'] else None, base64.decodestring(added_subscription['label'])
if added_subscription['label'] else None,
sample_subscription_name) sample_subscription_name)
self.assertTrue(added_subscription['enabled']) self.assertTrue(added_subscription['enabled'])
for s in json.loads(self.api.listSubscriptions())['subscriptions']: for s in json.loads(self.api.listSubscriptions())['subscriptions']:
# special address, added when sqlThread starts # special address, added when sqlThread starts
if s['address'] == sample_test_subscription_address[1]: if s['address'] == sample_subscription_addresses[1]:
self.assertEqual( self.assertEqual(
base64.decodestring(s['label']), base64.decodestring(s['label']),
'Bitmessage new releases/announcements') 'Bitmessage new releases/announcements')
@ -295,10 +299,10 @@ class TestAPI(TestAPIProto):
'Could not find Bitmessage new releases/announcements' 'Could not find Bitmessage new releases/announcements'
' in subscriptions') ' in subscriptions')
self.assertEqual( self.assertEqual(
self.api.deleteSubscription(sample_test_subscription_address[0]), self.api.deleteSubscription(sample_subscription_addresses[0]),
'Deleted subscription if it existed.') 'Deleted subscription if it existed.')
self.assertEqual( self.assertEqual(
self.api.deleteSubscription(sample_test_subscription_address[1]), self.api.deleteSubscription(sample_subscription_addresses[1]),
'Deleted subscription if it existed.') 'Deleted subscription if it existed.')
self.assertEqual( self.assertEqual(
json.loads(self.api.listSubscriptions())['subscriptions'], []) json.loads(self.api.listSubscriptions())['subscriptions'], [])

View File

@ -16,8 +16,9 @@ except ImportError:
RIPEMD = None RIPEMD = None
from .samples import ( from .samples import (
sample_pubsigningkey, sample_pubencryptionkey, sample_msg, sample_pubsigningkey, sample_pubencryptionkey,
sample_privsigningkey, sample_privencryptionkey, sample_ripe sample_privsigningkey, sample_privencryptionkey, sample_ripe,
sample_sig, sample_sig_sha1
) )
@ -62,6 +63,27 @@ class TestCrypto(RIPEMD160TestCase, unittest.TestCase):
class TestHighlevelcrypto(unittest.TestCase): class TestHighlevelcrypto(unittest.TestCase):
"""Test highlevelcrypto public functions""" """Test highlevelcrypto public functions"""
# def test_sign(self):
# """Check the signature of the sample_msg created with sample key"""
# self.assertEqual(
# highlevelcrypto.sign(sample_msg, sample_privsigningkey), sample_sig)
def test_verify(self):
"""Verify sample signatures and newly generated ones"""
pubkey_hex = hexlify(sample_pubsigningkey)
# pregenerated signatures
self.assertTrue(
highlevelcrypto.verify(sample_msg, sample_sig, pubkey_hex))
self.assertTrue(
highlevelcrypto.verify(sample_msg, sample_sig_sha1, pubkey_hex))
# new signatures
sig256 = highlevelcrypto.sign(sample_msg, sample_privsigningkey)
sig1 = highlevelcrypto.sign(sample_msg, sample_privsigningkey, "sha1")
self.assertTrue(
highlevelcrypto.verify(sample_msg, sig256, pubkey_hex))
self.assertTrue(
highlevelcrypto.verify(sample_msg, sig1, pubkey_hex))
def test_privtopub(self): def test_privtopub(self):
"""Generate public keys and check the result""" """Generate public keys and check the result"""
PeterSurda commented 2021-11-15 08:24:01 +01:00 (Migrated from github.com)
Review

ideally b'hello' should also be a variable in samples

ideally `b'hello'` should also be a variable in samples
g1itch commented 2021-11-15 13:10:44 +01:00 (Migrated from github.com)
Review
It's from here: https://pybitmessage-test.readthedocs.io/en/doc/protocol.html#hashes
self.assertEqual( self.assertEqual(