Live data from Hacker News

Implementing RSA from Scratch in JavaScript

coderoasis.com

11–20 of 27 posts

Re: Implementing RSA from Scratch in JavaScript

#11

This isn't a particularly good way to learn RSA, and Javascript isn't a good environment to teach it. It's better to use Python, which at least has bignums. And obviously, seeing a wrong implementation of RSA isn't at all essential for implementing secure systems, contrary to the article's claims. Few people will actually need to implement RSA, and for those people, all those details that the article gets wrong reall…

I see the peer comments regarding big ints, but the OP is still right in that the presented code doesn't work, for more than one reason.

Re: Implementing RSA from Scratch in JavaScript

#12
post #9

This isn't a particularly good way to learn RSA, and Javascript isn't a good environment to teach it. It's better to use Python, which at least has bignums. And obviously, seeing a wrong implementation of RSA isn't at all essential for implementing secure systems, contrary to the article's claims. Few people will actually need to implement RSA, and for those people, all those details that the article gets wrong reall…

JS not only has BigInt, but it also has TypedArrays too. It even used to have SIMD support before they dropped it for WASM.

Not sure why all these responses point out that BigInt is supported in Javascript these days. The fact remains that the code in the article doesn't use BigInt and 'numbers' aren't automatically promoted.

Re: Implementing RSA from Scratch in JavaScript

#13
post #11

This isn't a particularly good way to learn RSA, and Javascript isn't a good environment to teach it. It's better to use Python, which at least has bignums. And obviously, seeing a wrong implementation of RSA isn't at all essential for implementing secure systems, contrary to the article's claims. Few people will actually need to implement RSA, and for those people, all those details that the article gets wrong reall…

I see the peer comments regarding big ints, but the OP is still right in that the presented code doesn't work, for more than one reason.

Besides, the code does not use BigInts at all, so the fact that JavaScript has them does not help much.

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.

Re: Implementing RSA from Scratch in JavaScript

#14

This isn't a particularly good way to learn RSA, and Javascript isn't a good environment to teach it. It's better to use Python, which at least has bignums. And obviously, seeing a wrong implementation of RSA isn't at all essential for implementing secure systems, contrary to the article's claims. Few people will actually need to implement RSA, and for those people, all those details that the article gets wrong reall…

Yeah, the implementation here obviously has problems, but as a learning exercise, I do still think it's worth implementing RSA (or anything else complex) in whatever language you're comfortable with. Personally, I implemented RSA in JavaScript back in 1998 as an exercise. I had to roll my own basic BigNum implementation and it didn't perform well enough to handle non-trivial key or message sizes, but I still think it was valuable for my education (and fun).

Re: Implementing RSA from Scratch in JavaScript

#15
post #6

This isn't a particularly good way to learn RSA, and Javascript isn't a good environment to teach it. It's better to use Python, which at least has bignums. And obviously, seeing a wrong implementation of RSA isn't at all essential for implementing secure systems, contrary to the article's claims. Few people will actually need to implement RSA, and for those people, all those details that the article gets wrong reall…

Javascript has native BigInts, and there is already an RSA implementation using them: https://github.com/i404788/tiny-rsa

That's funny --

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.

Re: Implementing RSA from Scratch in JavaScript

#16

This isn't a particularly good way to learn RSA, and Javascript isn't a good environment to teach it. It's better to use Python, which at least has bignums. And obviously, seeing a wrong implementation of RSA isn't at all essential for implementing secure systems, contrary to the article's claims. Few people will actually need to implement RSA, and for those people, all those details that the article gets wrong reall…

Hi there! I think JavaScript is a great way to teach, since it's such an accessible language. It also allows you to ship your code to run easily for your users.

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...

Re: Implementing RSA from Scratch in JavaScript

#18
post #14

This isn't a particularly good way to learn RSA, and Javascript isn't a good environment to teach it. It's better to use Python, which at least has bignums. And obviously, seeing a wrong implementation of RSA isn't at all essential for implementing secure systems, contrary to the article's claims. Few people will actually need to implement RSA, and for those people, all those details that the article gets wrong reall…

Yeah, the implementation here obviously has problems, but as a learning exercise, I do still think it's worth implementing RSA (or anything else complex) in whatever language you're comfortable with. Personally, I implemented RSA in JavaScript back in 1998 as an exercise. I had to roll my own basic BigNum implementation and it didn't perform well enough to handle non-trivial key or message sizes, but I still think it…

If it was "valuable for my education" what did you learn? Your take away seems to have been that your toy RSA implementation "didn't perform well enough" which isn't a lesson.

Re: Implementing RSA from Scratch in JavaScript

#19
post #9

Earlier quoted context omitted.

JS not only has BigInt, but it also has TypedArrays too. It even used to have SIMD support before they dropped it for WASM.

Not sure why all these responses point out that BigInt is supported in Javascript these days. The fact remains that the code in the article doesn't use BigInt and 'numbers' aren't automatically promoted.

I was primarily addressing

> It's better to use Python, which at least has bignums.

Re: Implementing RSA from Scratch in JavaScript

#20
post #15
post #6

Earlier quoted context omitted.

Javascript has native BigInts, and there is already an RSA implementation using them: https://github.com/i404788/tiny-rsa

That's funny -- 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…

An implementation using TypedArray should be in constant time.
Post reply on HN