Debian Codegraph @

addons/auth_passkey/_vendor/webauthn/helpers/hash_by_alg.py

raw
1 import hashlib
2 from typing import Optional
3
4 from .cose import COSEAlgorithmIdentifier
5
6 SHA_256 = [
7 COSEAlgorithmIdentifier.ECDSA_SHA_256,
8 COSEAlgorithmIdentifier.RSASSA_PSS_SHA_256,
9 COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_256,
10 ]
11 SHA_384 = [
12 COSEAlgorithmIdentifier.RSASSA_PSS_SHA_384,
13 COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_384,
14 ]
15 SHA_512 = [
16 COSEAlgorithmIdentifier.ECDSA_SHA_512,
17 COSEAlgorithmIdentifier.RSASSA_PSS_SHA_512,
18 COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_512,
19 ]
20 SHA_1 = [
21 COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_1,
22 ]
23
24
25 def hash_by_alg(to_hash: bytes, alg: Optional[COSEAlgorithmIdentifier] = None) -> bytes:
26 """
27 Generate a hash of `to_hash` by the specified COSE algorithm ID. Defaults to hashing
28 with SHA256
29 """
30 # Default to SHA256 for hashing
31 hash = hashlib.sha256()
32
33 if alg in SHA_384:
34 hash = hashlib.sha384()
35 elif alg in SHA_512:
36 hash = hashlib.sha512()
37 elif alg in SHA_1:
38 hash = hashlib.sha1()
39
40 hash.update(to_hash)
41 return hash.digest()