From 509b542def2fd7cdefe8ff8ab28c8a004009fd3a Mon Sep 17 00:00:00 2001 From: Marco Ciotola <848222@stud.unive.it> Date: Tue, 3 Mar 2020 14:13:18 +0100 Subject: [PATCH] [py3] str-bytes comparisons --- helios/crypto/electionalgs.py | 22 +++++++++++++++++----- helios/models.py | 6 +++--- helios/tests.py | 6 +++++- helios/workflows/homomorphic.py | 30 +++++++++++++++++++++--------- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/helios/crypto/electionalgs.py b/helios/crypto/electionalgs.py index 1f7593b..43e3357 100644 --- a/helios/crypto/electionalgs.py +++ b/helios/crypto/electionalgs.py @@ -6,6 +6,7 @@ Ben Adida """ import datetime import uuid +import logging from helios.utils import to_json from . import algs @@ -322,17 +323,28 @@ class EncryptedVote(HeliosObject): FIELDS = ['encrypted_answers', 'election_hash', 'election_uuid'] def verify(self, election): - # right number of answers - if len(self.encrypted_answers) != len(election.questions): + # correct number of answers + # noinspection PyUnresolvedReferences + n_answers = len(self.encrypted_answers) if self.encrypted_answers is not None else 0 + n_questions = len(election.questions) if election.questions is not None else 0 + if n_answers != n_questions: + logging.error(f"Incorrect number of answers ({n_answers}) vs questions ({n_questions})") return False # check hash - if self.election_hash != election.hash: - # print "%s / %s " % (self.election_hash, election.hash) + # noinspection PyUnresolvedReferences + our_election_hash = self.election_hash if isinstance(self.election_hash, str) else self.election_hash.decode() + actual_election_hash = election.hash if isinstance(election.hash, str) else election.hash.decode() + if our_election_hash != actual_election_hash: + logging.error(f"Incorrect election_hash {our_election_hash} vs {actual_election_hash} ") return False # check ID - if self.election_uuid != election.uuid: + # noinspection PyUnresolvedReferences + our_election_uuid = self.election_uuid if isinstance(self.election_uuid, str) else self.election_uuid.decode() + actual_election_uuid = election.uuid if isinstance(election.uuid, str) else election.uuid.decode() + if our_election_uuid != actual_election_uuid: + logging.error(f"Incorrect election_uuid {our_election_uuid} vs {actual_election_uuid} ") return False # check proofs on all of answers diff --git a/helios/models.py b/helios/models.py index 6babb10..772559c 100644 --- a/helios/models.py +++ b/helios/models.py @@ -1044,9 +1044,9 @@ class CastVote(HeliosModel): """ find a tiny version of the hash for a URL slug. """ - safe_hash = self.vote_hash - for c in ['/', '+']: - safe_hash = safe_hash.replace(c,'') + safe_hash = self.vote_hash.decode() if isinstance(self.vote_hash, bytes) else self.vote_hash + for c in ['/', '+', '#']: + safe_hash = safe_hash.replace(c, '') length = 8 while True: diff --git a/helios/tests.py b/helios/tests.py index 4b1423e..5a5f601 100644 --- a/helios/tests.py +++ b/helios/tests.py @@ -378,7 +378,11 @@ class WebTest(django_webtest.WebTest): else: t = response.content - assert text in str(t), "missing text %s" % text + if isinstance(text, bytes): + text = text.decode() + if isinstance(t, bytes): + t = t.decode() + assert text in t, "missing text %s" % text ## diff --git a/helios/workflows/homomorphic.py b/helios/workflows/homomorphic.py index 2f03f50..9c80396 100644 --- a/helios/workflows/homomorphic.py +++ b/helios/workflows/homomorphic.py @@ -6,6 +6,7 @@ Ben Adida reworked 2011-01-09 """ +import logging from helios.crypto import algs from . import WorkflowObject @@ -160,7 +161,7 @@ class EncryptedVote(WorkflowObject): An encrypted ballot """ def __init__(self): - self.encrypted_answers = None + self.encrypted_answers = [] @property def datatype(self): @@ -176,19 +177,30 @@ class EncryptedVote(WorkflowObject): answers = property(_answers_get, _answers_set) def verify(self, election): - # right number of answers - if len(self.encrypted_answers) != len(election.questions): + # correct number of answers + # noinspection PyUnresolvedReferences + n_answers = len(self.encrypted_answers) if self.encrypted_answers is not None else 0 + n_questions = len(election.questions) if election.questions is not None else 0 + if n_answers != n_questions: + logging.error(f"Incorrect number of answers ({n_answers}) vs questions ({n_questions})") return False - + # check hash - if self.election_hash != election.hash: - # print "%s / %s " % (self.election_hash, election.hash) + # noinspection PyUnresolvedReferences + our_election_hash = self.election_hash if isinstance(self.election_hash, str) else self.election_hash.decode() + actual_election_hash = election.hash if isinstance(election.hash, str) else election.hash.decode() + if our_election_hash != actual_election_hash: + logging.error(f"Incorrect election_hash {our_election_hash} vs {actual_election_hash} ") return False - + # check ID - if self.election_uuid != election.uuid: + # noinspection PyUnresolvedReferences + our_election_uuid = self.election_uuid if isinstance(self.election_uuid, str) else self.election_uuid.decode() + actual_election_uuid = election.uuid if isinstance(election.uuid, str) else election.uuid.decode() + if our_election_uuid != actual_election_uuid: + logging.error(f"Incorrect election_uuid {our_election_uuid} vs {actual_election_uuid} ") return False - + # check proofs on all of answers for question_num in range(len(election.questions)): ea = self.encrypted_answers[question_num] -- GitLab