-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge.py
More file actions
80 lines (59 loc) · 2.23 KB
/
challenge.py
File metadata and controls
80 lines (59 loc) · 2.23 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 requests
from eth_account import Account
import secrets
from hashlib import sha256
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5
from base64 import b64decode, b64encode
baseUrl = "http://34.163.219.17:3000/"
def generateAddress():
priv = secrets.token_hex(32)
private_key = "0x" + priv
print ("SAVE BUT DO NOT SHARE THIS:", private_key)
acct = Account.from_key(private_key)
print("Address:", acct.address)
return acct.address
address = generateAddress()
def subscribe():
ret = requests.post(baseUrl + "subscribe", json={"Name": "gaetan", "Address": address})
if ret.status_code == 201:
return ret.json()
raise Exception("Failed to subscribe")
def getHashChallenge():
ret = requests.get(
baseUrl + "challenge/hash/" + address
)
if ret.status_code == 200:
return ret.json()
raise Exception("Failed to get the challenge")
def verifyHash(hashChallenge, hash):
ret = requests.post(
baseUrl + "challenge/hash/" + address + "/" + hashChallenge["challenge_id"],
json={"Sentence": hashChallenge["sentence"], "Hash":hash},
)
if ret.status_code == 200:
return ret.json()
raise Exception("Hash challenge failed")
def getCypherChallenge():
ret = requests.get(baseUrl + "challenge/encrypt/" + address)
if ret.status_code == 200:
return ret.json()
raise Exception("Failed to get the challenge")
def verifyCypher(chypherChallenge, cypherText):
ret = requests.post(
baseUrl + "challenge/hash/" + address + "/" + chypherChallenge["challenge_id"],
json={"Sentence": chypherChallenge["sentence"], "cyphertext": cypherText},
)
if ret.status_code == 200:
return ret.json()
raise Exception("Cypher challenge failed")
subscribeMsg = subscribe()
hashChallenge = getHashChallenge()
hash = sha256(hashChallenge["sentence"].encode("utf-8")).hexdigest()
hashMsg = verifyHash(hashChallenge, hash)
cypherChallenge = getCypherChallenge()
keyPub = RSA.importKey(cypherChallenge["public_key"])
cipher = Cipher_PKCS1_v1_5.new(keyPub)
cipher_text = cipher.encrypt(cypherChallenge["sentence"].encode())
cypherMsg = verifyCypher(cypherChallenge, cipher_text)
print(cypherMsg)