-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRing_Signature.py
More file actions
80 lines (68 loc) · 2.11 KB
/
Ring_Signature.py
File metadata and controls
80 lines (68 loc) · 2.11 KB
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
import os, hashlib, random, Crypto.PublicKey.RSA
import sys
from functools import reduce
class ring:
def __init__(self, k, L=1024):
self.k = k
self.l = L
self.n = len(list(k))
self.q = 1 << (L - 1)
def sign(self, m, z):
self.permut(m)
s = [None] * self.n
u = random.randint(0, self.q)
c = v = self.E(u)
for i in (list(range(z+1, self.n)) + list(range(z))):
s[i] = random.randint(0, self.q)
e = self.g(s[i], self.k[i].e, self.k[i].n)
v = self.E(v^e)
if (i+1) % self.n == 0:
c = v
s[z] = self.g(v^u, self.k[z].d, self.k[z].n)
return [c] + s
def verify(self, m, X):
self.permut(m)
def _f(i):
return self.g(X[i+1], self.k[i].e, self.k[i].n)
y = list(map(_f, list(range(len(X)-1))))
def _g(x, i):
return self.E(x^y[i])
r = reduce(_g, list(range(self.n)), X[0])
return r == X[0]
def permut(self, m):
self.p = int(hashlib.sha1(m.encode()).hexdigest(),16)
# sha1(s.encode(encoding)).hexdigest()
def E(self, x):
msg = '%s%s' % (x, self.p)
return int(hashlib.sha1(msg.encode()).hexdigest(),16)
def g(self, x, e, n):
q, r = divmod(x, n)
if ((q + 1) * n) <= ((1 << self.l) - 1):
rslt = q * n + pow(r, e, n)
else:
rslt = x
return rslt
size = 4
#msg1="Hello"
text_file = open("/Users/sonipriyapaul/Downloads/E_ID.txt","r")
data = text_file.read()
text_file.close()
print(data)
def _rn(_):
return Crypto.PublicKey.RSA.generate(1024, os.urandom)
#print(("Message is:",msg1))
key = list(map(_rn, list(range(size))))
r = ring(key)
f = open("Signed.txt","a+")
for i in range(size):
s1 = r.sign(data, i)
print(type(s1))
#s1_convert = ''.join(s1)
f.write(str(s1))
#print(type(s1))
# s2 = r.sign(msg2, i)
if (i==1):
print(("Signature is", s1))
#print(("Signature verified:",r.verify(data, s1)))
# assert r.verify(msg1, s1) and r.verify(msg2, s2) and not r.verify(msg1, s2)
f.close()