Beispiel #1
0
class Transacoes:

    hashTransAnterior = '0'
    assinatura = None
    Hash = None
    gerador = HashMachine()

    def assinar(self, dados=None, chavePrivada=None):
        if dados == None or chavePrivada == None:
            raise processoDeAssinaturaInvalido

        assinatura = chavePrivada.sign(dados.encode())
        assinaturaStr = codecs.encode(assinatura, 'hex').decode()

        return assinaturaStr

    def verificarAssinatura(self, assinatura, chavePublica):
        return chavePublica.verify(assinatura, chavePublica)
Beispiel #2
0
from pymerkle.hashing import HashMachine, HASH_TYPES
from pymerkle.exceptions import EmptyPathException, UndecodableArgumentError
from tests.conftest import ENCODINGS

MESSAGE = 'oculusnonviditnecaurisaudivit'

machines = []
machines__hash_types__encodings__securities = []
machines__single_args = []

for security in (True, False):
    for hash_type in HASH_TYPES:
        for encoding in ENCODINGS:
            machine = HashMachine(hash_type=hash_type,
                                  encoding=encoding,
                                  raw_bytes=True,
                                  security=security)

            machines.append(machine)
            machines__hash_types__encodings__securities.extend([
                (machine, hash_type, encoding, security)
            ])

            machines__single_args.extend([(machine, MESSAGE),
                                          (machine, bytes(MESSAGE, encoding))])

# .hash()


@pytest.mark.parametrize("machine, hash_type, encoding, security",
                         machines__hash_types__encodings__securities)
Beispiel #3
0
from pymerkle.exceptions import UndecodableRecord

from tests.conftest import ENCODINGS

trees__hash_machines = []
for raw_bytes in (True, False):
    for security in (True, False):
        for hash_type in HASH_TYPES:
            for encoding in ENCODINGS:

                trees__hash_machines.append((MerkleTree(hash_type=hash_type,
                                                        encoding=encoding,
                                                        raw_bytes=raw_bytes,
                                                        security=security),
                                             HashMachine(hash_type=hash_type,
                                                         encoding=encoding,
                                                         raw_bytes=raw_bytes,
                                                         security=security)))

single_records = []
for (tree, hash_machine) in trees__hash_machines:

    single_records.extend([(tree, hash_machine, 'string record'),
                           (tree, hash_machine,
                            bytes('bytes record', tree.encoding))])


@pytest.mark.parametrize("tree, hash_machine, record", single_records)
def test_encryptRecord(tree, hash_machine, record):
    encrypted = tree.encryptRecord(record)
    assert tree.leaves[-1].digest == hash_machine.hash(record)
Beispiel #4
0
def test_double_undecodableArgumentError(byte, encoding, security):
    machine = HashMachine(encoding=encoding,
                          security=security,
                          raw_bytes=False)
    with pytest.raises(UndecodableArgumentError):
        machine.hash(byte, byte)
Beispiel #5
0
"""
Tests represenation and serialization of nodes
"""

import pytest

from pymerkle.core.nodes import Node, Leaf
from pymerkle.hashing import HashMachine

_ = HashMachine()
encoding = _.encoding
hash_func = _.hash

pair_of_leaves = (
    Leaf(hash_func=hash_func, encoding=encoding, record=b'some record...'),
    Leaf(hash_func=hash_func,
         encoding=encoding,
         digest=
         '5f4e54b52702884b03c21efc76b7433607fa3b35343b9fd322521c9c1ed633b4'))

# Full binary structure (child-parent relations): 4 leaves, 7 nodes in total
leaf_1 = Leaf(hash_func=hash_func,
              encoding=encoding,
              record=b'first record...')
leaf_2 = Leaf(hash_func=hash_func,
              encoding=encoding,
              record=b'second record...')
leaf_3 = Leaf(hash_func=hash_func,
              encoding=encoding,
              record=b'third record...')
