Ejemplo n.º 1
0
    def create(name, description, ends, max_voters, choices):
        base = max_voters + int(ceil(max_voters / 10))
        pal = Paillier(int(floor(log(base * (base**(len(choices))), 2)) + 4))
        pal.generate_keys()
        public_key = str(pal.public_key['n']) + "#" + str(pal.public_key['g'])
        private_key = str(pal.private_key['n']) + "#" + str(
            pal.private_key['g']) + "#" + str(pal.private_key['lam'])
        state = Paillier.encrypt(0, pal.public_key)
        now = datetime.now()
        created = date(now.year, now.month, now.day)

        curs.execute("""INSERT INTO poll 
                        (name, description, created, ends, base, choices_num, state, public_key, private_key) 
                        VALUES ('%s', '%s', '%s', '%s', %s, %s, '%s', '%s', '%s')"""
                     % (name, description, created, ends, base, len(choices),
                        state, public_key, private_key))
        poll_id = curs.lastrowid

        i = 1
        for choice in choices:
            curs.execute("""INSERT INTO choice 
                            (poll_id, choice_id, choice_value) 
                            VALUES ('%s', '%s', '%s')""" %
                         (poll_id, i, choice))
            i += 1

        return Poll(poll_id, name, description, created, ends, base, state,
                    public_key, choices, private_key)
Ejemplo n.º 2
0
 def create(name, description, ends, max_voters, choices):
     base = max_voters + int(ceil(max_voters/10))
     pal = Paillier(int(floor(log(base*(base**(len(choices))),2))+4))
     pal.generate_keys()
     public_key = str(pal.public_key['n']) + "#" + str(pal.public_key['g'])
     private_key = str(pal.private_key['n']) + "#" + str(pal.private_key['g'])+ "#" + str(pal.private_key['lam'])
     state = Paillier.encrypt(0, pal.public_key)
     now = datetime.now()
     created = date(now.year, now.month, now.day)
     
     curs.execute("""INSERT INTO poll 
                     (name, description, created, ends, base, choices_num, state, public_key, private_key) 
                     VALUES ('%s', '%s', '%s', '%s', %s, %s, '%s', '%s', '%s')"""
                     % (name, description, created, ends, base, len(choices), state, public_key, private_key))
     poll_id = curs.lastrowid
     
     i = 1
     for choice in choices:
         curs.execute("""INSERT INTO choice 
                         (poll_id, choice_id, choice_value) 
                         VALUES ('%s', '%s', '%s')"""
                         % (poll_id, i, choice))
         i += 1
         
     return Poll(poll_id, name, description, created, ends, base, state, public_key, choices, private_key)
Ejemplo n.º 3
0
 def get_result(self, private_key):
     pk = private_key.split("#")
     enc_result = Paillier.decrypt(self.state, {
         'n': int(pk[0]),
         'g': int(pk[1]),
         'lam': int(pk[2])
     })
     return self.update_choices(self.id, self.choices, self.base,
                                int(enc_result))
Ejemplo n.º 4
0
 def get_result(self, private_key):
     pk = private_key.split("#")
     enc_result = Paillier.decrypt(self.state, {'n':int(pk[0]), 'g':int(pk[1]), 'lam':int(pk[2])})
     return self.update_choices(self.id, self.choices, self.base, int(enc_result))