Skip to content

getRandomMask uses Math.random #616

@coolaj86

Description

@coolaj86

https://github.com/websockets/ws/blob/master/lib/Sender.js#L309

function getRandomMask() {
  return new Buffer([
    ~~(Math.random() * 255),
    ~~(Math.random() * 255),
    ~~(Math.random() * 255),
    ~~(Math.random() * 255)
  ]);
}

should be replaced with

function getRandomMask() {
  return require('crypto').randomBytes(4);
}

If the desire is to be more performant this can be accomplished by pooling random data like this:

var rlen = 256;
var rbytes = crypto.randomBytes(rlen);
var rcounter = 4;

function getRandomMask() {
  var result;

  if (rcounter >= rbytes.length) {
    rbytes = crypto.randomBytes(rlen);
    rcounter = 4;
  }

  result = rbytes.slice(rcounter - 4, rcountner);
  rcounter += 4;

  return result;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions