#!/usr/bin/python3

import ecdsa
import binascii
import requests
from hashlib import sha256
from flask import Flask
from flask import request


##Generate them keys
# Generate private key (signing key)
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1) 
private_key_hex = sk.to_string().hex()

public_key = sk.verifying_key
public_key_hex = binascii.hexlify(public_key.to_string()).decode('utf-8')

keys = {
        "private_key": private_key_hex,
        "public_key": public_key_hex
        }

app = Flask(__name__)

@app.route('/send')
def send():
  message = b"localhost:5000/get/123?sum=5f944f849124d36621d5f0708c7752a84fa9caa90bba629b8db93eea44cd0d1a"

  print('private_key: ', private_key_hex)
  sig = sk.sign(message)

 
  reply = requests.get('http://localhost:5000/get/', headers={"auth": sig})
  return reply.str()

@app.route('/get')
def get():
  #vk = sk.get_verifying_key()
  # Get the public key from the signing key object
  print("vk2 - public_key:", keys['public_key'])
  
  # Get the sig from auth header
  sig = request.headers.get('auth')

  vk = ecdsa.VerifyingKey.from_string(bytes.fromhex(public_key_hex), curve=ecdsa.SECP256k1)
  print('sig: ', binascii.hexlify(sig).decode('utf-8'))
  if vk.verify(sig, message):
    print('vk1 # True ')
  else:
    print('vk1 # False ')
    
  
  vk2 = ecdsa.VerifyingKey.from_string(bytes.fromhex(public_key_hex), curve=ecdsa.SECP256k1) # the default is sha1
  if vk2.verify(sig, message):
    print('vk2 # True ')
  else:
    print('vk2 # False ')

