-
Notifications
You must be signed in to change notification settings - Fork 0
[feature/22] :: generate a valid cpf #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
771933a
feat: added cpf generator
wellfernandes bced2f0
feat: added additional function for cpf validation
wellfernandes 1826d13
feat: added cpf composition in person
wellfernandes 1d8846f
feat: added cpf errors
wellfernandes f826d41
feat: added cpf generation example
wellfernandes 8a494c2
fix: fixed return statement inverted
wellfernandes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package cpf | ||
|
|
||
| import "errors" | ||
|
|
||
| var ( | ||
| ErrInvalidCPF = errors.New("invalid CPF") | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package cpf | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "math/rand" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| // GenerateCPF generates a valid CPF number. | ||
| // If formatted is true, the CPF will be returned in the format xxx.xxx.xxx-xx. | ||
| // If formatted is false, the CPF will be returned as a plain string of 11 digits. | ||
| func GenerateCPF(formatted bool) (string, error) { | ||
| // Create a local random generator with a unique seed | ||
| src := rand.NewSource(time.Now().UnixNano()) | ||
| r := rand.New(src) | ||
|
|
||
| // Generate the first 9 digits | ||
| digits := make([]int, 9) | ||
| for i := range digits { | ||
| digits[i] = r.Intn(10) | ||
| } | ||
|
|
||
| // Calculate the first check digit | ||
| digits = append(digits, calculateCheckDigit(digits, 10)) | ||
|
|
||
| // Calculate the second check digit | ||
| digits = append(digits, calculateCheckDigit(digits, 11)) | ||
|
|
||
| // Convert the digits to a string | ||
| cpf := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(digits)), ""), "[]") | ||
|
|
||
| // Format the CPF if requested | ||
| if formatted { | ||
| if len(cpf) != 11 { | ||
| return "", errors.New(ErrInvalidCPF.Error()) | ||
| } | ||
| return cpf[:3] + "." + cpf[3:6] + "." + cpf[6:9] + "-" + cpf[9:], nil | ||
| } | ||
|
|
||
| return cpf, nil | ||
| } | ||
|
|
||
| // calculateCheckDigit calculates the check digit for a CPF. | ||
| func calculateCheckDigit(digits []int, weight int) int { | ||
| sum := 0 | ||
| for _, digit := range digits { | ||
| sum += digit * weight | ||
| weight-- | ||
| } | ||
|
|
||
| remainder := sum % 11 | ||
| if remainder < 2 { | ||
| return 0 | ||
| } | ||
| return 11 - remainder | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package cpf | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ValidateCPF checks if a CPF is valid. | ||
| // It accepts both formatted (xxx.xxx.xxx-xx) and unformatted (xxxxxxxxxxx) CPFs. | ||
| func ValidateCPF(cpf string) bool { | ||
| // Remove formatting (dots and dashes) | ||
| cpf = strings.ReplaceAll(cpf, ".", "") | ||
| cpf = strings.ReplaceAll(cpf, "-", "") | ||
|
|
||
| // Check if the CPF has 11 digits | ||
| if len(cpf) != 11 { | ||
| return false | ||
| } | ||
|
|
||
| // Convert the CPF string to a slice of integers | ||
| digits := make([]int, 11) | ||
| for i, char := range cpf { | ||
| digit, err := strconv.Atoi(string(char)) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| digits[i] = digit | ||
| } | ||
|
|
||
| // Check if all digits are the same (invalid CPF) | ||
| allSame := true | ||
| for i := 1; i < len(digits); i++ { | ||
| if digits[i] != digits[0] { | ||
| allSame = false | ||
| break | ||
| } | ||
| } | ||
| if allSame { | ||
| return false | ||
| } | ||
|
|
||
| // Calculate the first check digit | ||
| firstCheckDigit := calculateCheckDigit(digits[:9], 10) | ||
| if firstCheckDigit != digits[9] { | ||
| return false | ||
| } | ||
|
|
||
| // Calculate the second check digit | ||
| secondCheckDigit := calculateCheckDigit(digits[:10], 11) | ||
| return secondCheckDigit != digits[10] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the incorrect return statement logic.
The return statement is inverted. It currently returns
truewhen the second check digit doesn't match, which is incorrect.📝 Committable suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wellfernandes Thanks for fixing the CPF validator's return statement! 🎉
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!