Skip to content
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

NativePRNGNonBlocking is not found, fallback to SHA1PRNG #3281

Merged
merged 1 commit into from Dec 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions ktor-utils/jvm/src/io/ktor/util/Nonce.kt
Expand Up @@ -11,8 +11,11 @@ import java.security.*

private const val SHA1PRNG = "SHA1PRNG"

private val SECURE_RANDOM_PROVIDER_NAME: String =
System.getProperty("io.ktor.random.secure.random.provider") ?: "NativePRNGNonBlocking"
private val SECURE_RANDOM_PROVIDERS: List<String> = listOf(
"NativePRNGNonBlocking",
"WINDOWS-PRNG",
"DRBG"
)

private const val SECURE_RESEED_PERIOD = 30_000

Expand Down Expand Up @@ -91,11 +94,16 @@ internal fun ensureNonceGeneratorRunning() {
}

private fun lookupSecureRandom(): SecureRandom {
val secure = getInstanceOrNull(SECURE_RANDOM_PROVIDER_NAME)
if (secure != null) return secure
System.getProperty("io.ktor.random.secure.random.provider")?.let { name ->
getInstanceOrNull(name)?.let { return it }
}

for (name in SECURE_RANDOM_PROVIDERS) {
getInstanceOrNull(name)?.let { return it }
}

LoggerFactory.getLogger("io.ktor.util.random")
.warn("$SECURE_RANDOM_PROVIDER_NAME is not found, fallback to default")
.warn("None of the ${SECURE_RANDOM_PROVIDERS.joinToString(separator = ", ")} found, fallback to default")

return getInstanceOrNull() ?: error("No SecureRandom implementation found")
}
Expand Down