Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/securesystemslib/signer/_utils.py: 58%

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

12 statements  

1"""Signer utils for internal use.""" 

2 

3from __future__ import annotations 

4 

5import hashlib 

6from typing import Any 

7 

8from securesystemslib.exceptions import FormatError 

9from securesystemslib.formats import encode_canonical 

10 

11 

12def compute_default_keyid(keytype: str, scheme: str, keyval: dict[str, Any]) -> str: 

13 """Return sha256 hexdigest of the canonical json of the key.""" 

14 data: str | None = encode_canonical( 

15 { 

16 "keytype": keytype, 

17 "scheme": scheme, 

18 "keyval": keyval, 

19 } 

20 ) 

21 if isinstance(data, str): 

22 byte_data: bytes = data.encode("utf-8") 

23 else: 

24 raise FormatError("Failed to encode data into canonical json") 

25 

26 return hashlib.sha256(byte_data).hexdigest()