23 points · by SudoSH · 2y ago · 25 comments
The code here doesn't even work, partly because it calls euclideanAlgorithm when the author means extendedEuclidean, but also because Javascript only has 53-bit integers and the modular-multiplication code uses [edit] 60-bit intermediates when N is 30 bits long.
> It's better to use Python, which at least has bignums.
I agree with the sibling commenter that this seems to be automatically generated. The disclaimer is weird, you don't just use a random rsa implementation found in a blog post "with careful consideration and expert guidance". The article's structure is weird, it's just a bullet list of steps and then code dump. It gets many details wrong (code won't run, explanation seems to be mixing up euler & carmichael totients, ...). etc.
It is somewhat scary that this got to the front page at all.
Many red flags here:
- Look at the text below the headers "Explaining RSA Cryptography" and "Explaining RSA Cryptography with JavaScript". Both paragraphs start with the exact same text. I believe there are far more chances that this was generated by a machine rather than a human.
- Look at the math! There is a code block where it says "scssCopy" -- the "Copy" is generally garbage from machine generated text+code snippets.
- The code has a missing function, `lcm`. Author has forgotten to include it, which can be a human or machine error.
This kind of episode makes me wonder if I should continue posting, suggest that people filter everything through an LLM, or just resign to the botspamcallypse.
The OP's code needed a little help; there were some missing functions, misnamed calls, and he didn't use js BigInts (which are built-in to the browser, and you can use them as literals by appending 'n' to a number.)
See (and run) the fixed up code here: https://simpatico.io/crypto.md#rsafromscratch
EDIT: for my own crypto needs in the browser I use `subtle.crypto` with ECDH - https://en.wikipedia.org/wiki/Elliptic-curve_Diffie%E2%80%93...
I prefer to zoom out and realize that everyone wants all tutorials to be written in their favorite language, and realizing this makes me a little less attached to mine, and more accepting of others, even if they are not for me.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
> The operations supported on BigInt values are not constant-time and are thus open to timing attacks. JavaScript BigInts therefore could be dangerous for use in cryptography without mitigating factors.
Without checking the source code to understand their exact implementation, it seems questionable to implement cryptography algorithms for serious use in JavaScript.
Never use textbook RSA.
Source: every course on cryptography.