You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

19 lines
376 B

  1. import itertools
  2. __all__ = ["apply_mask"]
  3. def apply_mask(data: bytes, mask: bytes) -> bytes:
  4. """
  5. Apply masking to the data of a WebSocket message.
  6. :param data: Data to mask
  7. :param mask: 4-bytes mask
  8. """
  9. if len(mask) != 4:
  10. raise ValueError("mask must contain 4 bytes")
  11. return bytes(b ^ m for b, m in zip(data, itertools.cycle(mask)))