-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use WebCrypto API #143
Use WebCrypto API #143
Conversation
Not sure if it works on node <20 🥶 (because of the way crypto api is referenced, might need to change that) |
Btw, I'm already using this in production 😂 |
Will make one more commit. |
I'll have a closer look at the code soon, but before that we'll need to address the NodeJS 18 error (I'm okay with dropping support for Node 16 since it went out of maintenance half a year ago, but Node 18 will need to be supported for another year until it reaches its end of life. I've also tried running some simple performance tests with the following code (you have to remove the jest clock)
And currently this branch takes approximately double the time to execute a TOTP.generate compared to the main branch (I'm running this on node v21.7.2 when trying it out). I'll gladly help to unravel the performance issues in a few days! |
Hey, there, |
I can help you with trying to optimize it, but I'm out travling for another ~10 days, so it will take a little while before being able to do much ;) |
Much appreciated, I'll see what I can do meanwhile. |
@bellstrand I've got a question, so I had in my base32 to buffer function a piece of code which calculates a lookup object with charCode to Index for base32 alphabet, so in order to optimize (even a little) I did this: /**
* A precalculated mapping from base32 character codes to their corresponding index values for performance optimization.
* This mapping is used in the base32ToBuffer method to convert base32 encoded strings to their binary representation.
*/
private static readonly base32: { [key: string]: number } = { "50": 26, "51": 27, "52": 28, "53": 29, "54": 30, "55": 31, "65": 0, "66": 1, "67": 2, "68": 3, "69": 4, "70": 5, "71": 6, "72": 7, "73": 8, "74": 9, "75": 10, "76": 11, "77": 12, "78": 13, "79": 14, "80": 15, "81": 16, "82": 17, "83": 18, "84": 19, "85": 20, "86": 21, "87": 22, "88": 23, "89": 24, "90": 25 } does this look ok? |
Tested on first versions of node 16 and 18, it works! |
We can look at moving it to a different file, but might need to do something with the build script to bundle it in one file for that! |
Look at bun branch test speed lol test.lol.mp4 |
Oh, fair enough. |
@bellstrand I think I got it, since I await each promise it might be slower, but if I use promise.all? |
Well, you use the result of the first promise in the second await, so sadly that won't work! |
Well, at least we got isomorphic package now |
@bellstrand I read some stuff on the topic and it seems like because of V8 promises, the webcrypto API is actually slower than third-party libraries for small amounts of data, not sure if it's still relevant, but this might be why. |
@bellstrand I can't seem to be able to get jest to think I have tests for everything |
It went through the coverage tests on the last run thou! ;) |
@bellstrand sorry for bothering your travel 😅 |
Okay, so I don't really think I can do anything more about this I'd say this is finished state, but do tell me if you want me to change something (or everything 😅) |
@bellstrand not sure if you get notifications without ping. |
@7heMech Sorry for being slow, I stayed abroad for another week. |
@bellstrand resolved |
@bellstrand I'll do some more changes soon (maybe ASCII encoding key and fix some TS stuff) or at least suggest them as a pr. |
I've modified:
README.md
to reflect that library is now async