aboutsummaryrefslogtreecommitdiff
path: root/client.py
blob: 7a74607662ba06776120ede375c161392191d15d (plain)
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
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding
from base64 import b64encode
from email.utils import formatdate
from requests import post
from sys import argv

with open(argv[1], 'rb') as f:
    activity = f.read()

date = formatdate(usegmt=True)
digester = hashes.Hash(hashes.SHA256())
digester.update(activity)
digest = b64encode(digester.finalize()).decode()
message = f'date: {date}\ndigest: SHA-256={digest}'.encode('utf8')

with open('private.pem', 'rb') as f:
    privkey = serialization.load_pem_private_key(f.read(), None)

signature = b64encode(privkey.sign(message, padding.PKCS1v15(), hashes.SHA256())).decode()
header = f'keyId="https://0.exozy.me/users/test.jsonld#main-key",headers="date digest",signature="{signature}"'

resp = post('https://0.exozy.me/users/test.outbox', headers={
    'Date': date,
    'Digest': f'SHA-256={digest}',
    'Signature': header,
}, data=activity)
print(resp)
print(resp.text)