-
Notifications
You must be signed in to change notification settings - Fork 0
[feature/28] :: create mock for id registration pt br #44
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
wellfernandes
merged 13 commits into
develop
from
feature/28-create-mock-for-id-registration-pt-br
Apr 4, 2025
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ad123bc
feat: added initial structure for brazilian national id
wellfernandes caa6ddf
feat: added calculation for rg generation
wellfernandes 0d42533
feat: added national id errors
wellfernandes e28f259
feat: added national id generator
wellfernandes 45a6a16
feat: added national id example for brazil
wellfernandes 55ca91d
docs: added comment in func
wellfernandes c418c11
remove: removed image for new update
wellfernandes 3221282
docs: added mocai image
wellfernandes 37b32cb
docs: updated image in main readme
wellfernandes 12e83f0
docs: updated image in portuguese readme
wellfernandes ac77ba0
refactor: removed unused error
wellfernandes 57edec8
refactor: improved code readability
wellfernandes 35555a2
fix: fixed func comment
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
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
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions
77
pkg/mocai/entities/national_id/countries/brazilian_national_id.go
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,77 @@ | ||
| package countries | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math/rand" | ||
| "strconv" | ||
| "time" | ||
| ) | ||
|
|
||
| var globalRand = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
|
||
| type RG struct { | ||
| Number string | ||
| State string | ||
| IssuingBody string | ||
| } | ||
|
|
||
| // GenerateBrazilianNationalID generates a valid Brazilian national ID [RG] for São Paulo. | ||
| func GenerateBrazilianNationalID(formatted bool) (*RG, error) { | ||
| rgNumber, err := calculateSPRGDigit() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if formatted { | ||
| rgNumber = formatRG(rgNumber) | ||
| } | ||
| return &RG{ | ||
| Number: rgNumber, | ||
| State: "SP", | ||
| IssuingBody: "SSP - Secretaria de Seguranca Publica", | ||
| }, nil | ||
| } | ||
|
|
||
| // calculateSPRGDigit generates a valid Brazilian national ID [RG] for São Paulo. | ||
| func calculateSPRGDigit() (string, error) { | ||
| // generate a random base until 8 digits | ||
| base := globalRand.Intn(100000000) | ||
| baseStr := fmt.Sprintf("%08d", base) | ||
|
|
||
| // slice contains the digits | ||
| d := make([]int, 8) | ||
| for i := range 8 { | ||
| val, err := strconv.Atoi(string(baseStr[i])) | ||
| if err != nil { | ||
| return "", ErrToConvertDigit | ||
| } | ||
| d[i] = val | ||
| } | ||
|
|
||
| // wheights from right to left: 9, 8, 7, 6, 5, 4, 3, 2 | ||
| wheights := []int{2, 3, 4, 5, 6, 7, 8, 9} | ||
|
|
||
| // calculate sum | ||
| sum := 0 | ||
| for i := range 8 { | ||
| sum += d[7-i] * wheights[i] | ||
| } | ||
|
|
||
| // calculate check digit | ||
| checkDigit := sum % 11 | ||
| var dvStr string | ||
| if checkDigit == 10 { | ||
| dvStr = "X" | ||
| } else { | ||
| dvStr = strconv.Itoa(checkDigit) | ||
| } | ||
|
|
||
| full := fmt.Sprintf("%s%s", baseStr, dvStr) | ||
| return full, nil | ||
| } | ||
|
|
||
| func formatRG(rgNumber string) string { | ||
| if len(rgNumber) != 9 { | ||
| return rgNumber | ||
| } | ||
| return fmt.Sprintf("%s.%s.%s-%s", rgNumber[0:3], rgNumber[3:6], rgNumber[6:8], rgNumber[8:]) | ||
| } | ||
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,8 @@ | ||
| package countries | ||
|
|
||
| import "errors" | ||
|
|
||
| var ( | ||
| ErrGeneratingBrazilianNationalID = errors.New("error generating brazilian national id") | ||
| ErrToConvertDigit = errors.New("error converting digit") | ||
| ) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,19 @@ | ||
| package national_id | ||
|
|
||
| import "github.com/brazzcore/mocai/pkg/mocai/entities/national_id/countries" | ||
|
|
||
| type NationalID struct { | ||
| BrazilianRG *countries.RG | ||
| } | ||
|
|
||
| // GenerateNationalID generates a Brazilian national ID [RG]. | ||
| // If formatted is true, the Brazilian national ID will be returned in the format XX.XXX.XXX-X. | ||
| // if formatted is false, the Brazilian national ID will be returned as a plain string. | ||
| func GenerateNationalID(formatted bool) (*NationalID, error) { | ||
| rg, err := countries.GenerateBrazilianNationalID(formatted) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &NationalID{BrazilianRG: rg}, nil | ||
| } |
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.
Security risk with math/rand for ID generation.
Using
math/randfor generating identity documents is not secure enough as it's predictable. For security-sensitive data like national IDs,crypto/randshould be used instead.Then update the
calculateSPRGDigitfunction:func calculateSPRGDigit() (string, error) { // generate a random base until 8 digits - base := globalRand.Intn(100000000) - baseStr := fmt.Sprintf("%08d", base) + max := big.NewInt(100000000) + n, err := rand.Int(rand.Reader, max) + if err != nil { + return "", ErrGeneratingBrazilianNationalID + } + baseStr := fmt.Sprintf("%08d", n.Int64())📝 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.
there’s no need since the data is made up
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 You're right. Since this is just for generating mock data and not real IDs, using math/rand is perfectly fine. No need for the cryptographically secure alternative in this case.
✏️ Learnings added