Update randNum function and documentation for multiple versions

Expanded randNum function comments to provide detailed behavior and usage across different Go versions. Enhanced the randomStringSecure function doc to explain its cryptographic secure implementation and error handling.
This commit is contained in:
Winni Neessen 2024-10-06 17:00:49 +02:00
parent e640f2df46
commit cd90c3ddf3
Signed by: wneessen
GPG key ID: 385AC9889632126E
4 changed files with 56 additions and 9 deletions

View file

@ -14,14 +14,33 @@ import (
const cr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
// Bitmask sizes for the string generators (based on 93 chars total)
//
// These constants define bitmask-related values used for efficient random string generation.
// The bitmask operates over 93 possible characters, and the constants help determine the
// number of bits and indices used in the process.
const (
letterIdxBits = 7 // 7 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
// letterIdxBits: Number of bits (7) needed to represent a letter index.
letterIdxBits = 7
// letterIdxMask: Bitmask to extract letter indices (all 1-bits for letterIdxBits).
letterIdxMask = 1<<letterIdxBits - 1
// letterIdxMax: The maximum number of letter indices that fit in 63 bits.
letterIdxMax = 63 / letterIdxBits
)
// randomStringSecure returns a random, string of length characters. This method uses the
// crypto/random package and therfore is cryptographically secure
// randomStringSecure returns a random string of the specified length.
//
// This function generates a cryptographically secure random string of the given length using
// the crypto/rand package. It ensures that the randomness is secure and suitable for
// cryptographic purposes. The function reads random bytes, converts them to indices within
// a character range, and builds the string. If an error occurs while reading from the random
// pool, it returns the error.
//
// Parameters:
// - length: The length of the random string to be generated.
//
// Returns:
// - A randomly generated string.
// - An error if the random generation fails.
func randomStringSecure(length int) (string, error) {
randString := strings.Builder{}
randString.Grow(length)

View file

@ -12,7 +12,17 @@ import (
"time"
)
// randNum returns a random number with a maximum value of length
// randNum returns a random number with a maximum value of maxval.
//
// This function generates a random integer between 0 and maxval (exclusive). It seeds the
// random number generator with the current time in nanoseconds to ensure different results
// each time the function is called.
//
// Parameters:
// - maxval: The upper bound for the random number generation (exclusive).
//
// Returns:
// - A random integer between 0 and maxval. If maxval is less than or equal to 0, it returns 0.
func randNum(maxval int) int {
if maxval <= 0 {
return 0

View file

@ -11,7 +11,17 @@ import (
"math/rand"
)
// randNum returns a random number with a maximum value of length
// randNum returns a random number with a maximum value of maxval.
//
// This function generates a random integer between 0 and maxval (exclusive). If maxval is less
// than or equal to 0, it returns 0. The random number generator uses the default seed provided
// by the rand package.
//
// Parameters:
// - maxval: The upper bound for the random number generation (exclusive).
//
// Returns:
// - A random integer between 0 and maxval. If maxval is less than or equal to 0, it returns 0.
func randNum(maxval int) int {
if maxval <= 0 {
return 0

View file

@ -12,8 +12,16 @@ import (
)
// randNum returns a random number with a maximum value of maxval.
// go-mail compiled with Go 1.22+ will make use of the novel math/rand/v2 interface
// Older versions of Go will use math/rand
//
// This function generates a random integer between 0 and maxval (exclusive). It utilizes
// the math/rand/v2 interface for Go 1.22+ and will default to math/rand for older Go versions.
// If maxval is less than or equal to 0, it returns 0.
//
// Parameters:
// - maxval: The upper bound for the random number generation (exclusive).
//
// Returns:
// - A random integer between 0 and maxval. If maxval is less than or equal to 0, it returns 0.
func randNum(maxval int) int {
if maxval <= 0 {
return 0