chatgpt/encryption-on-apis/flask-encrypted-api.py
2022-12-17 22:39:21 +02:00

61 lines
1.4 KiB
Python
Executable File

#!/usr/bin/python3
from flask import Flask
from flask import request
import ecdsa
import codecs
ecdsa_public_key = '8716c78c09a4e4571a3112eca1c7ddce41289e20da446894b621f2a11ba91bc963f2e9fb9ddd5552c26faf814bc582b4'
#ecdsa_public_key = '048716c78c09a4e4571a3112eca1c7ddce41289e20da446894b621f2a11ba91bc963f2e9fb9ddd5552c26faf814bc582b4'
app = Flask(__name__)
@app.route("/get/<id>", methods=['get'])
def get(id):
r_id = id
r_sum = request.args.get('sum')
r_auth = request.headers.get('auth')
print('---------------------------')
print('host: ', request.host)
print('full_path: ', request.full_path)
print('---------------------------')
print('id: ', r_id)
print('sum: ', r_sum)
print('header, auth:', r_auth)
signed_data = request.host + request.full_path
print('might have been signed: ', signed_data)
r_auth_bytes = bytes.fromhex(str(r_auth))
#x_coord = ecdsa_public_key[:64]
#y_coord = ecdsa_public_key[64:]
#
#if int(y_coord, 16) % 2 == 0:
# prefix = b'\x02'
#else:
# prefix = b'\x03'
#
#bytes_public_key = prefix + codecs.decode(x_coord, 'hex')
bytes_public_key = bytes.fromhex(ecdsa_public_key)
bytes_signed_data = signed_data.encode('utf-8')
vk = ecdsa.VerifyingKey.from_string(bytes_public_key, curve=ecdsa.SECP256k1)
if vk.verify(r_auth_bytes, bytes_signed_data):
response = "YES"
else:
response = "NO"
return response
if __name__== "__main__":
app.run()