Math.random() is used for cryptography, but it is not a suitable source of randomness:
|
cryptographic_key = cryptographic_key.concat(String.fromCharCode(Math.floor(Math.random()*26) + 65)); |
These two articles illustrate the problem quite well:
TIFU by using Math.random() by Mike Malone, Betable CTO, 2015-11-19
Many random number generators in use today are not very good. — Donald Knuth
The current algorithm, which appears to have been passed down from one programmer to another, is comparatively unsatisfactory (and arguably completely broken) due to subtle, non-intuitive degenerate behavior that is likely to be encountered under realistic circumstances.
and
There’s Math.random(), and then there’s Math.random() by Yang Guo, v8 Engineer at Google, 2015-12-17
For use cases such as hashing, signature generation, and encryption/decryption, ordinary PRNGs are unsuitable. The Web Cryptography API introduces window.crypto.getRandomValues, a method that returns cryptographically secure random values, at a performance cost.
I thus propose to use the Web Cryptography API instead.