|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Algorithm for validating CPF |
| 5 | +The CPF calculation is based on the final 2 digits. |
| 6 | +To validate, take the first 9 digits of the CPF, generate the 2 digits and save in a new CPF. |
| 7 | +Compare if the CPF sent is the same as the CPF generated. |
| 8 | +If true, the CPF is valid, otherwise invalid. |
| 9 | + |
| 10 | +* Get first digit: |
| 11 | +--- Multiply the first 9 digits of the CPF by a count regress starting from 10 and ending at 2. |
| 12 | +--- Add all the multiplication values from step 1 |
| 13 | +--- Get the remainder of the division between the sum and 11 from step 2 |
| 14 | +--- Subtract the result from step 3 by 11 |
| 15 | +--- If the result of step 4 is greater than nine, the digit is zero, otherwise the digit is the value from step 4 |
| 16 | + |
| 17 | +* Get second digit: |
| 18 | +--- Multiply the first 9 digits of the CPF, PLUS THE FIRST DIGIT, previously obtained by a countdown starting from 11 and ending in 2 |
| 19 | +--- Same logic as step 2 from the first digit onwards. |
| 20 | + |
| 21 | +SAMPLE CPF: 113.314.390-35 |
| 22 | + |
| 23 | +1*10 = 10 |
| 24 | +1*9 = 9 |
| 25 | +3*8 = 24 |
| 26 | +3*7 = 21 |
| 27 | +1*6 = 6 |
| 28 | +4*5 = 20 |
| 29 | +3*4 = 12 |
| 30 | +9*3 = 27 |
| 31 | +0*2 = 0 |
| 32 | + |
| 33 | +DIGITS SUM: 1 +たす 1 +たす 3 +たす 3 +たす 1 +たす 4 +たす 3 +たす 9 +たす 0 +たす 3 +たす 5 =わ 33 |
| 34 | +TOTAL: 10 +たす 9 +たす 24 +たす 21 +たす 6 +たす 20 +たす 12 +たす 27 +たす 0 +たす 0 =わ 129 |
| 35 | + |
| 36 | +""" |
| 37 | + |
| 38 | +import re |
| 39 | + |
| 40 | +debugMode = True |
| 41 | + |
| 42 | + |
| 43 | +def logThis(message): |
| 44 | + if debugMode: |
| 45 | + print(message) |
| 46 | + |
| 47 | + |
| 48 | +class CPF_Validator_N: |
| 49 | + def __init__(self, userCPF): |
| 50 | + self.userCPF = userCPF |
| 51 | + |
| 52 | + @property |
| 53 | + def validateCPF(self): |
| 54 | + return self.userCPF |
| 55 | + |
| 56 | + # if debugMode: |
| 57 | + # sampleCPF = "113.314.390-35" |
| 58 | + # print(f"Sample CPF for testing: {sampleCPF}") |
| 59 | + # userCPF = input("CPF: ") |
| 60 | + validCPF = None |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + # Validates a CPF number with the easy way. |
| 64 | + def validateCPF(userCPF): |
| 65 | + # regular expression to remove any non-numeric characters, spaces, dots or dashes. |
| 66 | + userCPF = re.sub("[^0-9]", "", userCPF) |
| 67 | + logThis(userCPF) |
| 68 | + |
| 69 | + # Check length of CPF |
| 70 | + if len(userCPF) != 11: |
| 71 | + validCPF = False |
| 72 | + logThis("CPF is NOT valid.") |
| 73 | + return validCPF |
| 74 | + else: |
| 75 | + |
| 76 | + # Separate all numbers from userCPF into a list. |
| 77 | + userCPFList = list(userCPF) |
| 78 | + # Sum every integer of the userCPFList |
| 79 | + sumOfDigits = sum(int(i) for i in userCPFList) |
| 80 | + sumOfDigits = str(sumOfDigits) |
| 81 | + |
| 82 | + if sumOfDigits[0] == sumOfDigits[1]: |
| 83 | + logThis("CPF partially valid.") |
| 84 | + |
| 85 | + # Get first nine digits from userCPF |
| 86 | + firstNineDigits = userCPFList[0:9] |
| 87 | + sumMaster = 0 |
| 88 | + for key, multiply in enumerate(range(len(firstNineDigits) + 1, 1, -1)): |
| 89 | + # logThis(f"{userCPFList[key]} * {multiply}") |
| 90 | + sumMaster += int(userCPFList[key]) * multiply |
| 91 | + restSum = 11 - (sumMaster % 11) |
| 92 | + |
| 93 | + if restSum > 9: |
| 94 | + restSum = 0 |
| 95 | + |
| 96 | + logThis(f"Rest sum: {restSum}") |
| 97 | + mergeRes = str(userCPF[:9]) + str(restSum) |
| 98 | + logThis(f"Merged result: {mergeRes}") |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + sampleCPF = "113.314.390-35" |
| 103 | + CPF_Validator_N.validateCPF(sampleCPF) |
0 commit comments