forked from IdentityPython/pyMDOC-CBOR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
66 lines (48 loc) · 1.46 KB
/
tools.py
File metadata and controls
66 lines (48 loc) · 1.46 KB
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
# Aligns with https://github.com/eu-digital-identity-wallet/pyMDOC-CBOR
import json
import random
import cbor2
from cbor2.tool import DefaultEncoder, key_to_str
from pycose.messages import Sign1Message
def bytes2CoseSign1(data: bytes) -> Sign1Message:
"""
Gets bytes and return a COSE_Sign1 object
:param data: bytes: the COSE Sign1 as bytes
:return: Sign1Message: the COSE Sign1 object
"""
decoded = Sign1Message.decode(cbor2.loads(data).value)
return decoded
def cborlist2CoseSign1(data: list) -> Sign1Message:
"""
Gets cbor2 decoded COSE Sign1 as a list and return a COSE_Sign1 object
:param data: list: the COSE Sign1 as a list
:return: Sign1Message: the COSE Sign1 object
"""
decoded = Sign1Message.decode(
cbor2.dumps(
cbor2.CBORTag(18, value=data)
)
)
return decoded
def pretty_print(cbor_loaded: dict) -> None:
""""
Pretty print a CBOR object
:param cbor_loaded: dict: the CBOR object
"""
_obj = key_to_str(cbor_loaded)
res = json.dumps(
_obj,
indent=(None, 4),
cls=DefaultEncoder
)
print(res)
def shuffle_dict(d: dict) -> dict:
"""
Shuffle a dictionary
:param d: dict: the dictionary to shuffle
:return: dict: the shuffled dictionary
"""
keys = list(d.keys())
for i in range(random.randint(3, 27)): # nosec: B311
random.shuffle(keys)
return dict([(key, d[key]) for key in keys])