-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjsonsig.py
More file actions
31 lines (25 loc) · 890 Bytes
/
jsonsig.py
File metadata and controls
31 lines (25 loc) · 890 Bytes
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
import simplejson as json
import pysodium
import base64
def sign_json(message, pk, sk):
try:
del message['sender']
except KeyError:
True
try:
del message['signature']
except KeyError:
True
canonicalized = json.dumps(message, separators=(',', ':'), sort_keys=True)
signature = pysodium.crypto_sign_detached(canonicalized, sk)
message['sender'] = base64.b64encode(pk)
message['signature'] = base64.b64encode(signature)
return message
def check_json_signature(message):
message = message.copy()
sender = message['sender']
del message['sender']
signature = message['signature']
del message['signature']
canonicalized = json.dumps(message, separators=(',', ':'), sort_keys=True)
return pysodium.crypto_sign_verify_detached(base64.b64decode(signature), canonicalized, base64.b64decode(sender))