2019-01-31 16:42:22 +01:00
|
|
|
"""
|
|
|
|
Test the alternatives for crypto primitives
|
|
|
|
"""
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import unittest
|
|
|
|
from abc import ABCMeta, abstractmethod
|
2021-07-21 17:55:52 +02:00
|
|
|
from binascii import hexlify
|
2021-01-29 16:02:55 +01:00
|
|
|
|
2019-11-20 16:35:51 +01:00
|
|
|
from pybitmessage.pyelliptic import arithmetic
|
2019-01-31 16:42:22 +01:00
|
|
|
|
2021-01-29 16:02:55 +01:00
|
|
|
|
2019-01-31 16:42:22 +01:00
|
|
|
try:
|
|
|
|
from Crypto.Hash import RIPEMD
|
|
|
|
except ImportError:
|
|
|
|
RIPEMD = None
|
|
|
|
|
2021-07-21 17:55:52 +02:00
|
|
|
from .samples import (
|
|
|
|
sample_factor, sample_point, sample_pubsigningkey, sample_pubencryptionkey,
|
|
|
|
sample_privsigningkey, sample_privencryptionkey, sample_ripe
|
2021-02-02 19:17:11 +01:00
|
|
|
)
|
|
|
|
|
2021-07-21 17:55:52 +02:00
|
|
|
|
2019-01-31 16:42:22 +01:00
|
|
|
_sha = hashlib.new('sha512')
|
|
|
|
_sha.update(sample_pubsigningkey + sample_pubencryptionkey)
|
|
|
|
|
|
|
|
pubkey_sha = _sha.digest()
|
|
|
|
|
|
|
|
|
|
|
|
class RIPEMD160TestCase(object):
|
|
|
|
"""Base class for RIPEMD160 test case"""
|
2019-02-16 12:57:30 +01:00
|
|
|
# pylint: disable=too-few-public-methods,no-member
|
2019-01-31 16:42:22 +01:00
|
|
|
__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()
|
2019-11-20 16:35:51 +01:00
|
|
|
|
|
|
|
|
2021-07-20 19:06:01 +02:00
|
|
|
class TestHighlevelcrypto(unittest.TestCase):
|
|
|
|
"""Test highlevelcrypto public functions"""
|
|
|
|
|
2021-02-02 19:17:11 +01:00
|
|
|
def test_base10_multiply(self):
|
|
|
|
"""Test arithmetic.base10_multiply"""
|
|
|
|
self.assertEqual(
|
|
|
|
sample_point,
|
|
|
|
arithmetic.base10_multiply(arithmetic.G, sample_factor))
|
|
|
|
|
2019-11-20 16:35:51 +01:00
|
|
|
def test_privtopub(self):
|
|
|
|
"""Generate public keys and check the result"""
|
|
|
|
self.assertEqual(
|
2021-02-02 19:17:11 +01:00
|
|
|
arithmetic.privtopub(sample_privsigningkey).encode(),
|
2019-11-20 16:35:51 +01:00
|
|
|
hexlify(sample_pubsigningkey)
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2021-02-02 19:17:11 +01:00
|
|
|
arithmetic.privtopub(sample_privencryptionkey).encode(),
|
2019-11-20 16:35:51 +01:00
|
|
|
hexlify(sample_pubencryptionkey)
|
|
|
|
)
|