<s> from __future__ import absolute_import , division , print_function <EOL> from cryptography import utils <EOL> from cryptography . exceptions import UnsupportedAlgorithm , _Reasons <EOL> from cryptography . hazmat . primitives import hashes <EOL> @ utils . register_interface ( hashes . HashContext ) <EOL> class _HashContext ( object ) : <EOL> def __init__ ( self , backend , algorithm , ctx = None ) : <EOL> self . _algorithm = algorithm <EOL> self . _backend = backend <EOL> if ctx is None : <EOL> ctx = self . _backend . _lib . Cryptography_EVP_MD_CTX_new ( ) <EOL> ctx = self . _backend . _ffi . gc ( <EOL> ctx , self . _backend . _lib . Cryptography_EVP_MD_CTX_free <EOL> ) <EOL> evp_md = self . _backend . _lib . EVP_get_digestbyname ( <EOL> algorithm . name . encode ( "<STR_LIT:ascii>" ) ) <EOL> if evp_md == self . _backend . _ffi . NULL : <EOL> raise UnsupportedAlgorithm ( <EOL> "<STR_LIT>" . format ( <EOL> algorithm . name ) , <EOL> _Reasons . UNSUPPORTED_HASH <EOL> ) <EOL> res = self . _backend . _lib . EVP_DigestInit_ex ( ctx , evp_md , <EOL> self . _backend . _ffi . NULL ) <EOL> self . _backend . openssl_assert ( res != <NUM_LIT:0> ) <EOL> self . _ctx = ctx <EOL> algorithm = utils . read_only_property ( "<STR_LIT>" ) <EOL> def copy ( self ) : <EOL> copied_ctx = self . _backend . _lib . Cryptography_EVP_MD_CTX_new ( ) <EOL> copied_ctx = self . _backend . _ffi . gc ( <EOL> copied_ctx , self . _backend . _lib . Cryptography_EVP_MD_CTX_free <EOL> ) <EOL> res = self . _backend . _lib . EVP_MD_CTX_copy_ex ( copied_ctx , self . _ctx ) <EOL> self . _backend . openssl_assert ( res != <NUM_LIT:0> ) <EOL> return _HashContext ( self . _backend , self . algorithm , ctx = copied_ctx ) <EOL> def update ( self , data ) : <EOL> res = self . _backend . _lib . EVP_DigestUpdate ( self . _ctx , data , len ( data ) ) <EOL> self . _backend . openssl_assert ( res != <NUM_LIT:0> ) <EOL> def finalize ( self ) : <EOL> buf = self . _backend . _ffi . new ( "<STR_LIT>" , <EOL> self . _backend . _lib . EVP_MAX_MD_SIZE ) <EOL> outlen = self . _backend . _ffi . new ( "<STR_LIT>" ) <EOL> res = self . _backend . _lib . EVP_DigestFinal_ex ( self . _ctx , buf , outlen ) <EOL> self . _backend . openssl_assert ( res != <NUM_LIT:0> ) <EOL> self . _backend . openssl_assert ( outlen [ <NUM_LIT:0> ] == self . algorithm . digest_size ) <EOL> return self . _backend . _ffi . buffer ( buf ) [ : outlen [ <NUM_LIT:0> ] ] </s>
