fuwuqi/client.py
2023-01-18 18:55:53 +00:00

33 lines
1,017 B
Python

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}'
with open('private.pem', 'rb') as f:
privkey = serialization.load_pem_private_key(f.read(), None)
signature = b64encode(privkey.sign(
message.encode('utf8'),
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)