hbc/pywii/pywii-tools/ecsign.py

35 lines
710 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
import pywii as wii
if len(sys.argv) != 4:
2021-03-17 15:53:31 +01:00
print("Usage: %s keyfile.priv infile outfile"%sys.argv[0])
2016-11-23 06:35:12 +01:00
sys.exit(1)
if sys.argv[1] == "-":
k = sys.stdin.read()
else:
k = open(sys.argv[1],"rb").read()
if len(k) != 30:
2021-03-17 15:53:31 +01:00
print("Failed to read private key")
2016-11-23 06:35:12 +01:00
sys.exit(2)
indata = open(sys.argv[2],"rb").read()
sha = wii.SHA.new(indata).digest()
2021-03-17 15:53:31 +01:00
print("SHA1: %s"%sha.encode('hex'))
print()
print("Signature:")
2016-11-23 06:35:12 +01:00
r,s = wii.ec.generate_ecdsa(k,sha)
2021-03-17 15:53:31 +01:00
print("R =",r[:15].encode('hex'))
print(" ",r[15:].encode('hex'))
print("S =",s[:15].encode('hex'))
print(" ",s[15:].encode('hex'))
2016-11-23 06:35:12 +01:00
outdata = "SIG0" + r + s + indata
fd = open(sys.argv[3],"wb")
fd.write(outdata)
fd.close()