Refactor variable names and correct typos in RandomBytes and RandNum methods

This commit includes refactoring variable names to enhance readability and understanding within the `RandomBytes`, `RandNum` and `RandomStringFromCharRange` methods. A constant, `maxInt32`, was also added to replace a hard-coded value for better code practice. Typos in the comments and documentation were corrected as well.
This commit is contained in:
Winni Neessen 2024-03-07 22:50:47 +01:00
parent 4b874e499e
commit 8f8e439f56
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D

View file

@ -18,6 +18,9 @@ const (
letterIdxMax = 63 / letterIdxBits letterIdxMax = 63 / letterIdxBits
) )
// maxInt32 is the maximum positive value for a int32 number type
const maxInt32 = 2147483647
var ( var (
// ErrInvalidLength is returned if the provided maximum number is equal or less than zero // ErrInvalidLength is returned if the provided maximum number is equal or less than zero
ErrInvalidLength = errors.New("provided length value cannot be zero or less") ErrInvalidLength = errors.New("provided length value cannot be zero or less")
@ -58,80 +61,80 @@ func (g *Generator) GetPasswordLength() (int64, error) {
if g.config.FixedLength > 0 { if g.config.FixedLength > 0 {
return g.config.FixedLength, nil return g.config.FixedLength, nil
} }
mil := g.config.MinLength minLength := g.config.MinLength
mal := g.config.MaxLength maxLength := g.config.MaxLength
if mil > mal { if minLength > maxLength {
mal = mil maxLength = minLength
} }
diff := mal - mil + 1 diff := maxLength - minLength + 1
ra, err := g.RandNum(diff) randNum, err := g.RandNum(diff)
if err != nil { if err != nil {
return 0, err return 0, err
} }
l := mil + ra length := minLength + randNum
if l <= 0 { if length <= 0 {
return 1, nil return 1, nil
} }
return l, nil return length, nil
} }
// RandomBytes returns a byte slice of random bytes with length n that got generated by // RandomBytes returns a byte slice of random bytes with given length that got generated by
// the crypto/rand generator // the crypto/rand generator
func (g *Generator) RandomBytes(n int64) ([]byte, error) { func (g *Generator) RandomBytes(length int64) ([]byte, error) {
if n < 1 { if length < 1 {
return nil, ErrInvalidLength return nil, ErrInvalidLength
} }
b := make([]byte, n) bytes := make([]byte, length)
l, err := rand.Read(b) numBytes, err := rand.Read(bytes)
if int64(l) != n { if int64(numBytes) != length {
return nil, ErrLengthMismatch return nil, ErrLengthMismatch
} }
if err != nil { if err != nil {
return nil, err return nil, err
} }
return b, nil return bytes, nil
} }
// RandNum generates a random, non-negative number with given maximum value // RandNum generates a random, non-negative number with given maximum value
func (g *Generator) RandNum(m int64) (int64, error) { func (g *Generator) RandNum(max int64) (int64, error) {
if m < 1 { if max < 1 {
return 0, ErrInvalidLength return 0, ErrInvalidLength
} }
mbi := big.NewInt(m) max64 := big.NewInt(max)
rn, err := rand.Int(rand.Reader, mbi) randNum, err := rand.Int(rand.Reader, max64)
if err != nil { if err != nil {
return 0, fmt.Errorf("random number generation failed: %w", err) return 0, fmt.Errorf("random number generation failed: %w", err)
} }
return rn.Int64(), nil return randNum.Int64(), nil
} }
// RandomStringFromCharRange returns a random string of length l based of the range of characters given. // RandomStringFromCharRange returns a random string of length l based of the range of characters given.
// The method makes use of the crypto/random package and therfore is // The method makes use of the crypto/random package and therfore is
// cryptographically secure // cryptographically secure
func (g *Generator) RandomStringFromCharRange(l int64, cr string) (string, error) { func (g *Generator) RandomStringFromCharRange(length int64, charRange string) (string, error) {
if l < 1 { if length < 1 {
return "", ErrInvalidLength return "", ErrInvalidLength
} }
if len(cr) < 1 { if len(charRange) < 1 {
return "", ErrInvalidCharRange return "", ErrInvalidCharRange
} }
rs := strings.Builder{} rs := strings.Builder{}
// As long as the length is smaller than the max. int32 value let's grow // As long as the length is smaller than the max. int32 value let's grow
// the string builder to the actual size, so we need less allocations // the string builder to the actual size, so we need less allocations
if l <= 2147483647 { if length <= maxInt32 {
rs.Grow(int(l)) rs.Grow(int(length))
} }
crl := len(cr) charRangeLength := len(charRange)
rp := make([]byte, 8) rp := make([]byte, 8)
_, err := rand.Read(rp) _, err := rand.Read(rp)
if err != nil { if err != nil {
return rs.String(), err return rs.String(), err
} }
for i, c, r := l-1, binary.BigEndian.Uint64(rp), letterIdxMax; i >= 0; { for i, c, r := length-1, binary.BigEndian.Uint64(rp), letterIdxMax; i >= 0; {
if r == 0 { if r == 0 {
_, err = rand.Read(rp) _, err = rand.Read(rp)
if err != nil { if err != nil {
@ -139,8 +142,8 @@ func (g *Generator) RandomStringFromCharRange(l int64, cr string) (string, error
} }
c, r = binary.BigEndian.Uint64(rp), letterIdxMax c, r = binary.BigEndian.Uint64(rp), letterIdxMax
} }
if idx := int(c & letterIdxMask); idx < crl { if idx := int(c & letterIdxMask); idx < charRangeLength {
rs.WriteByte(cr[idx]) rs.WriteByte(charRange[idx])
i-- i--
} }
c >>= letterIdxBits c >>= letterIdxBits