docs: def _encrypt_entity(entity, key_encryption_key, encryption_resolver):
      '''
      Encrypts the given entity using AES256 in CBC mode with 128 bit padding.
      Will generate a content-encryption-key (cek) to encrypt the properties either
      stored in an EntityProperty with the 'encrypt' flag set or those
      specified by the encryption resolver. This cek is then wrapped using the 
      provided key_encryption_key (kek). Only strings may be encrypted and the
      result is stored as binary on the service. 
  
      :param entity:
          The entity to insert. Could be a dict or an entity object.
      :param object key_encryption_key:
          The user-provided key-encryption-key. Must implement the following methods:
          wrap_key(key)--wraps the specified key using an algorithm of the user's choice.
          get_key_wrap_algorithm()--returns the algorithm used to wrap the specified symmetric key.
          get_kid()--returns a string key id for this key-encryption-key.
      :param function(partition_key, row_key, property_name) encryption_resolver:
          A function that takes in an entities partition key, row key, and property name and returns 
          a boolean that indicates whether that property should be encrypted.
      :return: An entity with both the appropriate properties encrypted and the 
          encryption data.
      :rtype: object
      '''
  
      _validate_not_none('entity', entity)
      _validate_not_none('key_encryption_key', key_encryption_key)
      _validate_key_encryption_key_wrap(key_encryption_key)
  
      # AES256 uses 256 bit (32 byte) keys and always with 16 byte blocks
      content_encryption_key = os.urandom(32)
      entity_initialization_vector = os.urandom(16)
  
      encrypted_properties = []
      encrypted_entity = Entity()
      for key, value in entity.items():
          # If the property resolver says it should be encrypted
          # or it is an EntityProperty with the 'encrypt' property set.
          if (isinstance(value, EntityProperty) and value.encrypt) or \
                  (encryption_resolver is not None \
                           and encryption_resolver(entity['PartitionKey'], entity['RowKey'], key)):
  
              # Only strings can be encrypted and None is not an instance of str.
              if isinstance(value, EntityProperty):
                  if value.type == EdmType.STRING:
                      value = value.value
                  else:
                      raise ValueError(_ERROR_UNSUPPORTED_TYPE_FOR_ENCRYPTION)
              if not isinstance(value, str):
                  raise ValueError(_ERROR_UNSUPPORTED_TYPE_FOR_ENCRYPTION)
  
                  # Value is now confirmed to hold a valid string value to be encrypted
              # and should be added to the list of encrypted properties.
              encrypted_properties.append(key)
  
              propertyIV = _generate_property_iv(entity_initialization_vector,
                                                 entity['PartitionKey'], entity['RowKey'],
                                                 key, False)
  
              # Encode the strings for encryption.
              value = value.encode('utf-8')
  
              cipher = _generate_AES_CBC_cipher(content_encryption_key, propertyIV)
  
              # PKCS7 with 16 byte blocks ensures compatibility with AES.
              padder = PKCS7(128).padder()
              padded_data = padder.update(value) + padder.finalize()
  
              # Encrypt the data.
              encryptor = cipher.encryptor()
              encrypted_data = encryptor.update(padded_data) + encryptor.finalize()
  
              # Set the new value of this key to be a binary EntityProperty for proper serialization.
              value = EntityProperty(EdmType.BINARY, encrypted_data)
  
          encrypted_entity[key] = value
  
      encrypted_properties = dumps(encrypted_properties)
  
      # Generate the metadata iv.
      metadataIV = _generate_property_iv(entity_initialization_vector,
                                         entity['PartitionKey'], entity['RowKey'],
                                         '_ClientEncryptionMetadata2', False)
  
      encrypted_properties = encrypted_properties.encode('utf-8')
  
      cipher = _generate_AES_CBC_cipher(content_encryption_key, metadataIV)
  
      padder = PKCS7(128).padder()
      padded_data = padder.update(encrypted_properties) + padder.finalize()
  
      encryptor = cipher.encryptor()
      encrypted_data = encryptor.update(padded_data) + encryptor.finalize()
  
      encrypted_entity['_ClientEncryptionMetadata2'] = EntityProperty(EdmType.BINARY, encrypted_data)
  
      encryption_data = _generate_encryption_data_dict(key_encryption_key, content_encryption_key,
                                                       entity_initialization_vector)
  
      encrypted_entity['_ClientEncryptionMetadata1'] = dumps(encryption_data)
      return encrypted_entity