utils

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 11 Imported by: 8

README

go-utils

Simple utilities

Documentation

Index

Constants

View Source
const (
	PasswordHashTimesName   = "PASSWORD_HASH_TIMES"
	PasswordHashMemoryName  = "PASSWORD_HASH_MEMORY"
	PasswordHashThreadsName = "PASSWORD_HASH_THREADS"
	PasswordHashKeyLenName  = "PASSWORD_HASH_KEY_LENGTH"
	PasswordHashSaltLenName = "PASSWORD_HASH_SALT_LENGTH"
)
View Source
const (
	AlphaNum  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	Printable = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;:,.<>?/`~"
)

Variables

View Source
var (
	ErrInvalidHashFormat    = errors.New("invalid hash format")
	ErrInvalidHashVersion   = errors.New("invalid hash version")
	ErrInvalidHashAlgorithm = errors.New("invalid hash algorithm")
)

Functions

func ComparePassword added in v0.0.4

func ComparePassword(password, encodedHash string) (bool, error)

ComparePassword compares a password with an encoded hash to check if they match.

func GetEnv added in v0.0.3

func GetEnv(key, defaultValue string, nonEmpty bool) string

GetEnv returns the value of the environment variable named by the key. It is guaranteed to return the `defaultValue` if the environment variable is not found. If the environment variable is found, it is guaranteed to return the `defaultValue` if the environment variable is empty and `nonEmpty` is `true`.

func GetEnvWithDefault

func GetEnvWithDefault(key, defaultValue string) string

GetEnvWithDefault returns the value of the environment variable named by the key. It is guaranteed to return the default value if the environment variable is not found or is empty.

func GetEnvWithDefaultNE added in v0.0.3

func GetEnvWithDefaultNE(key, defaultValue string) string

GetEnvWithDefaultNE returns the value of the environment variable named by the key. It is guaranteed to return the default value if the environment variable is not found. If the environment variable is found, it is guaranteed to return the default value if the environment variable is empty.

func HashPassword added in v0.0.4

func HashPassword(password string) (string, error)

HashPassword generates a new password hash using the argon2id algorithm.

func HashPasswordWithParams added in v0.0.4

func HashPasswordWithParams(password string, params PasswordHashParams) (
	string, error,
)

HashPassword generates a new password hash using the argon2id algorithm.

func JoinInteger added in v0.0.4

func JoinInteger[A interface{ ~[]T }, T Integer](a A, sep string) string

JoinInteger converts a slice of integers to a string, joining them with the separator.

func MapToType added in v0.0.4

func MapToType[T any](val any) (T, error)

MapToType converts an element of interface{} to an element of specific type, returning an error if the conversion is not possible.

func PanicIfError added in v0.0.2

func PanicIfError(err error)

PanicIfError panics if the error is not nil.

func Pluck added in v0.0.4

func Pluck[I interface{ ~[]V }, V any, T any](
	a I, fn func(V) T,
) []T

Pluck extracts a list of values from a slice of structs. The predicate function fn is applied to each element of the slice, and the result is stored in a new slice.

func RandomAlphaNum added in v0.0.4

func RandomAlphaNum(length int) (string, error)

func RandomPrintable added in v0.0.4

func RandomPrintable(length int) (string, error)

func RandomString added in v0.0.4

func RandomString(dict string, length int) (string, error)

func SliceFindFunc added in v0.0.4

func SliceFindFunc[I any](a []I, fn func(I) bool) I

SliceFindFunc returns the first element in the slice that satisfies the predicate function, or the zero value of the element type if none is found.

func SliceMapFunc added in v0.0.4

func SliceMapFunc[I, O any](a []I, fn func(I) (O, error)) ([]O, error)

SliceMapFunc applies the predicate function to each element of the slice, returning a new slice with the results. If the predicate function returns an error, the function stops and returns the error.

func StringContainsAny added in v0.0.4

func StringContainsAny(s string, subs []string) bool

func StringIndexOfAny added in v0.0.4

func StringIndexOfAny(s string, subs []string) int

Types

type Integer added in v0.0.4

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Integer is a type constraint that includes all integer types

type PasswordHashParams added in v0.0.4

type PasswordHashParams struct {
	// Number of iterations
	Times uint32
	// Amount of memory to use, in KB
	Memory uint32
	// Parallelism, number of threads to use
	Threads uint8
	// Key length
	KeyLen uint32
	// Salt length
	SaltLen uint32
}

func DefaultPasswordHashParams added in v0.0.4

func DefaultPasswordHashParams() (*PasswordHashParams, error)

type SimpleTaggedLog added in v0.0.2

type SimpleTaggedLog struct {
	Debug bool
	// contains filtered or unexported fields
}

func NewDebugLogger added in v0.0.2

func NewDebugLogger() SimpleTaggedLog

func NewLogger added in v0.0.2

func NewLogger() SimpleTaggedLog

func WrapLogger added in v0.0.2

func WrapLogger(logger *lg.Logger, debug bool) SimpleTaggedLog

func (SimpleTaggedLog) Debugf added in v0.0.2

func (l SimpleTaggedLog) Debugf(format string, args ...interface{})

func (SimpleTaggedLog) Errorf added in v0.0.2

func (l SimpleTaggedLog) Errorf(format string, args ...interface{})

func (SimpleTaggedLog) Infof added in v0.0.2

func (l SimpleTaggedLog) Infof(format string, args ...interface{})

func (SimpleTaggedLog) PanicIfError added in v0.0.2

func (l SimpleTaggedLog) PanicIfError(err error)

func (SimpleTaggedLog) Panicf added in v0.0.2

func (l SimpleTaggedLog) Panicf(format string, args ...interface{})

Jump to

Keyboard shortcuts

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