Skip to content

Implement Random#javaSecuritySecureRandom on Scala.js#2906

Merged
vasilmkd merged 4 commits intotypelevel:series/3.3.xfrom
armanbilge:issue/2902
Mar 24, 2022
Merged

Implement Random#javaSecuritySecureRandom on Scala.js#2906
vasilmkd merged 4 commits intotypelevel:series/3.3.xfrom
armanbilge:issue/2902

Conversation

@armanbilge
Copy link
Copy Markdown
Member

Fixes #2902 by implementing our own java.security.SecureRandom shim that delegates to browser and Node.js crypto APIs.

While working on this I noticed there are other APIs that won't link on Scala.js, like the thread local stuff. Those really should be moved to platform traits in a PR targeting series/3.x.

@armanbilge armanbilge linked an issue Mar 24, 2022 that may be closed by this pull request

import scala.scalajs.js

private final class SecureRandom extends java.util.Random {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this gets called SecureRandom, the algebra in #2902 will need another name.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah. We can rename it UnsafeSecureRandom or JdkSecureRandom.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't call it Jdk when it doesn't come from one, but it's private. I just mostly want it to be called something else. 😆

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@armanbilge Do you want to do the renaming now, or should we press on? Otherwise, this is good to go.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rename it JavaSecureRandom.

Comment on lines +23 to +24
private val nextBytes: Int => js.typedarray.Int8Array =
if (js.typeOf(js.Dynamic.global.crypto) != "undefined") // browsers
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a trade-off initializing this in the class instead of the companion object. Caching is good, but this is not unreasonable if SecureRandom instances are not created very often.

Lazy initialization is important for environments that don't have any secure random capability, since there is no reasonable fallback implementation we can use in those situations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Random.javaSecuritySecureRandom doesn't link on Scala.js

3 participants