File: _utils.py

package info (click to toggle)
python-securesystemslib 1.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,316 kB
  • sloc: python: 5,319; sh: 38; makefile: 5
file content (26 lines) | stat: -rw-r--r-- 742 bytes parent folder | download
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
"""Signer utils for internal use."""

from __future__ import annotations

import hashlib
from typing import Any

from securesystemslib.exceptions import FormatError
from securesystemslib.formats import encode_canonical


def compute_default_keyid(keytype: str, scheme: str, keyval: dict[str, Any]) -> str:
    """Return sha256 hexdigest of the canonical json of the key."""
    data: str | None = encode_canonical(
        {
            "keytype": keytype,
            "scheme": scheme,
            "keyval": keyval,
        }
    )
    if isinstance(data, str):
        byte_data: bytes = data.encode("utf-8")
    else:
        raise FormatError("Failed to encode data into canonical json")

    return hashlib.sha256(byte_data).hexdigest()