import decrypt.ArchiveDecrypt
import decrypt.Encrypt
import decrypt.FileArchiveDecrypt
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import progress.Progress
import progress.ProgressFileHandler
import java.io.File
import java.util.concurrent.atomic.AtomicReference
import java.util.logging.Logger
import javax.swing.JOptionPane
import kotlin.time.measureTime

object BruteForce {
    private val archiveDecrypt: ArchiveDecrypt = FileArchiveDecrypt
    private val progress: ProgressFileHandler = Progress
    private val passwordFoundFlow = MutableSharedFlow<String>()
    private var passwordFound = AtomicReference<String?>(null)

    /**
     * Tenta descriptografar o arquivo fornecido utilizando a técnica de força bruta.
     *
     * @param file o arquivo a ser descriptografado
     * @return a senha utilizada para descriptografar o arquivo
     * @throws IllegalArgumentException se o arquivo não for criptografado
     */
    suspend fun force(file: File): String {
        if (!Encrypt().isEncrypted(file)) {
            throw IllegalArgumentException("Arquivo não criptografado!")
        }
        progress.loadProgress(file.name)

        val duration = measureTime {
            val consumers = List(Runtime.getRuntime().availableProcessors()) {
                CoroutineScope(Dispatchers.IO).launch { tryPasswords(file) }
            }
            passwordFound = passwordFoundFlow.first()
            consumers.forEach { it.cancel() }
        }
        val passwordsGenerated = progress.passwordIterator.totalPasswordsGenerated.toInt()

        JOptionPane.showMessageDialog(
            null, "\n--Senha encontrada--\n$passwordFound\nTempo de execução: $duration\nSenhas testadas: " + "$passwordsGenerated\nSenhas por segundo: ${
                if (duration.inWholeSeconds > 0) {
                    passwordsGenerated / duration.inWholeSeconds
                } else {
                    passwordsGenerated / duration.inWholeMilliseconds
                }
            }", "Senha encontrada!", JOptionPane.INFORMATION_MESSAGE
        )
        Logger.getLogger(this.javaClass.name).info(
            "\n--Senha encontrada--\n$passwordFound\nTempo de execução: $duration\nSenhas testadas: " + "$passwordsGenerated\nSenhas por segundo: ${
                if (duration.inWholeSeconds > 0) {
                    passwordsGenerated / duration.inWholeSeconds
                } else {
                    passwordsGenerated / duration.inWholeMilliseconds
                }
            }"
        )

        return passwordFound.get()!!.also {
            passwordFound = AtomicReference<String?>(null)
            progress.passwordIterator.reset()
        }
    }

    private suspend fun tryPasswords(archiveFile: File) {
        while (passwordFound.get() == null) {
            if (progress.passwordIterator.totalPasswordsGenerated.toInt() % 5000 == 0) {
                progress.saveProgress(archiveFile.name)
            }
            val result = archiveDecrypt.tryPassword(archiveFile, progress.passwordIterator.getNextPassword())
            if (passwordFound.compareAndSet(null, result) && result?.isNotBlank() == true) {
                passwordFoundFlow.emit(result)
                break
            }
        }
    }
}

a linha 
passwordFound = passwordFoundFlow.first()
está errada, como posso corrigir?