hbc/pywii/pywii-tools/ecgenpriv.py

34 lines
597 B
Python
Raw Normal View History

2021-03-17 15:53:31 +01:00
#!/usr/bin/env python3
2016-11-23 06:35:12 +01:00
import sys, os
import pywii as wii
if len(sys.argv) != 2:
2021-03-17 15:53:31 +01:00
print("Usage: %s keyfile.priv"%sys.argv[0])
2016-11-23 06:35:12 +01:00
sys.exit(1)
2021-03-17 15:53:31 +01:00
print("Generating private key...")
2016-11-23 06:35:12 +01:00
k = wii.ec.gen_priv_key()
2021-03-17 15:53:31 +01:00
print("Private key:")
2016-11-23 06:35:12 +01:00
pk = k.encode('hex')
2021-03-17 15:53:31 +01:00
print("K =",pk[:30])
print(" ",pk[30:])
2016-11-23 06:35:12 +01:00
2021-03-17 15:53:31 +01:00
print()
print("Corresponding public key:")
2016-11-23 06:35:12 +01:00
q = wii.ec.priv_to_pub(k)
pq = q.encode('hex')
2021-03-17 15:53:31 +01:00
print("X =",pq[:30])
print(" ",pq[30:60])
print("Y =",pq[60:90])
print(" ",pq[90:])
2016-11-23 06:35:12 +01:00
fd = open(sys.argv[1],"wb")
os.fchmod(fd.fileno(), 0o600)
fd.write(k)
fd.close()
2021-03-17 15:53:31 +01:00
print("Saved private key to %s"%sys.argv[1])