11/* 
2-  * Copyright 2019  Leonardo Colman Lopes 
2+  * Copyright 2024  Leonardo Colman Lopes 
33 * 
44 * Licensed under the Apache License, Version 2.0 (the "License"); 
55 * you may not use this file except in compliance with the License. 
1414 * limitations under the License. 
1515 */  
1616@file:Suppress(" MagicNumber" 
17+ 
1718package  br.com.colman.simplecpfvalidator 
1819
1920import  kotlin.math.abs 
@@ -39,7 +40,7 @@ import kotlin.math.abs
3940 */  
4041fun  String.isCpf (charactersToIgnore :  List <Char > = listOf('.', '-')): Boolean  {
4142    val  cleanCpf =  this .filterNot { it in  charactersToIgnore }
42-     if  (cleanCpf.containsInvalidCPFChars() ||  cleanCpf.isInvalidCpfSize() ||  cleanCpf.isBlacklistedCpf ()) return  false 
43+     if  (cleanCpf.containsInvalidCPFChars() ||  cleanCpf.isInvalidCpfSize() ||  cleanCpf.isInvalidCpf ()) return  false 
4344    return  cleanCpf.hasValidVerificationDigits()
4445}
4546
@@ -60,14 +61,14 @@ fun String.isCpf(charactersToIgnore: List<Char> = listOf('.', '-')): Boolean {
6061 * @see [https://pt.wikipedia.org/wiki/Cadastro_de_pessoas_f%C3%ADsicas] 
6162 * @see [http://normas.receita.fazenda.gov.br/sijut2consulta/link.action?visao=anotado&idAto=1893] 
6263 */  
63- fun  Long.isCpf ()  : Boolean  {
64+ fun  Long.isCpf (): Boolean  {
6465    val  absNumber =  abs(this )
6566    return  absNumber.toString().isCpf()
6667}
6768
6869private  fun  String.containsInvalidCPFChars () =  this .any { ! it.isDigit() }
6970private  fun  String.isInvalidCpfSize () =  this .length !=  11 
70- private  fun  String.isBlacklistedCpf () =  this  in  blacklistedCpfs 
71+ private  fun  String.isInvalidCpf () =  this  in  invalidCpfs 
7172
7273//  Algorithm from https://www.somatematica.com.br/faq/cpf.php
7374private  fun  String.hasValidVerificationDigits (): Boolean  {
@@ -101,7 +102,7 @@ private fun List<Int>.calculateFirstVerificationDigit(): Int {
101102    val  sum =  firstNineDigits.withIndex().sumOf { (index, element) ->  weights[index] *  element }
102103
103104    val  remainder =  sum %  11 
104-     return  if (remainder <  2 ) 0  else  11  -  remainder
105+     return  if   (remainder <  2 ) 0  else  11  -  remainder
105106}
106107
107108private  fun  List<Int>.calculateSecondVerificationDigit (firstDigit :  Int ): Int  {
@@ -127,7 +128,11 @@ private fun List<Int>.calculateSecondVerificationDigit(firstDigit: Int): Int {
127128    return  if  (remainder <  2 ) 0  else  11  -  remainder
128129}
129130
130- private  val  blacklistedCpfs =  listOf (
131+ /* *
132+  * These CPFs although are numerically valid (i.e. [hasValidVerificationDigits] are considered invalid as per CPF 
133+  * specification 
134+  */  
135+ private  val  invalidCpfs =  listOf (
131136    " 00000000000" 
132137    " 11111111111" 
133138    " 22222222222" 
0 commit comments