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.

34 lines
899 B

4 years ago
  1. 'use strict';
  2. /**
  3. * Masks a buffer using the given mask.
  4. *
  5. * @param {Buffer} source The buffer to mask
  6. * @param {Buffer} mask The mask to use
  7. * @param {Buffer} output The buffer where to store the result
  8. * @param {Number} offset The offset at which to start writing
  9. * @param {Number} length The number of bytes to mask.
  10. * @public
  11. */
  12. const mask = (source, mask, output, offset, length) => {
  13. for (var i = 0; i < length; i++) {
  14. output[offset + i] = source[i] ^ mask[i & 3];
  15. }
  16. };
  17. /**
  18. * Unmasks a buffer using the given mask.
  19. *
  20. * @param {Buffer} buffer The buffer to unmask
  21. * @param {Buffer} mask The mask to use
  22. * @public
  23. */
  24. const unmask = (buffer, mask) => {
  25. // Required until https://github.com/nodejs/node/issues/9006 is resolved.
  26. const length = buffer.length;
  27. for (var i = 0; i < length; i++) {
  28. buffer[i] ^= mask[i & 3];
  29. }
  30. };
  31. module.exports = { mask, unmask };