Skip to content

getRandomMask uses Math.random #616

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

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions