4

Is there anything similar to https://developer.mozilla.org/en/javascript_crypto for IE or webkit browsers?

I just need to generate cryptographically secure pseudo-random numbers, but I don't want to have to include a large 3rd party js library.

Kyle
  • 21,377
  • 37
  • 113
  • 200

4 Answers4

1

you should read into a buffer instead a directly into a string, as the above code will be UTF-8 encoded (why you have so many \0xFFFD bytes)

lorrat
  • 11
  • 1
0

This is a fairly small SHA1 module that could be seeded with Date.now + some salt n pepper to provide a fairly random string. Strip out the alphas and keep calling until you have enough numbers for your needs.

http://www.movable-type.co.uk/scripts/sha1.html

Definitely hacky - so perhaps an ajax call to a webservice would suffice?

Marcus Pope
  • 2,293
  • 20
  • 25
0

I second the webservice idea and offer this suggestion: random.org, a truly random numbers generator.

slacktracer
  • 6,262
  • 6
  • 28
  • 33
0

I noticed "crypto" in the Firefox 3 global scope, then found this reference.

Unfortunately, when I try crypto.random(8) in the JavaScript Error Console, it throws a NotImplemented error. I'd like to see this function made standard.

On node.js running on osx/linux I recommend this:

node> var urandom = fs.openSync('/dev/urandom', 'r');

node> fs.readSync(urandom, 8);

[ '.\ufffd\u000f\ufffdK!L\ufffd', 8 ]

I believe a synchronous read is ok, because /dev/urandom will always be non-blocking and doesn't rely on disk IO.

mbinette
  • 5,094
  • 3
  • 24
  • 32
  • 2
    If you're working with Node.js, look at the `crypto.randomBytes` function, which provides a buffer of cryptographically secure pseudo-random bytes. For a random integer, try `crypto.randomBytes(32).readInt32BE(0)` – skeggse Jan 01 '12 at 03:48