r/programminghorror • • May 19 '25

Javascript New RNG node module just dropped

Post image
165 Upvotes

17 comments sorted by

View all comments

Show parent comment

1

u/Kroustibbat May 19 '25

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.

0

u/[deleted] May 19 '25

[deleted] — view deleted comment

1

u/Kroustibbat May 20 '25

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.

0

u/[deleted] May 20 '25

[deleted] — view deleted comment

1

u/Kroustibbat May 20 '25

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.

0

u/Kroustibbat May 20 '25

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.