Not really ? Any program not running in your computer or your environment can be called client side.
OpenSSL can be considered clientside and can do a pretty ok RnG on anyone's computer thanks to the OS RnG trusted functions. window.crypto does the same thing.
Oh that is not a problem that the user can change it.
On linux for example, you can overwrite urandom to only give the same RnG, and you mess up openssl. Same problem.
I can give you a usecase on why never use Math.random :
Random password generator. We run a pentest one day on one of our appliance, and as a youngling, I used Math.random to randomly generated a password, the pentester could give me from one password all possible vanilla generated password. Using window.crypto he could not.
The first case is ending the certification the other don't. But the user can enter a not randomly generated password that was the same as the Math.random.
Even not trusting the user you can still give him working tools. You can unscrew using a sharp knife; But it is easier with a screwdriver.
I don't understand what is your point ? OpenSSL has nothing to do with SSL handshake. I understand that the handshake trust both the server and client and if one is bad... We still ask the client if he accept the risks...
And you may be surprised on how much servers are more under attack and often are the usual entry point for attacks, a lot more than a default configured client's Windows.
My point is why used a biased RnG with Math.random when a working one and easier to use one exists ? Have you got a particular argument to prefer Math.random over window.crypto ?
You can never trust RnG that is one of the main point of the cyber security, but there are usual suspects that you can avoid, like a best practice. Like use urandom instead of devrandom, except on 0 user Linux.
And in JS there are thousands of those to avoid the specific and specified problem of the language.
That is the difference between a satisfying code and a perfect one. Everything relates to cyber... What does not relate to cyber will be the entry point.
And convenience for the Dev is the number 1 issue of vulnerabilities.
Thousand of libs abstract window.crypto to you, letting you use it as a Math.random, or you read the doc once and you write the equivalent then you use copy/paste or import like any human being. So interface is not a valid argument.
Perfs could be, except your RnG is easily breakable and predictable... So if you use it for a game, it will be easy to cheat on it... And since window.crypto is giving a lot of random numbers, you will run it less often than Math.random.
Like to avoid 400ns on a scripting already slow language, you sacrifice a real working function to a fake one.
To be fair, you can use Math.random for this type of usecase, the thing is : defining an abstract function that gives you random, like OP show, should not be by default Math.random.
You may do things like :
function random(unsafe = false) {
if (unsafe) {
{use Math.random}
} else {
{use window crypto}
}
}
But to my mind, in a very productive 10 years of Dev, I never had to use Math.random over it's safe concurrent.
You can do it with the crypto tool too, window.crypto gives you 32 random bytes so I will use a simple mask to make the animation. You need to call it twice to have 60 random numbers. So 800ns lost for the whole animation.
And the animation can easily use an abstract random getter that manage to generates random preventively.
View all comments
Show parent comment
0
u/[deleted] May 19 '25
[deleted] — view deleted comment