leaf_4 = Leaf(hash_func=hash_func,
Beispiel #6
0
"""
Utilizes hash comparison in order to verify that the the .update() method of
the tree.MerkleTree class behaves as prescribed (preserves the tree's property
of being binary-balanced)
"""

import pytest

from pymerkle import MerkleTree
from pymerkle.hashing import HashMachine
from pymerkle.exceptions import EmptyTreeException

tree = MerkleTree()
machine = HashMachine()
update = tree.update
hash = machine.hash

t_1, t_2, t_3, t_4, t_5, t_6, t_7, t_8, t_9, t_10, t_11 = \
    'ingi', 'rum', 'imus', 'noc', 'te', 'et', 'con', 'su', 'mi', 'mur', 'igni'


def test_0_leaves():
    with pytest.raises(EmptyTreeException):
        tree.rootHash


def test_1_leaves():
    update(record=t_1)
    assert tree.rootHash == hash(t_1)

Beispiel #7
0
Tests construction and properies of nodes
"""

import pytest

from pymerkle.core.nodes import Node, Leaf
from pymerkle.hashing import HashMachine
from pymerkle.exceptions import (
    NoChildException,
    NoDescendantException,
    NoParentException,
    LeafConstructionError,
    UndecodableRecord,
)

_ = HashMachine()
encoding = _.encoding
hash_func = _.hash

pair_of_leaves = (
    Leaf(hash_func=hash_func, encoding=encoding, record=b'some record...'),
    Leaf(hash_func=hash_func,
         encoding=encoding,
         digest=
         '5f4e54b52702884b03c21efc76b7433607fa3b35343b9fd322521c9c1ed633b4'))

# Full binary structure (child-parent relations): 4 leaves, 7 nodes in total
leaf_1 = Leaf(hash_func=hash_func,
              encoding=encoding,
              record=b'first record...')
leaf_2 = Leaf(hash_func=hash_func,
Beispiel #8
0
def main():
    global HASH_TYPE
    global ENCODING
    global HASH
    global LENGTH
    global ADDITIONAL
    global ITERATIONS
    global ROUNDS
    global RECORD

    prog = sys.argv[0]
    usage = 'python %s [--hashtype] [--encoding] [--length] [--additional] [--iterations] [--rounds]' % prog

    parser = argparse.ArgumentParser(prog=prog,
                                     usage=usage,
                                     description=__doc__,
                                     epilog='\n')
    parser.add_argument('--hashtype',
                        type=str,
                        help='Hash algorithm to be used by the Merkle-tree',
                        default='sha256')
    parser.add_argument('--encoding',
                        type=str,
                        help='Encoding to be used by the Merkle-tree',
                        default='utf_8')
    parser.add_argument('--length',
                        type=int,
                        help='Initial number of leaves',
                        default=7000)
    parser.add_argument('--additional',
                        type=int,
                        help='Additional number of leaves to append (twice)',
                        default=2000)
    parser.add_argument(
        '--iterations',
        type=int,
        help='Number of iterations when accessing node\'s attributes',
        default=1000)
    parser.add_argument(
        '--rounds',
        type=int,
        help='Number of rounds when accessing node\'s attributes',
        default=20)

    parsed_args = parser.parse_args()

    HASH_TYPE = parsed_args.hashtype
    ENCODING = parsed_args.encoding
    HASH = HashMachine(hash_type=HASH_TYPE, encoding=ENCODING).hash
    LENGTH = parsed_args.length
    ADDITIONAL = parsed_args.additional
    ITERATIONS = parsed_args.iterations
    ROUNDS = parsed_args.rounds

    write(
        '============================= pymerkle benchmarks ============================'
    )
    write('\nConfiguration')
    write('\n')
    write('\nHash type : %s' % HASH_TYPE)
    write('\nEncoding  : %s' % ENCODING)

    attribute_access_benchmark()
    tree_generation_benchmark()
    sizes_benchmark()
    encryption_benchmark()
    audit_proofs_benchmark()
    # consistency_proofs_benchmark()
    proof_validations_benchmark()

    write('\n')
    sys.exit(0)