81 lines
2.1 KiB
Python
81 lines
2.1 KiB
Python
"""
|
|
Test the alternatives for crypto primitives
|
|
"""
|
|
|
|
import hashlib
|
|
import unittest
|
|
from abc import ABCMeta, abstractmethod
|
|
from binascii import hexlify
|
|
|
|
from pybitmessage.pyelliptic import arithmetic
|
|
|
|
|
|
try:
|
|
from Crypto.Hash import RIPEMD
|
|
except ImportError:
|
|
RIPEMD = None
|
|
|
|
from .samples import (
|
|
sample_factor, sample_point, sample_pubsigningkey, sample_pubencryptionkey,
|
|
sample_privsigningkey, sample_privencryptionkey, sample_ripe
|
|
)
|
|
|
|
|
|
_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)
|
|
)
|