@@ -317,39 +317,131 @@ def fullname(self, implementation):
317317 return f"{ module_name } .{ method_name } "
318318
319319
320- # Mapping from a "canonical" name to a pair (HACL*, _hashlib.*, hashlib.*)
321- # constructors. If the constructor name is None, then this means that the
322- # algorithm can only be used by the "agile" new() interfaces.
323- _EXPLICIT_CONSTRUCTORS = MappingProxyType ({ # fmt: skip
324- HashId .md5 : HashInfo ("_md5.md5" , "openssl_md5" , "md5" ),
325- HashId .sha1 : HashInfo ("_sha1.sha1" , "openssl_sha1" , "sha1" ),
326- HashId .sha224 : HashInfo ("_sha2.sha224" , "openssl_sha224" , "sha224" ),
327- HashId .sha256 : HashInfo ("_sha2.sha256" , "openssl_sha256" , "sha256" ),
328- HashId .sha384 : HashInfo ("_sha2.sha384" , "openssl_sha384" , "sha384" ),
329- HashId .sha512 : HashInfo ("_sha2.sha512" , "openssl_sha512" , "sha512" ),
330- HashId .sha3_224 : HashInfo (
331- "_sha3.sha3_224" , "openssl_sha3_224" , "sha3_224"
320+ class _HashInfo :
321+ """Dataclass containing information for supported hash functions.
322+
323+ - *builtin_method_fullname* is the fully-qualified name
324+ of the HACL* hash constructor function, e.g., "_md5.md5".
325+
326+ - *openssl_method_fullname* is the fully-qualified name
327+ of the "_hashlib" module method for the explicit OpenSSL
328+ hash constructor function, e.g., "_hashlib.openssl_md5".
329+
330+ - *hashlib_method_fullname* is the fully-qualified name
331+ of the "hashlib" module method for the explicit hash
332+ constructor function, e.g., "hashlib.md5".
333+ """
334+
335+ def __init__ (
336+ self ,
337+ name ,
338+ builtin_method_fullname ,
339+ openssl_method_fullname = None ,
340+ hashlib_method_fullname = None ,
341+ ):
342+ self .name = name
343+ self .func = _HashFuncInfo (
344+ name ,
345+ builtin_method_fullname ,
346+ openssl_method_fullname ,
347+ hashlib_method_fullname ,
348+ )
349+
350+
351+ _HASHINFO_DATABASE = MappingProxyType ({
352+ HashId .md5 : _HashInfo (
353+ HashId .md5 ,
354+ "_md5.md5" ,
355+ "_hashlib.openssl_md5" ,
356+ "hashlib.md5" ,
357+ ),
358+ HashId .sha1 : _HashInfo (
359+ HashId .sha1 ,
360+ "_sha1.sha1" ,
361+ "_hashlib.openssl_sha1" ,
362+ "hashlib.sha1" ,
363+ ),
364+ HashId .sha224 : _HashInfo (
365+ HashId .sha224 ,
366+ "_sha2.sha224" ,
367+ "_hashlib.openssl_sha224" ,
368+ "hashlib.sha224" ,
332369 ),
333- HashId .sha3_256 : HashInfo (
334- "_sha3.sha3_256" , "openssl_sha3_256" , "sha3_256"
370+ HashId .sha256 : _HashInfo (
371+ HashId .sha256 ,
372+ "_sha2.sha256" ,
373+ "_hashlib.openssl_sha256" ,
374+ "hashlib.sha256" ,
335375 ),
336- HashId .sha3_384 : HashInfo (
337- "_sha3.sha3_384" , "openssl_sha3_384" , "sha3_384"
376+ HashId .sha384 : _HashInfo (
377+ HashId .sha384 ,
378+ "_sha2.sha384" ,
379+ "_hashlib.openssl_sha384" ,
380+ "hashlib.sha384" ,
338381 ),
339- HashId .sha3_512 : HashInfo (
340- "_sha3.sha3_512" , "openssl_sha3_512" , "sha3_512"
382+ HashId .sha512 : _HashInfo (
383+ HashId .sha512 ,
384+ "_sha2.sha512" ,
385+ "_hashlib.openssl_sha512" ,
386+ "hashlib.sha512" ,
341387 ),
342- HashId .shake_128 : HashInfo (
343- "_sha3.shake_128" , "openssl_shake_128" , "shake_128"
388+ HashId .sha3_224 : _HashInfo (
389+ HashId .sha3_224 ,
390+ "_sha3.sha3_224" ,
391+ "_hashlib.openssl_sha3_224" ,
392+ "hashlib.sha3_224" ,
344393 ),
345- HashId .shake_256 : HashInfo (
346- "_sha3.shake_256" , "openssl_shake_256" , "shake_256"
394+ HashId .sha3_256 : _HashInfo (
395+ HashId .sha3_256 ,
396+ "_sha3.sha3_256" ,
397+ "_hashlib.openssl_sha3_256" ,
398+ "hashlib.sha3_256" ,
399+ ),
400+ HashId .sha3_384 : _HashInfo (
401+ HashId .sha3_384 ,
402+ "_sha3.sha3_384" ,
403+ "_hashlib.openssl_sha3_384" ,
404+ "hashlib.sha3_384" ,
405+ ),
406+ HashId .sha3_512 : _HashInfo (
407+ HashId .sha3_512 ,
408+ "_sha3.sha3_512" ,
409+ "_hashlib.openssl_sha3_512" ,
410+ "hashlib.sha3_512" ,
411+ ),
412+ HashId .shake_128 : _HashInfo (
413+ HashId .shake_128 ,
414+ "_sha3.shake_128" ,
415+ "_hashlib.openssl_shake_128" ,
416+ "hashlib.shake_128" ,
417+ ),
418+ HashId .shake_256 : _HashInfo (
419+ HashId .shake_256 ,
420+ "_sha3.shake_256" ,
421+ "_hashlib.openssl_shake_256" ,
422+ "hashlib.shake_256" ,
423+ ),
424+ HashId .blake2s : _HashInfo (
425+ HashId .blake2s ,
426+ "_blake2.blake2s" ,
427+ None ,
428+ "hashlib.blake2s" ,
429+ ),
430+ HashId .blake2b : _HashInfo (
431+ HashId .blake2b ,
432+ "_blake2.blake2b" ,
433+ None ,
434+ "hashlib.blake2b" ,
347435 ),
348- HashId .blake2s : HashInfo ("_blake2.blake2s" , None , "blake2s" ),
349- HashId .blake2b : HashInfo ("_blake2.blake2b" , None , "blake2b" ),
350436})
351- assert _EXPLICIT_CONSTRUCTORS .keys () == CANONICAL_DIGEST_NAMES
352- get_hash_info = _EXPLICIT_CONSTRUCTORS .__getitem__
437+ assert _HASHINFO_DATABASE .keys () == CANONICAL_DIGEST_NAMES
438+
439+
440+ def get_hash_func_info (name ):
441+ info = _HASHINFO_DATABASE [name ]
442+ assert isinstance (info , _HashInfo ), info
443+ return info .func
444+
353445
354446# Mapping from canonical hash names to their explicit HACL* HMAC constructor.
355447# There is currently no OpenSSL one-shot named function and there will likely
0 commit comments