Documentation ¶
Overview ¶
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.
All functions which produce strings from a alphabet of characters uses rand.Int() to ensure a uniform distribution of all possible values.
randsmust is specifically intended as an alternative to rands for use in tests. All functions return a single value, and panic in the event of an error. This makes them easy to use when building structs in test cases that need random data.
For production code, make sure to use the rands package and check returned errors.
Index ¶
- func Alphabetic(n int) string
- func Alphanumeric(n int) string
- func Base64(n int) string
- func Base64URL(n int) string
- func Bytes(n int) []byte
- func DNSLabel(n int) string
- func Hex(n int) string
- func Int(max int) int
- func Int64(max int64) int64
- func Lower(n int) string
- func LowerNumeric(n int) string
- func Numeric(n int) string
- func String(n int, alphabet string) string
- func UUID() string
- func UnicodeString(n int, alphabet []rune) string
- func Upper(n int) string
- func UpperNumeric(n int) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.Alphabetic(16) fmt.Println(s) // => RLaRaTVqcrxvNkiz }
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" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.Alphanumeric(16) fmt.Println(s) // => mjT119HdPslVfvUE }
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" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.Base64(16) fmt.Println(s) // => rGnZOxJunCd5h+piBpOfDA== }
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" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.Base64URL(16) fmt.Println(s) // => NlXKmutou2knLU8q7Hlp5Q }
Output:
func Bytes ¶
Bytes generates a byte slice of n number of random bytes.
Example ¶
package main import ( "fmt" "github.com/jimeh/rands/randsmust" ) func main() { b := randsmust.Bytes(8) fmt.Printf("%+v\n", b) // => [6 99 106 54 163 188 28 152] }
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" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.DNSLabel(16) fmt.Println(s) // => urqkt-remuwz5083 }
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" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.Hex(16) fmt.Println(s) // => 1013ec67a802be177d3e37f46951e97f }
Output:
func Int ¶
Int generates a random int ranging between 0 and max.
Example ¶
package main import ( "fmt" "github.com/jimeh/rands/randsmust" ) func main() { n := randsmust.Int(2147483647) fmt.Printf("%d\n", n) // => 1616989970 }
Output:
func Int64 ¶
Int64 generates a random int64 ranging between 0 and max.
Example ¶
package main import ( "fmt" "github.com/jimeh/rands/randsmust" ) func main() { n := randsmust.Int64(int64(9223372036854775807)) fmt.Printf("%d\n", n) // => 1599573251306894157 }
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" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.Lower(16) fmt.Println(s) // => aoybqdwigyezucjy }
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" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.LowerNumeric(16) fmt.Println(s) // => hs8l2l0750med3g2 }
Output:
func Numeric ¶
Numeric generates a random numeric string of n length.
The returned string may contain 0-9.
Example ¶
package main import ( "fmt" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.Numeric(16) fmt.Println(s) // => 3126402104379869 }
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" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.String(16, "abcdefABCDEF") fmt.Println(s) // => cbdCAbABECaADcaB }
Output:
func UUID ¶
func UUID() string
UUID returns a random UUID v4 in string format as defined by RFC 4122, section 4.4.
Example ¶
package main import ( "fmt" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.UUID() fmt.Println(s) // => 5baa35a6-9a46-49b4-91d0-9530173e118d }
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" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.UnicodeString(16, []rune("九七二人入八力十下三千上口土夕大")) 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" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.Upper(16) fmt.Println(s) // => CANJDLMHANPQNXUE }
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" "github.com/jimeh/rands/randsmust" ) func main() { s := randsmust.UpperNumeric(16) fmt.Println(s) // => EERZHC96KOIRU9DM }
Output:
Types ¶
This section is empty.