-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigital_signature.py
60 lines (40 loc) · 1.21 KB
/
digital_signature.py
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 json
import nacl.signing
import nacl.encoding
#!pip install pynacl
print("")
print("Keys")
print("----")
signing_key = nacl.signing.SigningKey.generate()
verify_key = signing_key.verify_key
print(f"Signing key is: {signing_key.encode(encoder=nacl.encoding.HexEncoder).decode('utf-8')}")
print(f"Verify key is: {verify_key.encode(encoder=nacl.encoding.HexEncoder).decode('utf-8')}")
print("")
print("Transaction")
print("-----------")
sender = input("Enter sender: ")
recipient = input("Enter recipient ")
amount = int(input("Enter amount: "))
transaction = (sender, recipient, amount)
print("")
print(f"Transaction is: {transaction}")
converted = json.dumps(transaction)
encoded_transaction = converted.encode('utf-8')
print("")
print("Signature")
print("---------")
signed_transaction = signing_key.sign(encoded_transaction)
print(f"Signature is: {signed_transaction.signature.hex()}")
print("")
print("Auth")
print("----")
signature = bytes.fromhex(input("Enter signature to validate: "))
def verify(key, message, sig):
try:
key(message,sig)
print("Signature valid")
except:
print("Signature invalid")
print("")
verify(verify_key.verify, encoded_transaction, signature)
print("")