-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.py
executable file
·44 lines (36 loc) · 1.22 KB
/
sign.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
#!/usr/bin/env python3
import hashlib
import sys
from binascii import hexlify
try:
from ecdsa import SigningKey
from ecdsa.util import sigencode_string
except ImportError:
print("Please install ecdsa package")
sys.exit(1)
def gen_binary_signature(data, key_filename):
with open(key_filename, "r") as f:
key_pem = f.read()
key = SigningKey.from_pem(key_pem)
sig = key.sign_deterministic(
# Note: "sigencode_string" means generate just the r + s values, each 32
# bytes long, NOT the DER encoded format, which is variable length,
# 71-73 bytes total depending on r + s values.
data,
hashfunc=hashlib.sha256,
sigencode=sigencode_string,
)
return sig
if __name__ == "__main__":
if len(sys.argv) != 4:
print(f"Usage: {sys.argv[0]} <private-key> <file-to-sign> <output-file>")
sys.exit(1)
private_key = sys.argv[1]
with open(sys.argv[2], "rb") as f:
data = f.read()
sig = gen_binary_signature(data, private_key)
print(f"Signature: {hexlify(sig).decode()}", file=sys.stderr)
print(f"Signature length: {len(sig)}", file=sys.stderr)
with open(sys.argv[3], "wb") as f:
f.write(data)
f.write(sig)