"""Utilities used by the message signing serializer."""importsysfromcontextlibimportcontextmanagerimportcryptography.exceptionsfromcryptography.hazmat.primitivesimporthashesfromcelery.exceptionsimportSecurityError,reraise__all__=('get_digest_algorithm','reraise_errors',)
[文档]defget_digest_algorithm(digest='sha256'):"""Convert string to hash object of cryptography library."""assertdigestisnotNonereturngetattr(hashes,digest.upper())()
[文档]@contextmanagerdefreraise_errors(msg='{0!r}',errors=None):"""Context reraising crypto errors as :exc:`SecurityError`."""errors=(cryptography.exceptions,)iferrorsisNoneelseerrorstry:yieldexcepterrorsasexc:reraise(SecurityError,SecurityError(msg.format(exc)),sys.exc_info()[2])