#NEWLINE# def assert_fingerprint( cert , fingerprint ) : #NEWLINE# #INDENT# '\n    Checks if given fingerprint matches the supplied certificate.\n\n    :param cert:\n        Certificate as bytes object.\n    :param fingerprint:\n        Fingerprint as string of hexdigits, can be interspersed by colons.\n    ' #NEWLINE# hashfunc_map = { #NEWLINE# #INDENT# 16 : md5 , #NEWLINE# 20 : sha1 , #NEWLINE# #UNINDENT# } #NEWLINE# fingerprint = fingerprint . replace ( ':' , '' ) . lower ( ) #NEWLINE# ( digest_length , rest ) = divmod ( len ( fingerprint ) , 2 ) #NEWLINE# if ( rest or ( digest_length not in hashfunc_map ) ) : #NEWLINE# #INDENT# raise SSLError ( 'Fingerprint is of invalid length.' ) #NEWLINE# #UNINDENT# fingerprint_bytes = unhexlify ( fingerprint . encode ( ) ) #NEWLINE# hashfunc = hashfunc_map [ digest_length ] #NEWLINE# cert_digest = hashfunc ( cert ) . digest ( ) #NEWLINE# if ( not ( cert_digest == fingerprint_bytes ) ) : #NEWLINE# #INDENT# raise SSLError ( 'Fingerprints did not match. Expected "{0}", got "{1}".' . format ( hexlify ( fingerprint_bytes ) , hexlify ( cert_digest ) ) )