Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/cryptography/hazmat/backends/openssl/utils.py: 92%
12 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 07:26 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 07:26 +0000
1# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
5from __future__ import annotations
7from cryptography.hazmat.primitives import hashes
8from cryptography.hazmat.primitives.asymmetric.utils import Prehashed
11def _calculate_digest_and_algorithm(
12 data: bytes,
13 algorithm: Prehashed | hashes.HashAlgorithm,
14) -> tuple[bytes, hashes.HashAlgorithm]:
15 if not isinstance(algorithm, Prehashed):
16 hash_ctx = hashes.Hash(algorithm)
17 hash_ctx.update(data)
18 data = hash_ctx.finalize()
19 else:
20 algorithm = algorithm._algorithm
22 if len(data) != algorithm.digest_size:
23 raise ValueError(
24 "The provided data must be the same length as the hash "
25 "algorithm's digest size."
26 )
28 return (data, algorithm)