decorator raising skiptest if a hashing algorithm is not available the hashing algorithm could be missing or blocked by a strict crypto policy if openssl is true then the decorator checks that openssl provides the algorithm otherwise the check falls back to builtin implementations the usedforsecurity flag is passed to the constructor valueerror digital envelope routines evpdigestinitex disabled for fips valueerror unsupported hash type md4 decorator raising skiptest if a hashing algorithm is not available the hashing algorithm could be missing or blocked by a strict crypto policy if openssl is true then the decorator checks that openssl provides the algorithm otherwise the check falls back to built in implementations the usedforsecurity flag is passed to the constructor valueerror digital envelope routines evp_digestinit_ex disabled for fips valueerror unsupported hash type md4
import functools
import hashlib
import unittest

try:
    import _hashlib
except ImportError:
    _hashlib = None


def requires_hashdigest(digestname, openssl=None, usedforsecurity=True):
    
    def decorator(func_or_class):
        if isinstance(func_or_class, type):
            setUpClass = func_or_class.__dict__.get('setUpClass')
            if setUpClass is None:
                def setUpClass(cls):
                    super(func_or_class, cls).setUpClass()
                setUpClass.__qualname__ = func_or_class.__qualname__ + '.setUpClass'
                setUpClass.__module__ = func_or_class.__module__
            else:
                setUpClass = setUpClass.__func__
            setUpClass = classmethod(decorator(setUpClass))
            func_or_class.setUpClass = setUpClass
            return func_or_class

        @functools.wraps(func_or_class)
        def wrapper(*args, **kwargs):
            try:
                if openssl and _hashlib is not None:
                    _hashlib.new(digestname, usedforsecurity=usedforsecurity)
                else:
                    hashlib.new(digestname, usedforsecurity=usedforsecurity)
            except ValueError:
                raise unittest.SkipTest(
                    f"hash digest '{digestname}' is not available."
                )
            return func_or_class(*args, **kwargs)
        return wrapper
    return decorator