'Websocket masking function.
`mask` is a `bytes` object of length 4; `data` is a `bytes` object of any length.
Returns a `bytes` object of the same length as `data` with the mask applied
as specified in section 5.3 of RFC 6455.
This pure-python implementation may be replaced by an optimized version when available.'

@classmethod
def mask_or_unmask(cls, mask, data):

    mask = array.array('B', mask)
    unmasked = array.array('B', data)
    for i in range(len(data)):
        unmasked[i] = (unmasked[i] ^ mask[(i % 4)])
    if hasattr(unmasked, 'tobytes'):
        return unmasked.tobytes()
    else:
        return unmasked.tostring()
