61 lines
1.5 KiB
Python
61 lines
1.5 KiB
Python
|
#!/usr/bin/python3
|
||
|
|
||
|
import requests
|
||
|
from hashlib import sha256
|
||
|
import ecdsa
|
||
|
|
||
|
private_key = '03486537091ceb021fb313e5cf3eb04d44ca2f19f72112a1'
|
||
|
# we need to send server:
|
||
|
# the question: domain.tld/get/<id>
|
||
|
# the checksum: ?sum=sha256
|
||
|
# the signed data: header 'auth'
|
||
|
|
||
|
id = 123
|
||
|
url = 'localhost:5000/get/'
|
||
|
url_id = url + str(id)
|
||
|
sum = sha256(url_id.encode('ascii')).hexdigest()
|
||
|
reg_url = 'http://' + url_id + '?sum=' + sum
|
||
|
|
||
|
unsigned_data = url_id + '?' + 'sum=' + sum
|
||
|
|
||
|
# Generate SK from the private key
|
||
|
private_key_int = int(private_key, 16)
|
||
|
sk = ecdsa.SigningKey.from_secret_exponent(private_key_int, curve=ecdsa.SECP256k1)
|
||
|
|
||
|
# sign the message
|
||
|
signature = sk.sign(unsigned_data.encode('utf-8'))
|
||
|
signature_hex = signature.hex()
|
||
|
|
||
|
print('we signed: ', unsigned_data)
|
||
|
print('We will send:')
|
||
|
print('to: ', reg_url)
|
||
|
print('sum: ', sum)
|
||
|
print('auth: ', signature_hex)
|
||
|
print('------------------------')
|
||
|
|
||
|
response = requests.get(reg_url, headers={"auth":signature_hex})
|
||
|
print('>>> ', response.status_code)
|
||
|
print('>>> ', response.content)
|
||
|
|
||
|
#ecdsa_public_key = '8716c78c09a4e4571a3112eca1c7ddce41289e20da446894b621f2a11ba91bc963f2e9fb9ddd5552c26faf814bc582b4'
|
||
|
ecdsa_public_key = '068716c78c09a4e4571a3112eca1c7ddce41289e20da446894b621f2a11ba91bc963f2e9fb9ddd5552c26faf814bc582b4'
|
||
|
|
||
|
bytes_public_key = bytes.fromhex(ecdsa_public_key)
|
||
|
|
||
|
bytes_signed_data = signature_hex.encode('utf-8')
|
||
|
|
||
|
|
||
|
vk = ecdsa.VerifyingKey.from_string(bytes_public_key, curve=ecdsa.SECP256k1)
|
||
|
|
||
|
if vk.verify(signature_hex, unsigned_data):
|
||
|
response = "YES"
|
||
|
else:
|
||
|
response = "NO"
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
exit(0)
|