Documentation ¶
Overview ¶
Package rands provides a suite of functions that use crypto/rand to generate cryptographically secure random strings in various formats, as well as ints and bytes.
All functions which produce strings from a alphabet of characters uses rand.Int() to ensure a uniform distribution of all possible values.
rands is intended for use in production code where random data generation is required. All functions have a error return value, which should be checked.
For tests there is the randsmust package, which has all the same functions but with single return values, and they panic in the event of an error.
Index ¶
- Variables
- func Alphabetic(n int) (string, error)
- func Alphanumeric(n int) (string, error)
- func Base64(n int) (string, error)
- func Base64URL(n int) (string, error)
- func Bytes(n int) ([]byte, error)
- func DNSLabel(n int) (string, error)
- func Hex(n int) (string, error)
- func Int(max int) (int, error)
- func Int64(max int64) (int64, error)
- func Lower(n int) (string, error)
- func LowerNumeric(n int) (string, error)
- func Numeric(n int) (string, error)
- func String(n int, alphabet string) (string, error)
- func UUID() (string, error)
- func UnicodeString(n int, alphabet []rune) (string, error)
- func Upper(n int) (string, error)
- func UpperNumeric(n int) (string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNonASCIIAlphabet = fmt.Errorf( "%w: alphabet contains non-ASCII characters", Err, ) ErrDNSLabelLength = fmt.Errorf( "%w: DNS labels must be between 1 and 63 characters in length", Err, ) )
var Err = errors.New("rands")
var ErrInvalidMaxInt = fmt.Errorf("%w: max cannot be less than 1", Err)
Functions ¶
func Alphabetic ¶
Alphabetic generates a random alphabetic string of n length.
The returned string may contain A-Z, and a-z.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.Alphabetic(16) if err != nil { log.Fatal(err) } fmt.Println(s) // => VzcovEqvMRBWUtQC }
Output:
func Alphanumeric ¶
Alphanumeric generates a random alphanumeric string of n length.
The returned string may contain A-Z, a-z, and 0-9.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.Alphanumeric(16) if err != nil { log.Fatal(err) } fmt.Println(s) // => EgPieCBO7MuWhHtj }
Output:
func Base64 ¶
Base64 generates a random base64 encoded string of n number of bytes.
Length of the returned string is about one third greater than the value of n, and it may contain characters A-Z, a-z, 0-9, "+", "/", and "=".
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.Base64(16) if err != nil { log.Fatal(err) } fmt.Println(s) // => nYQLhIYTqh8oH/W4hZuXMQ== }
Output:
func Base64URL ¶
Base64URL generates a URL-safe un-padded random base64 encoded string of n number of bytes.
Length of the returned string is about one third greater than the value of n, and it may contain characters A-Z, a-z, 0-9, "-", and "_".
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.Base64URL(16) if err != nil { log.Fatal(err) } fmt.Println(s) // => zI_zrc1l0uPT4MxncR6e5w }
Output:
func Bytes ¶
Bytes generates a byte slice of n number of random bytes.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { b, err := rands.Bytes(8) if err != nil { log.Fatal(err) } fmt.Printf("%+v\n", b) // => [181 153 143 235 241 20 208 173] }
Output:
func DNSLabel ¶
DNSLabel returns a random string of n length in a DNS label compliant format as defined in RFC 1035, section 2.3.1.
It also adheres to RFC 5891, section 4.2.3.1.
In summary, the generated random string will:
- be between 1 and 63 characters in length, other n values returns a error
- first character will be one of a-z
- last character will be one of a-z or 0-9
- in-between first and last characters consist of a-z, 0-9, or "-"
- potentially contain two or more consecutive "-", except the 3rd and 4th characters, as that would violate RFC 5891, section 4.2.3.1.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.DNSLabel(16) if err != nil { log.Fatal(err) } fmt.Println(s) // => ab-sbh5q0gfb6sqo }
Output:
func Hex ¶
Hex generates a random hexadecimal encoded string of n number of bytes.
Length of the returned string is twice the value of n, and it may contain characters 0-9 and a-f.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.Hex(16) if err != nil { log.Fatal(err) } fmt.Println(s) // => b59e8977a13f3c030bd2ea1002ec8081 }
Output:
func Int ¶
Int generates a random int ranging between 0 and max.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { n, err := rands.Int(2147483647) if err != nil { log.Fatal(err) } fmt.Printf("%d\n", n) // => 1908357440 }
Output:
func Int64 ¶
Int64 generates a random int64 ranging between 0 and max.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { n, err := rands.Int64(int64(9223372036854775807)) if err != nil { log.Fatal(err) } fmt.Printf("%d\n", n) // => 6530460062499341591 }
Output:
func Lower ¶
Lower generates a random lowercase alphabetic string of n length.
The returned string may contain a-z.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.Lower(16) if err != nil { log.Fatal(err) } fmt.Println(s) // => dhoqhrqljadsztaa }
Output:
func LowerNumeric ¶
LowerNumeric generates a random lowercase alphanumeric string of n length.
The returned string may contain A-Z and 0-9.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.LowerNumeric(16) if err != nil { log.Fatal(err) } fmt.Println(s) // => th1z1b1d24l5h8pu }
Output:
func Numeric ¶
Numeric generates a random numeric string of n length.
The returned string may contain 0-9.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.Numeric(16) if err != nil { log.Fatal(err) } fmt.Println(s) // => 3378802228987741 }
Output:
func String ¶
String generates a random string of n length using the given ASCII alphabet.
The specified alphabet determines what characters are used in the returned random string. The alphabet can only contain ASCII characters, use UnicodeString() if you need a alphabet with Unicode characters.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.String(16, "abcdefABCDEF") if err != nil { log.Fatal(err) } fmt.Println(s) // => BAFffADaadeeacfa }
Output:
func UUID ¶ added in v0.2.0
UUID returns a random UUID v4 in string format as defined by RFC 4122, section 4.4.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.UUID() if err != nil { log.Fatal(err) } fmt.Println(s) // => 6a1c4f65-d5d6-4a28-aa51-eaa94fa7ad4a }
Output:
func UnicodeString ¶
UnicodeString generates a random string of n length using the given Unicode alphabet.
The specified alphabet determines what characters are used in the returned random string. The length of the returned string will be n or greater depending on the byte-length of characters which were randomly selected from the alphabet.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.UnicodeString(16, []rune("九七二人入八力十下三千上口土夕大")) if err != nil { log.Fatal(err) } fmt.Println(s) // => 八三口上土土七入力夕人力下三上力 }
Output:
func Upper ¶
Upper generates a random uppercase alphabetic string of n length.
The returned string may contain A-Z.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.Upper(16) if err != nil { log.Fatal(err) } fmt.Println(s) // => MCZEGPWGYKNUEDCK }
Output:
func UpperNumeric ¶
UpperNumeric generates a random uppercase alphanumeric string of n length.
The returned string may contain A-Z and 0-9.
Example ¶
package main import ( "fmt" "log" "github.com/jimeh/rands" ) func main() { s, err := rands.UpperNumeric(16) if err != nil { log.Fatal(err) } fmt.Println(s) // => 6LLPBBUW77B26X2X }
Output:
Types ¶
This section is empty.
Directories ¶
Path | Synopsis |
---|---|
Package randsmust provides a suite of functions that use crypto/rand to generate cryptographically secure random strings in various formats, as well as ints and bytes.
|
Package randsmust provides a suite of functions that use crypto/rand to generate cryptographically secure random strings in various formats, as well as ints and bytes. |