-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.py
68 lines (55 loc) · 1.72 KB
/
encoder.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
import sys
import click
import hashlib
from lxml import etree
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import utils, padding
@click.command()
@click.option("--pk", help="private key pem")
@click.option("--xml", help="xml soap message")
def sign(pk, xml):
pk_bytes = None
request: etree.Element = None
soap_body = None
try:
with open(pk, "r") as pk_file:
pk_bytes = pk_file.read()
if "-----BEGIN PRIVATE KEY-----" not in pk_bytes:
raise RuntimeError("invalid pk file")
except FileNotFoundError:
print("pk file not found")
sys.exit(1)
else:
print("pk ok")
pk_bytes = bytes(pk_bytes.encode("utf-8"))
try:
with open(xml, "r") as xml_file:
request = xml_file.read()
except FileNotFoundError:
print("request file not found")
sys.exit(1)
else:
print("request ok")
request = etree.XML(request)
soap_body = request.find(
"soap:Body", namespaces={"soap": "http://schemas.xmlsoap.org/soap/envelope/"}
)
hasher = hashlib.sha256()
hasher.update(etree.tostring(soap_body))
soap_raw_digest = hasher.digest()
key = serialization.load_pem_private_key(pk_bytes, password=None)
signature = key.sign(
soap_raw_digest,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.MGF1.MAX_LENGTH
),
hashes.SHA256()
)
print("--- HEX DIGEST ---")
print(hasher.hexdigest())
print("--- SIGNATURE ---")
print(signature.hex())
if __name__ == "__main__":
sign()