-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheddsa_libnacl.py
85 lines (63 loc) · 2.45 KB
/
eddsa_libnacl.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import hashlib
import logging
import sys
import time
import libnacl.sign
def main():
"""
pip install libnacl
Dependencies
brew install libsodium
apt install libsodium23
yum install libsodium
Documentation
https://libnacl.readthedocs.io/en/latest/topics/sign.html
Python ctypes wrapper for libsodium
https://github.com/saltstack/libnacl
"""
# INFO or DEBUG
logging.basicConfig(level=logging.INFO)
# Input file
filename = sys.argv[1]
# Utils
param_hash = hashlib.sha256
# Timing variables
key_time = 0
sign_time = 0
verify_time = 0
logging.info("Started libnacl libsodium Ed25519")
with open(filename, "r") as f:
# skip file reading time as well
start = time.time()
for m in f:
logging.debug(m)
start_keygen = time.time()
private_key = libnacl.sign.Signer()
public_key = libnacl.sign.Verifier(private_key.hex_vk())
key_time += time.time() - start_keygen
# This may not be required as message will be hashed sha512 during sign() process.
# Unless we deliberately want to hash the message.
# Look at: https://pynacl.readthedocs.io/en/stable/_images/ed25519.png
# m = param_hash(m.encode()).hexdigest()
# logging.debug("param_hash m: %s", m)
start_sign = time.time()
signed = private_key.sign(m)
sign_time += time.time() - start_sign
signature = private_key.signature(m)
# logging.debug("binary_signature: %s", signature)
start_verify = time.time()
try:
public_key.verify(signature + m)
logging.debug("Verified: True")
except ValueError as e:
logging.warn("ValueError: %s", e.message)
exit(1)
verify_time += time.time() - start_verify
sum_time = key_time + sign_time + verify_time
logging.info("Total time cost for generating key is: " + str(key_time) + " s")
logging.info("Total time cost for signing the message is: " + str(sign_time) + " s")
logging.info("Total time cost for verifying the message is: " + str(verify_time) + " s")
logging.info("Total time cost for key generating, message signing and verifying: " + str(sum_time) + " s")
logging.info("Total time cost for system time: " + str(time.time() - start) + " s")
if __name__ == "__main__":
main()