Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.
This repository was archived by the owner on Dec 11, 2025. It is now read-only.

Math.random() is not cryptographically secure #12

@hacklschorsch

Description

@hacklschorsch

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions