-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge49_server.py
More file actions
43 lines (39 loc) · 1.22 KB
/
Copy pathchallenge49_server.py
File metadata and controls
43 lines (39 loc) · 1.22 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
#!/usr/bin/env python3
from fastapi import FastAPI
import re
from aes import aes_cbc_encrypt
from padding import pkcs7_unpad, detect_padding, pkcs7_pad
KEY = b'YELLOW SUBMARINE'
IV = b'\x00'*16
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/transfer")
def transfer(hexm: str):
msg = bytes.fromhex(hexm)
MAC = msg[-16:]
MSG = msg[:-16]
if not detect_padding(MSG):
MSG = pkcs7_pad(MSG)
if verify(MSG, IV, MAC):
MSG = pkcs7_unpad(MSG)
MSG = MSG.decode('utf-8')
from_user = re.search('from=#(\w+)', MSG).group(1)
# transactions are in the form of to:amount;to:amount*
transactions = re.search('tx_list=#(.+)', MSG).group(1)
transactions = transactions.split(';')
json_transactions = []
for transaction in transactions:
to, amount = transaction.split(':')
json_transactions.append({'to': to, 'amount': amount})
return {
"status": "success",
"from": from_user,
"transactions": json_transactions,
}
else:
return {"status": "error"}
def verify(MSG, IV, MAC):
key = KEY
return aes_cbc_encrypt(MSG, key, IV)[-16:] == MAC