Skip to content

SecureRandom.javaSecuritySecureRandom for two effect types #4338

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

Open
wants to merge 3 commits into
base: series/3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,15 @@ private[std] trait SecureRandomCompanionPlatform {
}
}

@deprecated("Use 'of' instead", "3.7.0")
def javaSecuritySecureRandom[F[_]: Sync]: F[SecureRandom[F]] =
Sync[F].delay(unsafeJavaSecuritySecureRandom())

def of[F[_]: Sync]: F[SecureRandom[F]] = in[F, F]

def in[F[_]: Sync, G[_]: Sync]: F[SecureRandom[G]] =
Sync[F].delay(unsafeJavaSecuritySecureRandom[G]())

private[effect] def unsafeJavaSecuritySecureRandom[F[_]: Sync](): SecureRandom[F] =
new ScalaRandom[F](Applicative[F].pure(new JavaSecureRandom())) with SecureRandom[F] {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ private[std] trait SecureRandomCompanionPlatform {
private def javaUtilRandomBlocking[F[_]: Sync](random: JavaSecureRandom): SecureRandom[F] =
new ScalaRandom[F](Applicative[F].pure(random), Sync.Type.Blocking) with SecureRandom[F] {}

@deprecated("Use 'of' instead", "3.7.0")
def javaSecuritySecureRandom[F[_]: Sync]: F[SecureRandom[F]] =
Sync[F].delay(unsafeJavaSecuritySecureRandom())

def of[F[_]: Sync]: F[SecureRandom[F]] = in[F, F]

def in[F[_]: Sync, G[_]: Sync]: F[SecureRandom[G]] =
Sync[F].delay(unsafeJavaSecuritySecureRandom[G]())

/**
* Ported from https://github.com/http4s/http4s/.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ private[std] trait SecureRandomCompanionPlatform {

}

@deprecated("Use 'of' instead", "3.7.0")
def javaSecuritySecureRandom[F[_]: Sync]: F[SecureRandom[F]] =
Sync[F].delay(unsafeJavaSecuritySecureRandom())

def of[F[_]: Sync]: F[SecureRandom[F]] = in[F, F]

def in[F[_]: Sync, G[_]: Sync]: F[SecureRandom[G]] =
Sync[F].delay(unsafeJavaSecuritySecureRandom())

private[effect] def unsafeJavaSecuritySecureRandom[F[_]: Sync](): SecureRandom[F] =
new ScalaRandom[F](Applicative[F].pure(new JavaSecureRandom())) with SecureRandom[F] {}

Expand Down
31 changes: 29 additions & 2 deletions std/shared/src/main/scala/cats/effect/std/SecureRandom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ object SecureRandom extends SecureRandomCompanionPlatform {
}

/**
* Builds a `SecureRandom[F]` value for effect types that are [[cats.effect.kernel.Sync]] and
* initializes random state using the same effect type
*
* On the JVM, delegates to [[java.security.SecureRandom]].
*
* In browsers, delegates to the
Expand All @@ -136,7 +139,31 @@ object SecureRandom extends SecureRandomCompanionPlatform {
* on Linux, macOS, and BSD. Unsupported platforms such as Windows will encounter link-time
* errors.
*/
override def javaSecuritySecureRandom[F[_]: Sync]: F[SecureRandom[F]] =
super.javaSecuritySecureRandom[F]
override def javaSecuritySecureRandom[F[_]: Sync]: F[SecureRandom[F]] = of

/**
* Builds a `SecureRandom[F]` value for effect types that are [[cats.effect.kernel.Sync]] and
* initializes random state using the same effect type
*
* On the JVM, delegates to [[java.security.SecureRandom]].
*
* In browsers, delegates to the
* [[https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API Web Crypto API]].
*
* In Node.js, delegates to the [[https://nodejs.org/api/crypto.html crypto module]].
*
* On Native, delegates to
* [[https://man7.org/linux/man-pages/man3/getentropy.3.html getentropy]] which is supported
* on Linux, macOS, and BSD. Unsupported platforms such as Windows will encounter link-time
* errors.
*/
override def of[F[_]: Sync]: F[SecureRandom[F]] = in[F, F]

/**
* Builds a `SecureRandom[G]` value for effect types that are [[cats.effect.kernel.Sync]] but
* initializes random state using another effect constructor
*/
override def in[F[_]: Sync, G[_]: Sync]: F[SecureRandom[G]] =
Sync[F].delay(unsafeJavaSecuritySecureRandom[G]())

}
Loading