Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing some software that requires the use of a CSPRNG with javascript for use in the browser and I'm wondering what libraries are available. Crypto-js doesn't seem to have a CSPRNG I can directly call (unless I missed it?). I've looked through the Stanford crypto js library, but I'm uncertain how much analysis this code has gotten.

They appear to be implementing Schneier's Fortuna:

I've also seen

My question is there a well vetted crypto js library that has a well designed CSPRNG that generates values independently of some browser support? Or with a standard all modern browsers implement? And where is the entropy source? I'm used to crypto with languages like java and C++, not this fragmented world.

share|improve this question

closed as off-topic by Maarten Bodewes, DrLecter, otus, poncho, Gilles Jul 25 '14 at 20:57

  • This question does not appear to be about cryptography within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

Similar to… – archie Nov 2 '13 at 1:07
Not so much in my opinion, I'm asking a broader question than a particular implementation. I'm also concerned about javascript entropy sources. – Charles Hoskinson Nov 2 '13 at 1:39
Thanks e-sushi for the edits – Charles Hoskinson Nov 2 '13 at 7:10
This question appears to be off-topic because it is about implementations for a specific runtime. – Maarten Bodewes Jul 25 '14 at 14:26
Your question is not clear: you say you want a CSPRNG, and then you talk about an entropy source. Do you need a CSRNG (unpredictable, i.e. at least seeded with a sufficient entropy source), or a CSPRNG (i.e. deterministic, but predictable only by knowing the secret seed)? – Gilles Jul 25 '14 at 20:57

I would try to avoid doing any cryptography using javascript… but that's just my personal opinion.

Anyway, if you really want to walk the Javascript path, you could skip the libraries and take a look at the window.crypto.getRandomValues() function, as long as you can live with the fact that it's an experimental API and not yet supported in all browsers… it should work on current versions of Firefox and Chrome though.

And for Opera, there's Math.Random() which has been implemented as a CSRNG. But you should be aware that Math.Random will only be a CSRNG in Opera (only in Opera, not any other browser!!!), and that the function will only return 53 bits of entropy on each call.

Besides that, I don't know of any further CSRNGs being available in Javascript, or in Javascript libraries.


Some stuff that might help you find solutions to your quest:

share|improve this answer
""Am I really dealing with a random number generator, or with something provided by an attacker?"" You resolve this attack via https and can even have the server sign the code. "First up: I would try to avoid doing any cryptography using javascript… " As much as we'd like to return to the C days, JS is king now and will be for some time. I have to work with the cards I've been dealt. – Charles Hoskinson Nov 2 '13 at 6:01
@e-sushi: "Am I really dealing with a random number generator, or with something provided by an attacker?" - what's your line of reasoning for doubting this assertion? Say for example, a sound javascript implementation of CTR_DRBG as defined in SP800-90A, delivered via TLS, using decent quality entropy (or even something like the service offered here). Given the NSA's recent indiscretions (amongst others), I think JS crypto is something worth considering. – hunter Nov 2 '13 at 6:04
@CharlesHoskinson Well, as I noted, there's window.crypto.getRandomValues() and Opera's Math.Random(). With some browser detection code you can do a lot. Problem will be to get IE into the crypto bucket. – e-sushi Nov 2 '13 at 6:05
@CharlesHoskinson - C'mon - I'm a fan of JS, really, but 'King'? In my opinion, a language that can't handle 64-bit integers natively in crypto is going to struggle. I also think that weakly-typed languages like JS are asking for trouble. – hunter Nov 2 '13 at 6:11
@e-sushi - the whole chicken/egg problem with javascript is real - but still, the world could be forgiven for having trust-issues with TLS/the-internet/NSA. This question is strongly related to this one. It's a contentious issue - I err very much on the side of experimentation and creativity - not qualities that usually lend themselves well to crypto. – hunter Nov 2 '13 at 6:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.