Documentation
¶
Index ¶
- Constants
- Variables
- func Bytes(size int, charset string) ([]byte, error)
- func BytesWithContext(ctx context.Context, size int, charset string) ([]byte, error)
- func MustBytes(size int, charset string) []byte
- func MustString(size int, charset string) string
- func SeededBytes(size int, charset string, seed ...int64) []byte
- func SeededBytesWithContext(ctx context.Context, size int, charset string, seed ...int64) ([]byte, error)
- func SeededString(size int, charset string, seed ...int64) string
- func SeededStringWithContext(ctx context.Context, size int, charset string, seed ...int64) (string, error)
- func String(size int, charset string) (string, error)
- func StringWithContext(ctx context.Context, size int, charset string) (string, error)
Constants ¶
const ( // UppercaseAlphabet contains all uppercase letters in the English alphabet (A-Z). UppercaseAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // LowercaseAlphabet contains all lowercase letters in the English alphabet (a-z). LowercaseAlphabet = "abcdefghijklmnopqrstuvwxyz" // Alphabet combines both lowercase and uppercase letters in the English alphabet. // It is equivalent to LowercaseAlphabet + UppercaseAlphabet. Alphabet = LowercaseAlphabet + UppercaseAlphabet // Numbers contains all digits (0-9) used to create numeric values. Numbers = "0123456789" // AlphaNumeric combines all letters and numbers. // It is equivalent to Alphabet + Numbers. AlphaNumeric = Alphabet + Numbers // Symbols contains a selection of common special characters for use in // password and token generation. Symbols = "<>,\\./|?;:[]{}+=_-()*&^%$#@!~" // ALL combines all alphanumeric characters and symbols. // It is equivalent to AlphaNumeric + Symbols. ALL = AlphaNumeric + Symbols )
Variables ¶
var ( ErrInvalidSize = errors.New("invalid size: must be greater than 0") ErrEmptyCharset = errors.New("invalid charset: cannot be empty") ErrRandomFailure = errors.New("failed to generate random bytes") )
Common error types for the strand package.
Functions ¶
func Bytes ¶
Bytes generates a cryptographically secure random byte slice using characters from the provided charset.
Parameters:
- size: the length of the byte slice to be returned. Must be greater than 0.
- charset: the string of characters from which bytes will be selected. Cannot be empty.
Returns:
- []byte: a randomly generated byte slice of the specified size.
- error: an error if random generation fails or if invalid parameters are provided.
This function uses crypto/rand and is suitable for security-sensitive applications like generating tokens, passwords, and cryptographic keys.
func BytesWithContext ¶ added in v1.3.0
BytesWithContext generates a cryptographically secure random byte slice using characters from the provided charset, with support for context cancellation.
Parameters:
- ctx: context for cancellation support.
- size: the length of the byte slice to be returned. Must be greater than 0.
- charset: the string of characters from which bytes will be selected. Cannot be empty.
Returns:
- []byte: a randomly generated byte slice of the specified size.
- error: an error if random generation fails, if invalid parameters are provided, or if the context is canceled.
This function uses crypto/rand and is suitable for security-sensitive applications.
func MustBytes ¶ added in v1.3.0
MustBytes works like Bytes but panics on error instead of returning it.
This function is useful when you know the inputs are valid and want a simpler API without error checking, such as in initialization code or when using constant values.
Parameters:
- size: the length of the byte slice to be returned. Must be greater than 0.
- charset: the string of characters from which bytes will be selected. Cannot be empty.
Returns a randomly generated byte slice of the specified size.
Panics if an error occurs during generation or if invalid parameters are provided.
func MustString ¶ added in v1.3.0
MustString works like String but panics on error instead of returning it.
This function is useful when you know the inputs are valid and want a simpler API without error checking, such as in initialization code or when using constant values.
Parameters:
- size: the length of the string to be returned. Must be greater than 0.
- charset: the string of characters from which the result will be generated. Cannot be empty.
Returns a randomly generated string of the specified size.
Panics if an error occurs during generation or if invalid parameters are provided.
func SeededBytes ¶
SeededBytes returns a deterministic byte slice based on the provided seed. The function generates a sequence of bytes where each byte is selected from the provided charset.
Parameters:
- size: the length of the byte slice to be returned.
- charset: the string of characters from which the bytes will be selected.
- seed: optional int64 value to initialize the random source. If omitted, time.Now().UnixNano() will be used as the default seed.
Returns a byte slice of the specified size with characters from the charset.
Security Notice: This function uses math/rand/v2 which is NOT cryptographically secure. For security-sensitive applications, use Bytes() instead.
func SeededBytesWithContext ¶ added in v1.3.0
func SeededBytesWithContext(ctx context.Context, size int, charset string, seed ...int64) ([]byte, error)
SeededBytesWithContext returns a deterministic byte slice like SeededBytes, but accepts a context for cancellation support.
Parameters:
- ctx: context for cancellation support.
- size: the length of the byte slice to be returned.
- charset: the string of characters from which the bytes will be selected.
- seed: optional int64 value to initialize the random source. If omitted, time.Now().UnixNano() will be used as the default seed.
Returns a byte slice of the specified size or an error if the context is canceled.
Security Notice: This function uses math/rand/v2 which is NOT cryptographically secure. For security-sensitive applications, use BytesWithContext() instead.
func SeededString ¶
SeededString returns a deterministic string based on the provided seed. This is a convenience wrapper around SeededBytes that converts the result to a string.
Parameters:
- size: the length of the string to be returned.
- charset: the string of characters from which the string will be generated.
- seed: optional int64 value to initialize the random source. If omitted, time.Now().UnixNano() will be used as the default seed.
Returns a string of the specified size with characters from the charset.
Security Notice: This function uses math/rand/v2 which is NOT cryptographically secure. For security-sensitive applications, use String() instead.
func SeededStringWithContext ¶ added in v1.3.0
func SeededStringWithContext(ctx context.Context, size int, charset string, seed ...int64) (string, error)
SeededStringWithContext returns a deterministic string like SeededString, but accepts a context for cancellation support.
Parameters:
- ctx: context for cancellation support.
- size: the length of the string to be returned.
- charset: the string of characters from which the string will be generated.
- seed: optional int64 value to initialize the random source. If omitted, time.Now().UnixNano() will be used as the default seed.
Returns a string of the specified size or an error if the context is canceled.
Security Notice: This function uses math/rand/v2 which is NOT cryptographically secure. For security-sensitive applications, use StringWithContext() instead.
func String ¶
String generates a cryptographically secure random string using characters from the provided charset.
Parameters:
- size: the length of the string to be returned. Must be greater than 0.
- charset: the string of characters from which the result will be generated. Cannot be empty.
Returns:
- string: a randomly generated string of the specified size.
- error: an error if random generation fails or if invalid parameters are provided.
This function uses crypto/rand and is suitable for security-sensitive applications like generating passwords, session tokens, and API keys.
func StringWithContext ¶ added in v1.3.0
StringWithContext generates a cryptographically secure random string using characters from the provided charset, with support for context cancellation.
Parameters:
- ctx: context for cancellation support.
- size: the length of the string to be returned. Must be greater than 0.
- charset: the string of characters from which the result will be generated. Cannot be empty.
Returns:
- string: a randomly generated string of the specified size.
- error: an error if random generation fails, if invalid parameters are provided, or if the context is canceled.
This function uses crypto/rand and is suitable for security-sensitive applications.
Types ¶
This section is empty.