rand

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package rand provides the random string

Index

Constants

View Source
const DefaultSaltSize = 16

DefaultSaltSize is the default length of the salt string.

Variables

View Source
var (
	Digit             = NewRand(KindDigit)
	LowerCase         = NewRand(KindLowerCase)
	UpperCase         = NewRand(KindUpperCase)
	Symbol            = NewRand(KindSymbol)
	LowerAndUpperCase = NewRand(KindLowerCase | KindUpperCase)
	DigitAndLowerCase = NewRand(KindDigit | KindLowerCase)
	DigitAndUpperCase = NewRand(KindDigit | KindUpperCase)
	All               = NewRand(KindAll)
)

predefined charsets for different random types

Functions

func GenerateRandom added in v0.0.18

func GenerateRandom(length int) string

GenerateRandom generates a random salt string of specified length.

Parameters:

length int - the desired length of the salt string

Return value:

string - the generated random salt string

func GenerateSalt added in v0.0.18

func GenerateSalt() string

GenerateSalt generates a random salt string of specified length.

Return value:

string - the generated random salt string

Types

type Kind

type Kind int
const (
	KindDigit     Kind = 1 << iota
	KindLowerCase Kind = 1 << iota
	KindUpperCase Kind = 1 << iota
	KindSymbol    Kind = 1 << iota
	KindAll       Kind = KindDigit | KindLowerCase | KindUpperCase | KindSymbol
	KindCustom    Kind = 0xFF
)

Define a series of character type constants representing different categories of characters. KindDigit - Represents digit characters. KindLowerCase - Represents lowercase letter characters. KindUpperCase - Represents uppercase letter characters. KindSymbol - Represents symbol characters. KindAll - Represents the collective set of all character types, including digits, lowercase, uppercase, and symbols.

func (Kind) String

func (k Kind) String() string

type Rand

type Rand struct {
	// contains filtered or unexported fields
}

func CustomRand

func CustomRand(charset string) *Rand

CustomRand creates a new Rand object with a custom charset.

Parameters: - charset: a string representing the custom charset.

Return: - a pointer to a Rand object.

func NewRand

func NewRand(kind Kind) *Rand

NewRand generates a new Rand object based on the given random type.

Parameters: - rndType: an integer representing the random type.

Return: - a pointer to a Rand object.

func (Rand) RandBytes

func (r Rand) RandBytes(size int) []byte

RandBytes generates a random byte slice of given size using the given charset.

Parameters: - size: the length of the byte slice to be generated.

Return: - []byte: the generated random byte slice.

func (Rand) Read added in v0.0.18

func (r Rand) Read(p []byte) (n int, err error)

The Read method populates the given byte slice by randomly selecting characters.

Parameters: p []byte: The byte slice to be populated.

Return values: n int: The number of bytes actually populated. err error: An error encountered during population, always nil in this implementation.

Jump to

Keyboard shortcuts

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