def serialize_encryption_context(encryption_context):
    """Serializes the contents of a dictionary into a byte string.

    :param dict encryption_context: Dictionary of encrytion context keys/values.
    :returns: Serialized encryption context
    :rtype: bytes
    """
    if not encryption_context:
        return bytes()

    serialized_context = bytearray()
    dict_size = len(encryption_context)

    if dict_size > aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE:
        raise SerializationError("The encryption context contains too many elements.")

    serialized_context.extend(struct.pack(">H", dict_size))

    # Encode strings first to catch bad values.
    encryption_context_list = []
    for key, value in encryption_context.items():
        try:
            if isinstance(key, bytes):
                key = codecs.decode(key)
            if isinstance(value, bytes):
                value = codecs.decode(value)
            encryption_context_list.append(
                (aws_encryption_sdk.internal.str_ops.to_bytes(key), aws_encryption_sdk.internal.str_ops.to_bytes(value))
            )
        except Exception:
            raise SerializationError(
                "Cannot encode dictionary key or value using {}.".format(aws_encryption_sdk.internal.defaults.ENCODING)
            )

    for key, value in sorted(encryption_context_list, key=lambda x: x[0]):
        serialized_context.extend(
            struct.pack(
                ">H{key_size}sH{value_size}s".format(key_size=len(key), value_size=len(value)),
                len(key),
                key,
                len(value),
                value,
            )
        )
        if len(serialized_context) > aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE:
            raise SerializationError("The serialized context is too large.")
    return bytes(serialized_context)
def serialize_encryption_context ( encryption_context ) : if not encryption_context : return bytes ( ) serialized_context = bytearray ( ) dict_size = len ( encryption_context ) if dict_size > aws_encryption_sdk . internal . defaults . MAX_BYTE_ARRAY_SIZE : raise SerializationError ( "The encryption context contains too many elements." ) serialized_context . extend ( struct . pack ( ">H" , dict_size ) ) encryption_context_list = [ ] for key , value in encryption_context . items ( ) : try : if isinstance ( key , bytes ) : key = codecs . decode ( key ) if isinstance ( value , bytes ) : value = codecs . decode ( value ) encryption_context_list . append ( ( aws_encryption_sdk . internal . str_ops . to_bytes ( key ) , aws_encryption_sdk . internal . str_ops . to_bytes ( value ) ) ) except Exception : raise SerializationError ( "Cannot encode dictionary key or value using {}." . format ( aws_encryption_sdk . internal . defaults . ENCODING ) ) for key , value in sorted ( encryption_context_list , key = lambda x : x [ 0 ] ) : serialized_context . extend ( struct . pack ( ">H{key_size}sH{value_size}s" . format ( key_size = len ( key ) , value_size = len ( value ) ) , len ( key ) , key , len ( value ) , value , ) ) if len ( serialized_context ) > aws_encryption_sdk . internal . defaults . MAX_BYTE_ARRAY_SIZE : raise SerializationError ( "The serialized context is too large." ) return bytes ( serialized_context )
Serializes the contents of a dictionary into a byte string.

    :param dict encryption_context: Dictionary of encrytion context keys/values.
    :returns: Serialized encryption context
    :rtype: bytes