override suspend fun verifyNonce()

in ktor-utils/jvm/src/io/ktor/util/StatelessHmacNonceManager.kt [61:86]


    override suspend fun verifyNonce(nonce: String): Boolean {
        val parts = nonce.split('+')
        if (parts.size != 3) return false
        val (random, time, mac) = parts

        if (random.length < 8) return false
        if (mac.length != macLength * 2) return false
        if (time.length != 16) return false

        val nanoTime = time.toLong(16)
        if (nanoTime + TimeUnit.MILLISECONDS.toNanos(timeoutMillis) < System.nanoTime()) return false

        val computedMac = hex(Mac.getInstance(algorithm).apply {
            init(keySpec)
            update("$random:$time".toByteArray(Charsets.ISO_8859_1))
        }.doFinal())

        var validCount = 0
        for (i in 0 until minOf(computedMac.length, mac.length)) {
            if (computedMac[i] == mac[i]) {
                validCount++
            }
        }

        return validCount == macLength * 2
    }