Skip to content

Commit 2c00522

Browse files
authored
Use ticks to hash dates in election descriptions (#192)
1 parent fc2eba2 commit 2c00522

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/electionguard/election.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .hash import CryptoHashable, hash_elems
1010
from .logs import log_warning
1111
from .serializable import Serializable
12-
from .utils import get_optional
12+
from .utils import get_optional, to_ticks
1313

1414

1515
@unique
@@ -567,8 +567,8 @@ def crypto_hash(self) -> ElementModQ:
567567
return hash_elems(
568568
self.election_scope_id,
569569
str(self.type),
570-
self.start_date.isoformat(),
571-
self.end_date.isoformat(),
570+
to_ticks(self.start_date),
571+
to_ticks(self.end_date),
572572
self.name,
573573
self.contact_information,
574574
self.geopolitical_units,

tests/test_election.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
from datetime import datetime
23

34
from electionguard.election import (
45
ContestDescriptionWithPlaceholders,
@@ -9,6 +10,7 @@
910
)
1011
import electionguardtest.election_factory as ElectionFactory
1112
import electionguardtest.ballot_factory as BallotFactory
13+
from electionguard.serializable import read_json
1214

1315
election_factory = ElectionFactory.ElectionFactory()
1416
ballot_factory = BallotFactory.BallotFactory()
@@ -46,6 +48,25 @@ def test_election_has_deterministic_hash(self):
4648
# Assert
4749
self.assertEqual(subject1.crypto_hash(), subject2.crypto_hash())
4850

51+
def test_election_hash_is_consistent_regardless_of_format(self):
52+
53+
# Act
54+
subject1 = election_factory.get_simple_election_from_file()
55+
subject1.start_date = read_json('"2020-03-01T08:00:00-05:00"', datetime)
56+
57+
subject2 = election_factory.get_simple_election_from_file()
58+
subject2.start_date = read_json('"2020-03-01T13:00:00-00:00"', datetime)
59+
60+
subject3 = election_factory.get_simple_election_from_file()
61+
subject3.start_date = read_json('"2020-03-01T13:00:00.000-00:00"', datetime)
62+
63+
subjects = [subject1, subject2, subject3]
64+
65+
# Assert
66+
hashes = [subject.crypto_hash() for subject in subjects]
67+
for other_hash in hashes[1:]:
68+
self.assertEqual(hashes[0], other_hash)
69+
4970
def test_election_from_file_generates_consistent_internal_description_contest_hashes(
5071
self,
5172
):

0 commit comments

Comments
 (0)