@@ -163,22 +163,29 @@ def _get_hash_algorithm(public_key: Key) -> str:
163
163
# TODO: This could be a public abstract method on Key so that GCPSigner
164
164
# would not be tied to a specific Key implementation -- not all keys
165
165
# have a pre hash algorithm though.
166
- if (
167
- public_key .keytype == "rsa" and public_key .scheme .endswith (("256" , "512" ))
168
- ) or (
166
+ if public_key .keytype == "rsa" :
167
+ # hash algorithm is encoded as last scheme portion
168
+ algo = public_key .scheme .split ("-" )[- 1 ]
169
+ elif public_key .keytype in [
170
+ "ecdsa" ,
171
+ "ecdsa-sha2-nistp256" ,
172
+ "ecdsa-sha2-nistp384" ,
173
+ ]:
169
174
# nistp256 uses sha-256, nistp384 uses sha-384
170
- # TODO: Check for invalid type/scheme combinations (#766)
171
- public_key .keytype
172
- in ["ecdsa" , "ecdsa-sha2-nistp256" , "ecdsa-sha2-nistp384" ]
173
- and public_key .scheme .endswith (("256" , "384" ))
174
- ):
175
- algo = public_key .scheme [- 3 :]
175
+ bits = public_key .scheme .split ("-nistp" )[- 1 ]
176
+ algo = f"sha{ bits } "
176
177
else :
177
178
raise exceptions .UnsupportedAlgorithmError (
178
- f"Unsupported { public_key .keytype } /{ public_key .scheme } "
179
- f"(type/scheme) in key { public_key .keyid } "
179
+ f"Unsupported key type { public_key .keytype } in key { public_key .keyid } "
180
180
)
181
181
182
+ # trigger UnsupportedAlgorithm if appropriate
183
+ # TODO: validate scheme/algo in constructor (#766)
184
+ try :
185
+ _ = hashlib .new (algo )
186
+ except (ValueError , TypeError ) as e :
187
+ raise exceptions .UnsupportedAlgorithmError (algo ) from e
188
+
182
189
return algo
183
190
184
191
def sign (self , payload : bytes ) -> Signature :
0 commit comments