crypto

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: BSD-2-Clause Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateSecureToken

func GenerateSecureToken(byteSize int, stringLength int) (string, error)

GenerateSecureToken generates a cryptographically secure random token encoded as a base64 URL-safe string. If stringLength > 0, generates a token of exactly that character length (truncated after encoding). If stringLength is 0, uses byteSize to determine the number of random bytes to generate before encoding. If both are 0, defaults to 32 bytes.

Maximum byte size is 96, which produces a 128-character encoded token. Returns an error if stringLength exceeds 128 or byteSize exceeds 96.

Example (ByteSize)
package main

import (
	"fmt"

	"github.com/SUNET/vc/pkg/crypto"
)

func main() {
	token, err := crypto.GenerateSecureToken(10, 0)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	// 10 bytes -> 14 base64url characters (ceil(10*4/3) without padding)
	fmt.Println("length:", len(token))
}
Output:
length: 14
Example (DefaultSize)
package main

import (
	"fmt"

	"github.com/SUNET/vc/pkg/crypto"
)

func main() {
	token, err := crypto.GenerateSecureToken(0, 0)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	// Default: 32 bytes -> 43 base64url characters
	fmt.Println("length:", len(token))
}
Output:
length: 43
Example (ExactStringLength)
package main

import (
	"fmt"

	"github.com/SUNET/vc/pkg/crypto"
)

func main() {
	token, err := crypto.GenerateSecureToken(0, 16)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Println("length:", len(token))
}
Output:
length: 16
Example (ExceedsMaxByteSize)
package main

import (
	"fmt"

	"github.com/SUNET/vc/pkg/crypto"
)

func main() {
	_, err := crypto.GenerateSecureToken(100, 0)
	fmt.Println("error:", err)
}
Output:
error: requested byteSize 100 exceeds maximum supported size of 96 bytes
Example (ExceedsMaxLength)
package main

import (
	"fmt"

	"github.com/SUNET/vc/pkg/crypto"
)

func main() {
	_, err := crypto.GenerateSecureToken(0, 200)
	fmt.Println("error:", err)
}
Output:
error: requested stringLength 200 exceeds maximum supported length of 128

Types

This section is empty.

Jump to

Keyboard shortcuts

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