#!/usr/bin/python3

import ecdsa, binascii, sys
from hashlib import sha256

message = 'Hello public/private key world!'
hashed_message = sha256(message.encode('utf-8')).hexdigest()

m = message + hashed_message
print('')
print('to be signed: ', m)
to_be_signed = binascii.hexlify(m.encode('utf-8'))

# Get the keys in their raw format
signing_key = ecdsa.SigningKey.generate()
public_key = signing_key.verifying_key

signature = signing_key.sign(to_be_signed)
signature_hex = signature.hex()

# deform_signature if deform argument is given
if len(sys.argv) > 1:
  if sys.argv[1] == 'deform':
    max_id = len(signature)
    for i in range(max_id):
      if i + 2 <= max_id:
        mess_id = i + 2
        mess = signature[mess_id].to_bytes(4, 'big')
        replace_me = signature[i].to_bytes(4, 'big')
        #print('>>> replacing ', replace_me, ' with ', mess )
        print('>>> ', i, 'to', mess_id, ', max is: ', max_id)
        signature = signature.replace(replace_me, mess )

print('signed: ', signature_hex)
print('')

try:
  is_valid =  public_key.verify(signature, to_be_signed)
except ecdsa.keys.BadSignatureError:
  is_valid = False
  print('Something bad is on foot')

if is_valid:
  print('This is COOL')
else:
  print('Something bad is on foot')

exit(0)

