import os
import ecdsa

# 生成私钥和公钥
def generate_keys():
    # 使用secp256k1曲线生成密钥对
    sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
    vk = sk.get_verifying_key()
    return sk, vk

# 对消息进行签名
def sign_message(private_key, message):
    return private_key.sign(message)

# 验证签名
def verify_signature(public_key, message, signature):
    return public_key.verify(signature, message)

# %%
private_key, public_key = generate_keys()
# 简单消息
message = b"Hello, ECDSA!"

# 签名消息
signature = sign_message(private_key, message)
print(f"Signature: {signature.hex()}")


# %% 
sig='adee313bebf78c1bd4a9719e0ad9aa871ff8195788ad2d7510f35e1ef0ce00a6cbfea518e4ea1716a729c7313a52da8b0c57ff0e0f97251ea604beef3890f699'
message = b"Hello, ECDSA!"
signature = bytes.fromhex(sig)
print(verify_signature(public_key, message, signature))   #验证签名