-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathdigital_signature_example.py
More file actions
60 lines (39 loc) · 1.54 KB
/
digital_signature_example.py
File metadata and controls
60 lines (39 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import binascii
from Crypto.Hash import SHA256
from ecdsa import SigningKey, VerifyingKey, SECP256k1, keys
from random import randint
class Transaction:
def __init__(self, public_key):
self.id = randint(1, 10**5)
self.signature = None
self.public_key = public_key
def verify(self):
vk = VerifyingKey.from_string(bytes.fromhex(self.public_key), curve=SECP256k1)
try:
vk.verify(bytes.fromhex(self.signature), SHA256.new(str(self.id).encode()).digest())
except keys.BadSignatureError:
print('invalid transaction signature')
return False
return True
def sign(self, private_key):
data_to_sign = SHA256.new(str(self.id).encode()).digest()
sk = SigningKey.from_string(bytes.fromhex(private_key), curve=SECP256k1)
self.signature = binascii.b2a_hex(sk.sign(data_to_sign)).decode()
class Wallet:
def __init__(self):
self.private_key = None
self.public_key = None
def generate_key_pair(self):
sk = SigningKey.generate(curve=SECP256k1)
self.private_key = binascii.b2a_hex(sk.to_string()).decode()
self.public_key = binascii.b2a_hex(sk.get_verifying_key().to_string()).decode()
account = Wallet()
account.generate_key_pair()
print("Generated public key: %s" % account.public_key)
print("Generated private key: %s" % account.private_key)
tx = Transaction(account.public_key)
tx.sign(account.private_key)
print("Generated signature: %s" % tx.signature)
tx.verify()
tx.id = '1234'
tx.verify()