-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecretSanta.py
169 lines (142 loc) · 4.75 KB
/
SecretSanta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created in 2021
@author: Stanislas MEZUREUX
Copyright (c) 2021 Stanislas MEZUREUX. All rights reserved.
"""
import smtplib
from random import shuffle
import copy
import secrets
import time
import elgamal as eg
from math import factorial
AMOUNT = 10
NAME = 'MPSI1 227/228'
DATE = '03/01/2022'
GMAIL_ADDRESS = '[email protected]'
GMAIL_PASSWORD = '**secret(santa:)**'
class TooMuchInTheTeam(Exception):
pass
def nb_participants_check(L):
for i, team in enumerate(L):
M = [e for A in L[:i]+L[i+1:] for e in A]
if M != [] and len(M) < len(L[i]):
raise TooMuchInTheTeam(f"Too much participants in {team[0][3]}")
def csv_to_list(data):
with open(data) as f:
L = f.read().splitlines()
L = L[1:]
for i, e in enumerate(L):
L[i] = [i] + e.split(',')
return L
def group_by_team(L):
teams = []
for e in L:
if e[3] not in teams:
teams.append(e[3])
nb_teams = len(teams)
M = [[] for _ in range(nb_teams)]
for i, team in enumerate(teams):
for e in L:
if e[3] == team:
M[i].append(e[0])
return M
def make_pairs(L, pk):
nb_teams = len(L)
if nb_teams == 1:
M = L[0].copy()
shuffle(M)
length = len(M)
R = [(0, 0)]*length
for i in range(length):
R[i] = (eg.encrypt(M[i]+1, pk),
eg.encrypt(M[(i+1) % length]+1, pk))
with open('/draw_files/secret_santa_draw.py', 'w') as f:
f.write(f'draw = {R}')
return R
R = []
M = copy.deepcopy(L)
shuffle(M)
L_new = copy.deepcopy(M)
for i, team in enumerate(L_new):
for j, e in enumerate(L_new[i]):
if len(M) == 1:
M_next = M[0]
else:
M_next = (M[:i]+M[i+1:])[(j+1) % (len(M)-1)]
M_next_len = len(M_next)
k = secrets.randbelow(M_next_len)
gift_to = M_next[k]
R.append((eg.encrypt(e+1, pk),
eg.encrypt(gift_to+1, pk)))
with open('draw_files/secret_santa_draw.py', 'w') as f:
f.write(f'draw = {R}\ndraw_len = {len(R)}')
del M_next[k]
if M_next_len == 1:
M = [e for e in M if e != []]
return R
def send_email(L, data, sk, pk, display_team=True):
from_addr = GMAIL_ADDRESS
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.set_debuglevel(1)
server.ehlo
server.login(GMAIL_ADDRESS, GMAIL_PASSWORD)
for e_encrypted in L:
e = (eg.decrypt(e_encrypted[0], sk, pk)-1,
eg.decrypt(e_encrypted[1], sk, pk)-1)
to_addrs = data[e[0]][4]
subject = f"Secret Santa - {NAME}"
text = (
f'Bonjour {data[e[0]][1]},\nCette année, tu es en charge du '
f'cadeau de {data[e[1]][1]} {data[e[1]][2]}'
f'{" ("+data[e[1]][3]+")" if display_team else ""}. Je te rappelle '
f'que le budget est de {AMOUNT}€ et que la célébration aura lieu '
f'le {DATE}.\nJoyeux Nöel à toi !'
)
message = f"Subject: {subject}\nFrom: {from_addr}\nTo: {to_addrs}\n\n"
message = message + text
server.sendmail(from_addr, to_addrs, message.encode("utf8"))
time.sleep(0.1)
server.quit()
def zero_knowledge_proof(sk, pk):
import draw_files.secret_santa_draw as ssd
if ssd.draw_len == 1:
return True
gift_from = ssd.draw[0][0]
gift_to = ssd.draw[0][1]
for i in range(1, ssd.draw_len):
gift_from = eg.multiply(gift_from, ssd.draw[i][0])
gift_to = eg.multiply(gift_to, ssd.draw[i][1])
fact = factorial(ssd.draw_len)
for j in range(1, ssd.draw_len):
for i in range(1, j):
c_i = ssd.draw[i]
c_j = ssd.draw[j]
if (eg.is_equal(c_i[0], c_j[0], sk, pk) or
eg.is_equal(c_i[1], c_j[1], sk, pk) or
eg.is_equal(c_j[0], c_j[1], sk, pk)):
return False
return eg.decrypt(gift_from, sk, pk) == fact and eg.decrypt(gift_to, sk, pk) == fact
def Secret_Santa(data):
try:
sk, pk = eg.keygen()
info = csv_to_list(data)
L = group_by_team(info)
nb_teams = len(L)
nb_participants_check(L)
R = make_pairs(L, pk)
print(f'secret key : {sk}')
print(f'public key : {pk}')
send_email(R, info, sk, pk, nb_teams != 1)
except TooMuchInTheTeam as TeamError:
print(TeamError)
def resend(sk, pk, data):
try:
import draw_files.secret_santa_draw as ssd
info = csv_to_list(data)
nb_teams = len(ssd.draw)
send_email(ssd.draw, info, sk, pk, nb_teams != 1)
except ModuleNotFoundError as Error:
print(Error)