randsmust

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2021 License: MIT Imports: 1 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alphabetic

func Alphabetic(n int) string

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

func Alphanumeric(n int) string

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

func Base64(n int) string

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

func Base64URL(n int) string

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

func Bytes(n int) []byte

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

func DNSLabel(n int) string

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

func Hex(n int) string

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

func Int(max int) 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

func Int64(max int64) 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

func Lower(n int) string

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

func LowerNumeric(n int) string

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

func Numeric(n int) string

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

func String(n int, alphabet string) 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

func UnicodeString(n int, alphabet []rune) string

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

func Upper(n int) string

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

func UpperNumeric(n int) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL