init
This commit is contained in:
50
src/globals.py
Normal file
50
src/globals.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
import hashlib
|
||||
import base64
|
||||
import json
|
||||
import re
|
||||
import os
|
||||
|
||||
def canonical_json(value):
|
||||
return json.dumps(
|
||||
value,
|
||||
ensure_ascii=False,
|
||||
separators=(',',':'),
|
||||
sort_keys=True
|
||||
).encode("UTF-8")
|
||||
|
||||
'''
|
||||
def encode_base64(data: bytes) -> str:
|
||||
return base64.b64encode(data).decode('utf-8')
|
||||
|
||||
def sign_json(json_object, signing_key, signing_name):
|
||||
signatures = json_object.pop("signatures", {})
|
||||
unsigned = json_object.pop("unsigned", None)
|
||||
|
||||
signed = signing_key.sign(canonical_json(json_object))
|
||||
signature_base64 = encode_base64(signed)
|
||||
|
||||
key_id = "ed25519:VonaA"
|
||||
signatures.setdefault(signing_name, {})[key_id] = signature_base64
|
||||
|
||||
json_object["signatures"] = signatures
|
||||
if unsigned is not None:
|
||||
json_object["unsigned"] = unsigned
|
||||
|
||||
return json_object
|
||||
'''
|
||||
|
||||
vona_version = '1.2.3'
|
||||
|
||||
def make_event_id():
|
||||
return re.sub(r'[\/+=]', '_', base64.b64encode(os.urandom(32)).decode('utf-8'))[:44]
|
||||
|
||||
def hash_event(input) -> str:
|
||||
input.pop('signatures', None)
|
||||
input.pop('unsigned', None)
|
||||
|
||||
sha256_hash = hashlib.sha256(canonical_json(input)).digest()
|
||||
base64_encoded = base64.b64encode(sha256_hash)
|
||||
|
||||
return base64_encoded.decode().rstrip('=')
|
||||
Reference in New Issue
Block a user