<s> from . bindings import _FFI , _C <EOL> from binascii import hexlify <EOL> import pytest <EOL> def _check ( return_val ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( return_val , int ) and return_val == <NUM_LIT:1> : <EOL> return <EOL> if isinstance ( return_val , bool ) and return_val == True : <EOL> return <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def secure_compare ( a1 , a2 ) : <EOL> """<STR_LIT>""" <EOL> _check ( type ( a1 ) == type ( a2 ) ) <EOL> if len ( a1 ) != len ( a2 ) : <EOL> return False <EOL> x = _C . CRYPTO_memcmp ( a1 , a2 , len ( a1 ) ) <EOL> if int ( x ) == <NUM_LIT:0> : <EOL> return True <EOL> return False <EOL> class Hmac ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , key ) : <EOL> self . mac_ctx = None <EOL> md = _C . EVP_get_digestbyname ( name ) <EOL> if md == _FFI . NULL : <EOL> raise Exception ( "<STR_LIT>" , name ) <EOL> self . outsize = _C . EVP_MD_size ( md ) <EOL> self . mac_ctx = _FFI . new ( "<STR_LIT>" ) <EOL> _C . HMAC_CTX_init ( self . mac_ctx ) <EOL> _check ( _C . HMAC_Init_ex ( self . mac_ctx , key , len ( key ) , md , _FFI . NULL ) ) <EOL> self . active = True <EOL> def update ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if not self . active : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> _check ( _C . HMAC_Update ( self . mac_ctx , data , len ( data ) ) ) <EOL> def digest ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . active : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . active = False <EOL> out_md = _FFI . new ( "<STR_LIT>" , self . outsize ) <EOL> out_len = _FFI . new ( "<STR_LIT>" ) <EOL> _check ( _C . HMAC_Final ( self . mac_ctx , out_md , out_len ) ) <EOL> if int ( out_len [ <NUM_LIT:0> ] ) != self . outsize : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return bytes ( _FFI . buffer ( out_md ) [ : ] ) <EOL> def __del__ ( self ) : <EOL> if self . mac_ctx != None : <EOL> _C . HMAC_CTX_cleanup ( self . mac_ctx ) <EOL> def test_init ( ) : <EOL> h = Hmac ( b"<STR_LIT>" , b"<STR_LIT>" ) <EOL> h . update ( b"<STR_LIT:hello>" ) <EOL> d = h . digest ( ) <EOL> assert d <EOL> assert len ( d ) == <NUM_LIT> / <NUM_LIT:8> <EOL> def test_vectors ( ) : <EOL> """<STR_LIT>""" <EOL> h = Hmac ( b"<STR_LIT>" , b"<STR_LIT>" ) <EOL> assert <NUM_LIT> / <NUM_LIT:8> == h . outsize <EOL> h . update ( b"<STR_LIT>" ) <EOL> h . update ( b"<STR_LIT>" ) <EOL> d = h . digest ( ) <EOL> with pytest . raises ( Exception ) as excinfo : <EOL> h . update ( b"<STR_LIT>" ) <EOL> assert '<STR_LIT>' in str ( excinfo . value ) <EOL> with pytest . raises ( Exception ) as excinfo : <EOL> h . digest ( ) <EOL> assert '<STR_LIT>' in str ( excinfo . value ) <EOL> with pytest . raises ( Exception ) as excinfo : <EOL> h = Hmac ( b"<STR_LIT>" , b"<STR_LIT>" ) <EOL> assert '<STR_LIT>' in str ( excinfo . value ) <EOL> ans1 = hexlify ( d ) <EOL> ans2 = b"<STR_LIT>" <EOL> assert len ( ans1 ) == len ( ans2 ) <EOL> assert ans1 == ans2 <EOL> def test_cmp ( ) : <EOL> assert secure_compare ( b"<STR_LIT>" , b"<STR_LIT>" ) <EOL> assert not secure_compare ( b"<STR_LIT>" , b"<STR_LIT>" ) <EOL> assert not secure_compare ( b"<STR_LIT>" , b"<STR_LIT>" ) <EOL> with pytest . raises ( Exception ) as excinfo : <EOL> assert not secure_compare ( b"<STR_LIT>" , <NUM_LIT:2> ) <EOL> assert '<STR_LIT>' in str ( excinfo . value ) </s>
