Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/securesystemslib/_internal/utils.py: 45%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

11 statements  

1"""Internal utilities""" 

2 

3import base64 

4import binascii 

5 

6 

7def b64enc(data: bytes) -> str: 

8 """To encode byte sequence into base64 string 

9 

10 Arguments: 

11 data: Byte sequence to encode 

12 

13 Exceptions: 

14 TypeError: If "data" is not byte sequence 

15 

16 Returns: 

17 base64 string 

18 """ 

19 

20 return base64.standard_b64encode(data).decode("utf-8") 

21 

22 

23def b64dec(string: str) -> bytes: 

24 """To decode byte sequence from base64 string 

25 

26 Arguments: 

27 string: base64 string to decode 

28 

29 Raises: 

30 binascii.Error: If invalid base64-encoded string 

31 

32 Returns: 

33 A byte sequence 

34 """ 

35 

36 data = string.encode("utf-8") 

37 try: 

38 return base64.b64decode(data, validate=True) 

39 except binascii.Error: 

40 # altchars for urlsafe encoded base64 - instead of + and _ instead of / 

41 return base64.b64decode(data, altchars=b"-_", validate=True)