Skip to content

Commit

Permalink
Merge pull request #3678 from armanbilge/topic/optimize-native-secure…
Browse files Browse the repository at this point in the history
…random
  • Loading branch information
djspiewak authored Jun 12, 2023
2 parents d6cf051 + cc0cda8 commit 5783a0b
Showing 1 changed file with 5 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package cats.effect.std

import org.typelevel.scalaccompat.annotation._

import scala.scalanative.libc.errno
import scala.scalanative.libc.errno._
import scala.scalanative.libc.string._
import scala.scalanative.unsafe._
import scala.scalanative.unsigned._

Expand All @@ -30,19 +31,12 @@ private[std] trait SecureRandomCompanionPlatform {

override def nextBytes(bytes: Array[Byte]): Unit = {
val len = bytes.length
val buffer = stackalloc[Byte](256)
var i = 0
while (i < len) {
val n = Math.min(256, len - i)
if (sysrandom.getentropy(buffer, n.toULong) < 0)
throw new RuntimeException(s"getentropy: ${errno.errno}")

var j = 0L
while (j < n) {
bytes(i) = buffer(j)
i += 1
j += 1
}
if (sysrandom.getentropy(bytes.at(i), n.toULong) < 0)
throw new RuntimeException(fromCString(strerror(errno)))
i += n
}
}

Expand Down

0 comments on commit 5783a0b

Please sign in to comment.