RandomString (security/random.go)

pocketbase · tools/security/random.go · go · MIT
security, tag:knn-classified

Module exposing: RandomString, RandomStringWithAlphabet, PseudorandomString, PseudorandomStringWithAlphabet

// Source: https://github.com/pocketbase/pocketbase | License: MIT | Copyright 2022 - present, Gani Georgiev
// Provided via justclaw.it.com; keep this notice with the code.
package security

import (
	cryptoRand "crypto/rand"
	"math/big"
	mathRand "math/rand/v2"
)

const defaultRandomAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

// RandomString generates a cryptographically random string with the specified length.
//
// The generated string matches [A-Za-z0-9]+ and it's transparent to URL-encoding.
func RandomString(length int) string {
	return RandomStringWithAlphabet(length, defaultRandomAlphabet)
}

// RandomStringWithAlphabet generates a cryptographically random string
// with the specified length and characters set.
//
// It panics if for some reason ra

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key