mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 05:40:50 +01:00
Refactor variable names for better clarity
Adjusted variable names in several functions and improved code readability. Full, descriptive names have replaced abbreviations to make the codes self-explanatory. The change is aimed at enhancing code maintenance and adhering to Go's best practices for naming conventions.
This commit is contained in:
parent
19a3cf61ed
commit
7de4f5053f
1 changed files with 31 additions and 32 deletions
63
random.go
63
random.go
|
@ -22,54 +22,53 @@ const (
|
||||||
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
|
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
|
||||||
)
|
)
|
||||||
|
|
||||||
// randomStringSecure returns a random, n long string of characters. The character set is based
|
// randomStringSecure returns a random, string of length characters. This method uses the
|
||||||
// on the s (special chars) and h (human readable) boolean arguments. This method uses the
|
|
||||||
// crypto/random package and therfore is cryptographically secure
|
// crypto/random package and therfore is cryptographically secure
|
||||||
func randomStringSecure(n int) (string, error) {
|
func randomStringSecure(length int) (string, error) {
|
||||||
rs := strings.Builder{}
|
randString := strings.Builder{}
|
||||||
rs.Grow(n)
|
randString.Grow(length)
|
||||||
crl := len(cr)
|
charRangeLength := len(cr)
|
||||||
|
|
||||||
rp := make([]byte, 8)
|
randPool := make([]byte, 8)
|
||||||
_, err := rand.Read(rp)
|
_, err := rand.Read(randPool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rs.String(), err
|
return randString.String(), err
|
||||||
}
|
}
|
||||||
for i, c, r := n-1, binary.BigEndian.Uint64(rp), letterIdxMax; i >= 0; {
|
for idx, char, rest := length-1, binary.BigEndian.Uint64(randPool), letterIdxMax; idx >= 0; {
|
||||||
if r == 0 {
|
if rest == 0 {
|
||||||
_, err := rand.Read(rp)
|
_, err = rand.Read(randPool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rs.String(), err
|
return randString.String(), err
|
||||||
}
|
}
|
||||||
c, r = binary.BigEndian.Uint64(rp), letterIdxMax
|
char, rest = binary.BigEndian.Uint64(randPool), letterIdxMax
|
||||||
}
|
}
|
||||||
if idx := int(c & letterIdxMask); idx < crl {
|
if idx := int(char & letterIdxMask); idx < charRangeLength {
|
||||||
rs.WriteByte(cr[idx])
|
randString.WriteByte(cr[idx])
|
||||||
i--
|
idx--
|
||||||
}
|
}
|
||||||
c >>= letterIdxBits
|
char >>= letterIdxBits
|
||||||
r--
|
rest--
|
||||||
}
|
}
|
||||||
|
|
||||||
return rs.String(), nil
|
return randString.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// randNum returns a random number with a maximum value of n
|
// randNum returns a random number with a maximum value of length
|
||||||
func randNum(n int) (int, error) {
|
func randNum(length int) (int, error) {
|
||||||
if n <= 0 {
|
if length <= 0 {
|
||||||
return 0, fmt.Errorf("provided number is <= 0: %d", n)
|
return 0, fmt.Errorf("provided number is <= 0: %d", length)
|
||||||
}
|
}
|
||||||
mbi := big.NewInt(int64(n))
|
length64 := big.NewInt(int64(length))
|
||||||
if !mbi.IsUint64() {
|
if !length64.IsUint64() {
|
||||||
return 0, fmt.Errorf("big.NewInt() generation returned negative value: %d", mbi)
|
return 0, fmt.Errorf("big.NewInt() generation returned negative value: %d", length64)
|
||||||
}
|
}
|
||||||
rn64, err := rand.Int(rand.Reader, mbi)
|
randNum64, err := rand.Int(rand.Reader, length64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
rn := int(rn64.Int64())
|
randomNum := int(randNum64.Int64())
|
||||||
if rn < 0 {
|
if randomNum < 0 {
|
||||||
return 0, fmt.Errorf("generated random number does not fit as int64: %d", rn64)
|
return 0, fmt.Errorf("generated random number does not fit as int64: %d", randNum64)
|
||||||
}
|
}
|
||||||
return rn, nil
|
return randomNum, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue