This repository has been archived on 2024-12-22. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-22/src/tests/test_crypto.py

96 lines
2.9 KiB
Python
Raw Normal View History

"""
Test the alternatives for crypto primitives
"""
import hashlib
import unittest
from abc import ABCMeta, abstractmethod
from binascii import hexlify, unhexlify
from pybitmessage.pyelliptic import arithmetic
try:
from Crypto.Hash import RIPEMD
except ImportError:
RIPEMD = None
# These keys are from addresses test script
sample_pubsigningkey = unhexlify(
'044a367f049ec16cb6b6118eb734a9962d10b8db59c890cd08f210c43ff08bdf09d'
'16f502ca26cd0713f38988a1237f1fc8fa07b15653c996dc4013af6d15505ce')
sample_pubencryptionkey = unhexlify(
'044597d59177fc1d89555d38915f581b5ff2286b39d022ca0283d2bdd5c36be5d3c'
'e7b9b97792327851a562752e4b79475d1f51f5a71352482b241227f45ed36a9')
sample_privsigningkey = \
'93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665'
sample_privencryptionkey = \
'4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf40f4d7400a3a'
sample_ripe = b'003cd097eb7f35c87b5dc8b4538c22cb55312a9f'
sample_factor = 66858749573256452658262553961707680376751171096153613379801854825275240965733
# G * sample_factor
sample_point = (
33567437183004486938355437500683826356288335339807546987348409590129959362313,
94730058721143827257669456336351159718085716196507891067256111928318063085006
)
_sha = hashlib.new('sha512')
_sha.update(sample_pubsigningkey + sample_pubencryptionkey)
pubkey_sha = _sha.digest()
class RIPEMD160TestCase(object):
"""Base class for RIPEMD160 test case"""
# pylint: disable=too-few-public-methods,no-member
__metaclass__ = ABCMeta
@abstractmethod
def _hashdigest(self, data):
"""RIPEMD160 digest implementation"""
pass
def test_hash_string(self):
"""Check RIPEMD160 hash function on string"""
self.assertEqual(hexlify(self._hashdigest(pubkey_sha)), sample_ripe)
class TestHashlib(RIPEMD160TestCase, unittest.TestCase):
"""RIPEMD160 test case for hashlib"""
@staticmethod
def _hashdigest(data):
hasher = hashlib.new('ripemd160')
hasher.update(data)
return hasher.digest()
@unittest.skipUnless(RIPEMD, 'pycrypto package not found')
class TestCrypto(RIPEMD160TestCase, unittest.TestCase):
"""RIPEMD160 test case for Crypto"""
@staticmethod
def _hashdigest(data):
return RIPEMD.RIPEMD160Hash(data).digest()
class TestHighlevelcrypto(unittest.TestCase):
"""Test highlevelcrypto public functions"""
def test_base10_multiply(self):
"""Test arithmetic.base10_multiply"""
self.assertEqual(
sample_point,
arithmetic.base10_multiply(arithmetic.G, sample_factor))
def test_privtopub(self):
"""Generate public keys and check the result"""
self.assertEqual(
arithmetic.privtopub(sample_privsigningkey).encode(),
hexlify(sample_pubsigningkey)
)
self.assertEqual(
arithmetic.privtopub(sample_privencryptionkey).encode(),
hexlify(sample_pubencryptionkey)
)