Skip to content

Fast xoring in python #686

@socketpair

Description

@socketpair

instead of

def _websocket_mask_python(mask, data):
    return bytes(b ^ mask[i % 4] for i, b in enumerate(data))

please use this:

native_byteorder = sys.byteorder

def _websocket_mask_python(mask, data):
    assert len(mask) == 4
    datalen = len(data)
    if datalen == 0:
        return b''  #  everything work without this, but may be changed later in python.
    data = int.from_bytes(data, native_byteorder)
    mask = int.from_bytes(mask * (datalen // 4) + mask[: datalen % 4], native_byteorder)
    return (data ^ mask).to_bytes(datalen, native_byteorder)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions