feat: registrationBegin method without email;
fix: missing sqlc query parameter name; feat: util for generating security codes; feat: enums package
This commit is contained in:
8
backend/internal/utils/enums/enums.go
Normal file
8
backend/internal/utils/enums/enums.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package enums
|
||||
|
||||
type ConfirmationCodeType int32
|
||||
|
||||
const (
|
||||
RegistrationCodeType ConfirmationCodeType = iota
|
||||
PasswordResetCodeType
|
||||
)
|
||||
26
backend/internal/utils/securityCode.go
Normal file
26
backend/internal/utils/securityCode.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
func GenerateSecure6DigitNumber() (string, error) {
|
||||
// Generate a random number between 0 and 999999 (inclusive)
|
||||
// This ensures we get a 6-digit number, including those starting with 0
|
||||
max := 1000000 // Upper bound (exclusive)
|
||||
b := make([]byte, 4) // A 4-byte slice is sufficient for a 32-bit integer
|
||||
|
||||
if _, err := io.ReadFull(rand.Reader, b); err != nil {
|
||||
return "", fmt.Errorf("failed to read random bytes: %w", err)
|
||||
}
|
||||
|
||||
// Convert bytes to an integer
|
||||
// We use a simple modulo operation to get a number within our desired range.
|
||||
// While this introduces a slight bias for very large ranges, for 1,000,000
|
||||
// it's negligible and simpler than more complex methods like rejection sampling.
|
||||
num := int(uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])) % max
|
||||
|
||||
return fmt.Sprintf("%06d", num), nil
|
||||
}
|
||||
Reference in New Issue
Block a user