security

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 14 Imported by: 3

Documentation

Overview

Package security provides cryptographic primitives including AES-GCM encryption, password hashing with bcrypt, HMAC hashing, secure key generation, PKCE generation, and environment variable parsing utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(ciphertext []byte, key [32]byte) ([]byte, error)

Decrypt takes an encrypted byte slice (ciphertext) and a 256-bit AES key, and decrypts the ciphertext using AES-GCM.

func Encrypt

func Encrypt(plaintext []byte, key [32]byte) []byte

Encrypt takes an input byte slice (plaintext) and encrypts it using AES-GCM. It returns the encrypted data (ciphertext) and the key used for encryption.

func GenerateID added in v0.1.71

func GenerateID() string

GenerateID generates a unique ID using a secure random key.

func GenerateKey

func GenerateKey() [32]byte

GenerateKey generates a 256-bit (32-byte) random key for AES encryption. It uses a cryptographically secure random number generator.

func GeneratePKCE added in v0.1.87

func GeneratePKCE() (string, string)

GeneratePKCE generates a OAuth 2.0 PKCE challenge by using a random string.

func Getenv added in v0.1.7

func Getenv(key string) [32]byte

Getenv retrieves an environment variable by the given key, interprets its value as a hexadecimal string, and decodes it into a 32-byte array. If the environment variable is not set, the value is not a valid hex string, or the decoded byte length is not 32, the function returns an empty array.

func Hash

func Hash(tag string, data []byte) []byte

Hash generates an HMAC hash using the SHA-512/256 algorithm.

func IsPasswordValid

func IsPasswordValid(ciphertext, plaintext []byte) bool

IsPasswordValid checks if a given plaintext password matches a hashed password.

func ParseBoolOrDefault added in v0.2.37

func ParseBoolOrDefault(key string, def bool) bool

ParseBoolOrDefault parses the value of the environment variable with the given key as a boolean. If the value is not set or cannot be parsed, the default boolean is returned.

func ParseDurationOrDefault added in v0.2.36

func ParseDurationOrDefault(key string, def time.Duration) time.Duration

ParseDurationOrDefault parses the value of the environment variable with the given key as a duration. If the value is not set or cannot be parsed, the default duration is returned.

func ParseFloatOrDefault added in v0.2.37

func ParseFloatOrDefault(key string, def float64) float64

ParseFloatOrDefault parses the value of the environment variable with the given key as a float. If the value is not set or cannot be parsed, the default float is returned.

func ParseIntOrDefault added in v0.2.36

func ParseIntOrDefault(key string, def int) int

ParseIntOrDefault parses the value of the environment variable with the given key as an integer. If the value is not set or cannot be parsed, the default integer is returned.

func ParseStringOrDefault added in v0.2.37

func ParseStringOrDefault(key string, def string) string

ParseStringOrDefault parses the value of the environment variable with the given key as a string. If the value is not set or cannot be parsed, the default string is returned.

func Password

func Password(plaintext []byte) ([]byte, error)

Password hashes a plaintext password using bcrypt with a cost of 14.

Types

This section is empty.

Jump to

Keyboard shortcuts

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