Skip to content

Instantly share code, notes, and snippets.

@f3llps
Forked from trfiladelfo/CPFValidator.kt
Created April 27, 2024 18:59
Show Gist options
  • Save f3llps/a5ff113a1af644286d3cf5ae8eb367e7 to your computer and use it in GitHub Desktop.
Save f3llps/a5ff113a1af644286d3cf5ae8eb367e7 to your computer and use it in GitHub Desktop.
Validador de CPF em Kotlin
fun isCPF(document: String): Boolean {
if (document.isEmpty()) return false
val numbers = document.filter { it.isDigit() }.map {
it.toString().toInt()
}
if (numbers.size != 11) return false
//repeticao
if (numbers.all { it == numbers[0] }) return false
//digito 1
val dv1 = ((0..8).sumOf { (it + 1) * numbers[it] }).rem(11).let {
if (it >= 10) 0 else it
}
val dv2 = ((0..8).sumOf { it * numbers[it] }.let { (it + (dv1 * 9)).rem(11) }).let {
if (it >= 10) 0 else it
}
return numbers[9] == dv1 && numbers[10] == dv2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment