code: def _verify_public_keys(self, nonce, server_key, public_key):
          
          if nonce is not None:
              hash_input = b"CredSSP Server-To-Client Binding Hash\x00" + nonce \
                           + public_key
              actual = hashlib.sha256(hash_input).digest()
              expected = server_key
          else:
              first_byte = struct.unpack("B", server_key[0:1])[0]
              actual_first_byte = struct.pack("B", first_byte - 1)
  
              actual = actual_first_byte + server_key[1:]
              expected = public_key
  
          if actual != expected:
              raise AuthenticationException("Could not verify key sent from the "
                                            "server, potential man in the "
                                            "middle attack")
docs: [MS-CSSP] 3.1.5 Processing Events and Sequencing Rules - Step 4
          https://msdn.microsoft.com/en-us/library/cc226791.aspx
  
          The rules vary depending on the server version
  
          For version 2 to 4:
          After the server received the public key in Step 3 it verifies the key
          with what was in the handshake. After the verification it then adds 1
          to the first byte representing the public key and encrypts the bytes
          result by using the authentication protocol's encryption services.
  
          This method does the opposite where it will decrypt the public key
          returned from the server and subtract the first byte by 1 to compare
          with the public key we sent originally.
  
          For versions 5 to 6:
          A hash is calculated with the magic string value, the nonce that was
          sent to the server and the public key that was used. This is verified
          against the returned server public key.
  
          :param nonce: If version 5+, the nonce used in the hash calculations
          :param server_key: The unwrapped value returned in the
              TSRequest['pubKeyAuth'] field.
          :param public_key: The actual public key of the server