-
Notifications
You must be signed in to change notification settings - Fork 4
/
controller.py
156 lines (125 loc) · 3.69 KB
/
controller.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
from flask import Flask,render_template
import connect_to_database
import print_from_table
import hashlib
import model
import sys
def list():
conn=connect_to_database.connect()
rows=model.fetch_everything_from_table(conn,'data')
connect_to_database.close(conn)
return render_template('test.html',rows = rows)
def copy_data_from_src_to_dest(src,dest):
conn=connect_to_database.connect()
model.delete_everything_table(conn,dest)
param=model.fetch_a_b_c_from_table(conn,src)
model.insert_param_into_table(conn,dest,param)
connect_to_database.close(conn)
def update():
conn=connect_to_database.connect()
rows=model.fetch_everything_from_table(conn,'data2')
connect_to_database.close(conn)
return render_template('test.html',rows = rows)
def hash(id):
m=hashlib.md5()
m.update('gdkjssaklhd14252e8967bvsvvc')
m.update(str(id))
return m.hexdigest()
def check_id(id):
if int(hash(id),27)%11 == 0:
return True
else:
return False
def watermark(table_name):
conn=connect_to_database.connect()
count=0
params=[]
#print "Watermarked Tuples"
for i in range(1,10000):
if check_id(i) == True:
row = model.fetch_only_a_b_c_from_table(conn,table_name,i)
#model.update_only_a_b_c_in_table(conn,table_name,i,1,1,1)
a_old,b_old,c_old=row[0],row[1],row[2]
a8_old,b8_old=a_old%256,b_old%256
a_mod,b_mod=a_old-a8_old,b_old-b8_old
lbefore=(a8_old+b8_old)/2
hbefore=a8_old - b8_old
hdash=2*hbefore+1
g=(hdash+1)/2
f=hdash/2
anew8,bnew8=lbefore+g,lbefore-f
if ((anew8>=0 and anew8<256) and (bnew8>=0 and bnew8<256)):
a_new=a_mod+anew8
b_new=b_mod+bnew8
c_new=c_old|10
#print i,a_new,b_new,c_new
params.append((a_new,b_new,c_new,i))
count+=1
#print i
model.update_all_a_b_c_in_table(conn,table_name,params)
print "Total rows Updated %d"%count
connect_to_database.close(conn)
def reverse_watermark(table_name):
conn=connect_to_database.connect()
count=0
params=[]
#print "Reverse Watermarked Tuples"
for i in range(1,10000):
if check_id(i) == True:
row = model.fetch_only_a_b_c_from_table(conn,table_name,i)
#model.update_only_a_b_c_in_table(conn,table_name,i,1,1,1)
a_old,b_old,c_old=row[0],row[1],row[2]
a8_old,b8_old=a_old%256,b_old%256
a_mod,b_mod=a_old-a8_old,b_old-b8_old
lbefore=(a8_old+b8_old)/2
hbefore=a8_old - b8_old
hdash=hbefore/2
g=(hdash+1)/2
f=hdash/2
anew8,bnew8=lbefore+g,lbefore-f
if ((c_old&10)==10):
a_new=a_mod+anew8
b_new=b_mod+bnew8
temp=sys.maxint
c_new=c_old&(temp^10)
#print i,a_new,b_new,c_new
params.append((a_new,b_new,c_new,i))
count+=1
#print i
model.update_all_a_b_c_in_table(conn,table_name,params)
print "Total rows Updated %d"%count
connect_to_database.close(conn)
def count_similarity(list1,list2):
count=0;
#print list1
#print list2
for i in range(0,9999):
if (int(list1[i][0])==int(list2[i][0]))&(int(list1[i][1])==int(list2[i][1]))&(int(list1[i][2])==int(list2[i][2])):
count+=1
#print i+1
per=(float(count)/9999)*100
ans=9999-count
print "Total tuples not matched %d "%(ans)
return per
def compare_tables(src,dest):
conn=connect_to_database.connect()
list1=model.fetch_a_b_c_from_table(conn,src)
list2=model.fetch_a_b_c_from_table(conn,dest)
ans = count_similarity(list1,list2)
print ans
if __name__ == '__main__':
while True:
text=int(raw_input('''Enter 1 for refershing table_2
Enter 2 for watermarking table_2
Enter 3 for comparing original and watermarked table
Enter 4 for extracting watermark\n'''))
if text == 1:
copy_data_from_src_to_dest('data','data2')
elif text==2:
watermark('data2')
elif text==3:
compare_tables('data','data2')
elif text==4:
reverse_watermark('data2')
else:
break