Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math.random on V8 not random enough #57

Closed
pmontrasio opened this issue Nov 21, 2015 · 5 comments
Closed

Math.random on V8 not random enough #57

pmontrasio opened this issue Nov 21, 2015 · 5 comments

Comments

@pmontrasio
Copy link

I just read TIFU by using Math.random() (discussion on HN at https://news.ycombinator.com/item?id=10598065). TL;DR this is what Math.random yields on Firefox

firefox random 500x140

and this is what it yields on V8

v8 random 500x140

The author suggests using crypto.getRandomValues in the browser. That populates a typed array with random values. Example:

> var random_int32 = new Uint32Array(5);
> crypto.getRandomValues(random_int32)
[3893288831, 502897280, 4154045839, 415033321, 3522955257]

https://developer.mozilla.org/en-US/docs/Web/API/RandomSource/getRandomValues

@jalada
Copy link

jalada commented Nov 30, 2015

As far as I know, JSEncrypt already uses crypto.getRandomValues when it can.

JSEncrypt uses JSBN behind the scenes, and that has a PRNG that first tries getRandomValues. If that's not available, it uses mouse entropy. If it runs out of mouse entropy then yes, it falls back to Math.random.

https://github.com/travist/jsencrypt/blob/master/lib/jsbn/rng.js

@zoloft
Copy link
Collaborator

zoloft commented Dec 1, 2015

@jalada exactly, thank you :) (closing now)

@zoloft zoloft closed this as completed Dec 1, 2015
@pmontrasio
Copy link
Author

I'm afraid I didn't check the code well enough. Sorry for wasting your time.

@jalada
Copy link

jalada commented Dec 2, 2015

Not a problem @pmontrasio, I actually came here to double check JSEncrypt because of the same blog post.

@zoloft
Copy link
Collaborator

zoloft commented Dec 2, 2015

@pmontrasio & @jalada P.S. kudos to @ctso for his work in #6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants