2019-01-31 16:42:22 +01:00
|
|
|
"""
|
|
|
|
Test the alternatives for crypto primitives
|
|
|
|
"""
|
|
|
|
|
|
|
|
import hashlib
|
2022-05-11 01:01:57 +02:00
|
|
|
import ssl
|
2019-01-31 16:42:22 +01:00
|
|
|
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
|
|
|
|
2021-07-20 19:06:01 +02:00
|
|
|
from pybitmessage import highlevelcrypto
|
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:
|
2022-05-11 01:01:57 +02:00
|
|
|
from Crypto.Hash import RIPEMD160
|
2019-01-31 16:42:22 +01:00
|
|
|
except ImportError:
|
2022-05-11 01:01:57 +02:00
|
|
|
RIPEMD160 = None
|
2019-01-31 16:42:22 +01:00
|
|
|
|
2021-07-21 17:55:52 +02:00
|
|
|
from .samples import (
|
2021-07-20 19:06:01 +02:00
|
|
|
sample_pubsigningkey, sample_pubencryptionkey,
|
2021-07-21 17:55:52 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2022-05-11 01:01:57 +02:00
|
|
|
@unittest.skipIf(
|
|
|
|
ssl.OPENSSL_VERSION.startswith('OpenSSL 3'), 'no ripemd160 in openssl 3')
|
2019-01-31 16:42:22 +01:00
|
|
|
class TestHashlib(RIPEMD160TestCase, unittest.TestCase):
|
|
|
|
"""RIPEMD160 test case for hashlib"""
|
|
|
|
@staticmethod
|
|
|
|
def _hashdigest(data):
|
|
|
|
hasher = hashlib.new('ripemd160')
|
|
|
|
hasher.update(data)
|
|
|
|
return hasher.digest()
|
|
|
|
|
|
|
|
|
2022-05-11 01:01:57 +02:00
|
|
|
@unittest.skipUnless(RIPEMD160, 'pycrypto package not found')
|
2019-01-31 16:42:22 +01:00
|
|
|
class TestCrypto(RIPEMD160TestCase, unittest.TestCase):
|
|
|
|
"""RIPEMD160 test case for Crypto"""
|
|
|
|
@staticmethod
|
|
|
|
def _hashdigest(data):
|
2022-05-11 01:01:57 +02:00
|
|
|
return RIPEMD160.new(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"""
|
|
|
|
|
2019-11-20 16:35:51 +01:00
|
|
|
def test_privtopub(self):
|
|
|
|
"""Generate public keys and check the result"""
|
|
|
|
self.assertEqual(
|
2021-07-20 19:06:01 +02:00
|
|
|
highlevelcrypto.privToPub(sample_privsigningkey),
|
2019-11-20 16:35:51 +01:00
|
|
|
hexlify(sample_pubsigningkey)
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2021-07-20 19:06:01 +02:00
|
|
|
highlevelcrypto.privToPub(sample_privencryptionkey),
|
2019-11-20 16:35:51 +01:00
|
|
|
hexlify(sample_pubencryptionkey)
|
|
|
|
)
|