Skip to content
Permalink
Browse files

Rename nextNonce to generateNonce

  • Loading branch information...
cy6erGn0m committed Oct 19, 2018
1 parent d224f85 commit e788f34b8a1de91253ed870bc510d3fa08dda5fc
@@ -211,7 +211,7 @@ sealed class HttpAuthHeader(val authScheme: String) {
/**
* Generates an [AuthScheme.Digest] challenge as a [HttpAuthHeader].
*/
fun digestAuthChallenge(realm: String, nonce: String = nextNonce(), domain: List<String> = emptyList(), opaque: String? = null, stale: Boolean? = null, algorithm: String = "MD5")
fun digestAuthChallenge(realm: String, nonce: String = generateNonce(), domain: List<String> = emptyList(), opaque: String? = null, stale: Boolean? = null, algorithm: String = "MD5")
= Parameterized(AuthScheme.Digest, linkedMapOf<String, String>().apply {
put("realm", realm)
put("nonce", nonce)
@@ -33,7 +33,7 @@ interface OAuth2StateProvider {

object DefaultOAuth2StateProvider : OAuth2StateProvider {
override suspend fun getState(call: ApplicationCall): String {
return nextNonce()
return generateNonce()
}

override suspend fun verifyState(state: String) {
@@ -57,7 +57,7 @@ internal suspend fun simpleOAuth1aStep1(
client: HttpClient,
settings: OAuthServerSettings.OAuth1aServerSettings,
callbackUrl: String,
nonce: String = nextNonce(),
nonce: String = generateNonce(),
extraParameters: List<Pair<String, String>> = emptyList()
): OAuthCallback.TokenPair = simpleOAuth1aStep1(
client,
@@ -75,7 +75,7 @@ private suspend fun simpleOAuth1aStep1(
baseUrl: String,
callback: String,
consumerKey: String,
nonce: String = nextNonce(),
nonce: String = generateNonce(),
extraParameters: List<Pair<String, String>> = emptyList()
): OAuthCallback.TokenPair {
val authHeader = obtainRequestTokenHeader(
@@ -131,7 +131,7 @@ internal suspend fun simpleOAuth1aStep2(
client: HttpClient,
settings: OAuthServerSettings.OAuth1aServerSettings,
callbackResponse: OAuthCallback.TokenPair,
nonce: String = nextNonce(),
nonce: String = generateNonce(),
extraParameters: Map<String, String> = emptyMap()
): OAuthAccessTokenResponse.OAuth1a = simpleOAuth1aStep2(
client,
@@ -151,7 +151,7 @@ private suspend fun simpleOAuth1aStep2(
consumerKey: String,
token: String,
verifier: String,
nonce: String = nextNonce(),
nonce: String = generateNonce(),
extraParameters: Map<String, String> = emptyMap()
): OAuthAccessTokenResponse.OAuth1a {
val params = listOf(HttpAuthHeader.Parameters.OAuthVerifier to verifier) + extraParameters.toList()
@@ -92,7 +92,7 @@ class Serializer {
private fun setMaskBuffer(mask: Boolean) {
if (mask) {
maskBuffer = ByteBuffer.allocate(4).apply {
putInt(nextNonce().hashCode())
putInt(generateNonce().hashCode())
clear()
}
} else {
@@ -155,7 +155,7 @@ class PartialContent(private val maxRangeCount: Int) {
ranges: List<LongRange>,
length: Long
) {
val boundary = "ktor-boundary-" + nextNonce()
val boundary = "ktor-boundary-" + generateNonce()

call.attributes.put(Compression.SuppressionAttribute, true) // multirange with compression is not supported yet

@@ -169,7 +169,7 @@ class CookieIdSessionBuilder<S : Any>(type: KClass<S>) : CookieSessionBuilder<S>
/**
* Current session ID provider function
*/
var sessionIdProvider: () -> String = { nextNonce() }
var sessionIdProvider: () -> String = { generateNonce() }
private set
}

@@ -242,6 +242,6 @@ class HeaderIdSessionBuilder<S : Any>(type: KClass<S>) : HeaderSessionBuilder<S>
/**
* Current session ID provider function
*/
var sessionIdProvider: () -> String = { nextNonce() }
var sessionIdProvider: () -> String = { generateNonce() }
private set
}
@@ -7,10 +7,11 @@ import java.util.*
@KtorExperimentalAPI
fun getDigestFunction(algorithm: String, salt: String): (String) -> ByteArray = { e -> getDigest(e, algorithm, salt) }

private fun getDigest(text: String, algorithm: String, salt: String): ByteArray = with(MessageDigest.getInstance(algorithm)) {
update(salt.toByteArray())
digest(text.toByteArray())
}
private fun getDigest(text: String, algorithm: String, salt: String): ByteArray =
with(MessageDigest.getInstance(algorithm)) {
update(salt.toByteArray())
digest(text.toByteArray())
}

@InternalAPI
fun decodeBase64(s: String): ByteArray = Base64.getDecoder().decode(s)
@@ -27,7 +28,10 @@ fun hex(s: String): ByteArray {
val result = ByteArray(s.length / 2)
for (idx in 0 until result.size) {
val srcIdx = idx * 2
result[idx] = ((Integer.parseInt(s[srcIdx].toString(), 16)) shl 4 or Integer.parseInt(s[srcIdx + 1].toString(), 16)).toByte()
result[idx] = ((Integer.parseInt(s[srcIdx].toString(), 16)) shl 4 or Integer.parseInt(
s[srcIdx + 1].toString(),
16
)).toByte()
}

return result
@@ -42,21 +46,28 @@ fun hex(bytes: ByteArray) = bytes.joinToString("") {
fun raw(s: String) = s.toByteArray(Charsets.UTF_8)

@Suppress("KDocMissingDocumentation", "unused")
@Deprecated("Use nextNonce() instead")
@Deprecated("Use generateNonce() instead")
val nonceRandom: Random by lazy { SecureRandom() }

/**
* Generates a nonce string 16 characters long. Could block if system's entropy source is too
* Generates a nonce string 16 characters long. Could block if the system's entropy source is empty
*/
@KtorExperimentalAPI
@Deprecated("Use generateNonce() instead", ReplaceWith("generateNonce()"))
fun nextNonce(): String = generateNonce()

/**
* Generates a nonce string 16 characters long. Could block if the system's entropy source is empty
*/
@KtorExperimentalAPI
fun nextNonce(): String {
fun generateNonce(): String {
val nonce = seedChannel.poll()
if (nonce != null) return nonce

return nextNonceBlocking()
return generateNonceBlocking()
}

private fun nextNonceBlocking(): String {
private fun generateNonceBlocking(): String {
ensureNonceGeneratorRunning()
return runBlocking {
seedChannel.receive()
@@ -9,7 +9,7 @@ class NonceSmokeTest {
val nonceSet = HashSet<String>(4096)

repeat(4096) {
nonceSet.add(nextNonce())
nonceSet.add(generateNonce())
}

assertTrue { nonceSet.size == 4096 }

0 comments on commit e788f34

Please sign in to comment.
You can’t perform that action at this time.