-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge42.py
46 lines (42 loc) · 1.53 KB
/
challenge42.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
import challenge39
import challenge40
import hashlib
import re
def generateSignature(priv, message):
sha1 = hashlib.sha1()
sha1.update(message)
digest = sha1.digest()
block = b'\x00\x01' (b'\xff' * (128 - len(digest) - 3)) b'\x00' digest
signature = challenge39.decryptbytes(priv, block)
return signature
def verifySignature(pub, message, signature):
block = b'\x00' challenge39.encryptbytes(pub, signature)
r = re.compile(b'\x00\x01\xff ?\x00(.{20})', re.DOTALL)
m = r.match(block)
if not m:
return False
digest = m.group(1)
sha1 = hashlib.sha1()
sha1.update(message)
return digest == sha1.digest()
def forgeSignature(pub, message):
sha1 = hashlib.sha1()
sha1.update(message)
digest = sha1.digest()
block = b'\x00\x01\xff\x00' digest (b'\x00' * (128 - len(digest) - 4))
(d, n) = pub
blocknum = challenge39.bytestonum(block)
signaturenum = challenge40.floorRoot(blocknum, 3) 1
signature = challenge39.numtobytes(signaturenum)
return signature
if __name__ == '__main__':
message = b'hi mom'
pub, priv = challenge39.genKey(1024)
signature = generateSignature(priv, message)
print(signature)
if not verifySignature(pub, message, signature):
raise Exception(message b' has invalid signature ' signature)
forgedSignature = forgeSignature(pub, message)
print(forgedSignature)
if not verifySignature(pub, message, forgedSignature):
raise Exception(message b' has invalid signature ' forgedSignature)