Documentation
¶
Index ¶
- func Base64Decode(encoded string) (string, error)
- func Base64Encode(data string) string
- func Base64URLDecode(encoded string) (string, error)
- func Base64URLEncode(data string) string
- func CalculateOffset(page, pageSize int) int
- func Chunk(slice interface{}, chunkSize int) interface{}
- func Clamp(value, min, max int) int
- func Contains(s, substr string) bool
- func ContainsAny(s string, substrings []string) bool
- func Filter(slice interface{}, predicate func(interface{}) bool) interface{}
- func FormatBytes(bytes int64) string
- func FormatDuration(d time.Duration) string
- func GenerateKey() (string, error)
- func GenerateRandomBytes(length int) ([]byte, error)
- func GenerateRandomString(length int) (string, error)
- func GenerateSecret() (string, error)
- func GetPaginationLinks(meta PaginationMeta, baseURL string) map[string]interface{}
- func GetSortString(sortFields []SortField) string
- func IsEmpty(s string) bool
- func IsNotEmpty(s string) bool
- func IsStrongPassword(password string) bool
- func IsValidEmail(email string) bool
- func IsValidPhoneNumber(phone string) bool
- func IsValidURL(url string) bool
- func IsValidUUID(uuid string) bool
- func Map(slice interface{}, mapper func(interface{}) interface{}) interface{}
- func Max(a, b int) int
- func Min(a, b int) int
- func ParseBool(s string, defaultValue bool) bool
- func ParseFloat(s string, defaultValue float64) float64
- func ParseInt(s string, defaultValue int) int
- func Reduce(slice interface{}, initial interface{}, ...) interface{}
- func RemoveDuplicates(slice []string) []string
- func Round(value float64, places int) float64
- func Sign(data, secret string) string
- func SliceContains(slice interface{}, value interface{}) bool
- func SortSlice(slice interface{}, sortFields []SortField) error
- func SortStructs(slice interface{}, sortFields []SortField) error
- func TimeAgo(t time.Time) string
- func Truncate(s string, length int) string
- func TruncateWords(s string, wordCount int) string
- func ValidatePaginationRequest(req *PaginationRequest, config *PaginationConfig) error
- func ValidateSortFields(sortFields []SortField, config *SortConfig) error
- func Verify(password, hash string, config *HashConfig) (bool, error)
- func VerifySignature(data, signature, secret string) bool
- type DecryptResult
- type EncryptResult
- type EncryptionConfig
- type HashAlgorithm
- type HashConfig
- type HashResult
- type PaginationConfig
- type PaginationMeta
- type PaginationRequest
- type PaginationResponse
- type PaginationResponseWithLinks
- type SortConfig
- type SortField
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Base64Decode ¶
Base64Decode decodes a base64 string
func Base64URLDecode ¶
Base64URLDecode decodes a base64 URL-safe string
func Base64URLEncode ¶
Base64URLEncode encodes a string to base64 URL-safe encoding
func CalculateOffset ¶
CalculateOffset calculates the offset for database queries
func Chunk ¶
func Chunk(slice interface{}, chunkSize int) interface{}
Chunk splits a slice into chunks of specified size
func ContainsAny ¶
ContainsAny checks if a string contains any of the substrings
func Filter ¶
func Filter(slice interface{}, predicate func(interface{}) bool) interface{}
Filter filters a slice based on a predicate function
func FormatBytes ¶
FormatBytes formats bytes into human-readable format
func FormatDuration ¶
FormatDuration formats a duration into human-readable format
func GenerateRandomBytes ¶
GenerateRandomBytes generates random bytes of specified length
func GenerateRandomString ¶
GenerateRandomString generates a random string of specified length
func GenerateSecret ¶
GenerateSecret generates a random secret for HMAC
func GetPaginationLinks ¶
func GetPaginationLinks(meta PaginationMeta, baseURL string) map[string]interface{}
GetPaginationLinks generates pagination links for API responses
func GetSortString ¶
GetSortString converts sort fields back to query string format
func IsNotEmpty ¶
IsNotEmpty checks if a string is not empty and contains non-whitespace characters
func IsStrongPassword ¶
IsStrongPassword checks if a password meets strength requirements
func IsValidEmail ¶
IsValidEmail checks if a string is a valid email address
func IsValidPhoneNumber ¶
IsValidPhoneNumber checks if a string is a valid phone number
func IsValidUUID ¶
IsValidUUID checks if a string is a valid UUID
func Map ¶
func Map(slice interface{}, mapper func(interface{}) interface{}) interface{}
Map applies a function to each element of a slice
func ParseFloat ¶
ParseFloat safely parses a string to float64 with default value
func Reduce ¶
func Reduce(slice interface{}, initial interface{}, reducer func(interface{}, interface{}) interface{}) interface{}
Reduce reduces a slice to a single value
func RemoveDuplicates ¶
RemoveDuplicates removes duplicate strings from a slice
func SliceContains ¶
func SliceContains(slice interface{}, value interface{}) bool
Contains checks if a slice contains a value
func SortStructs ¶
SortStructs sorts a slice of structs by multiple fields
func TruncateWords ¶
TruncateWords truncates a string to the specified number of words
func ValidatePaginationRequest ¶
func ValidatePaginationRequest(req *PaginationRequest, config *PaginationConfig) error
ValidatePaginationRequest validates pagination parameters
func ValidateSortFields ¶
func ValidateSortFields(sortFields []SortField, config *SortConfig) error
ValidateSortFields validates that all sort fields are allowed
func Verify ¶
func Verify(password, hash string, config *HashConfig) (bool, error)
Verify verifies a password against a hash
func VerifySignature ¶
VerifySignature verifies an HMAC signature
Types ¶
type DecryptResult ¶
type DecryptResult struct {
Data string `json:"data"` // Decrypted data
Valid bool `json:"valid"` // Whether the signature is valid
Error string `json:"error,omitempty"` // Error message if decryption failed
}
DecryptResult represents the result of a decryption operation
func Decrypt ¶
func Decrypt(encryptedData, signature, nonce string, config *EncryptionConfig) (*DecryptResult, error)
Decrypt decrypts data using AES-256-GCM with HMAC verification
func DecryptWithPassword ¶
func DecryptWithPassword(encryptedData, signature, nonce, password string) (*DecryptResult, error)
DecryptWithPassword decrypts data using a password-derived key
type EncryptResult ¶
type EncryptResult struct {
Data string `json:"data"` // Encrypted data (base64 encoded)
Signature string `json:"signature"` // HMAC signature (hex encoded)
Nonce string `json:"nonce"` // Nonce used for encryption (hex encoded)
}
EncryptResult represents the result of an encryption operation
func Encrypt ¶
func Encrypt(data string, config *EncryptionConfig) (*EncryptResult, error)
Encrypt encrypts data using AES-256-GCM with HMAC signing
func EncryptWithPassword ¶
func EncryptWithPassword(data, password string) (*EncryptResult, error)
EncryptWithPassword encrypts data using a password-derived key
type EncryptionConfig ¶
type EncryptionConfig struct {
Key string `json:"key"` // 32-byte key for AES-256
Secret string `json:"secret"` // Secret for HMAC signing
}
EncryptionConfig holds configuration for encryption
func NewEncryptionConfig ¶
func NewEncryptionConfig() (*EncryptionConfig, error)
NewEncryptionConfig creates a new encryption configuration with a random key
func NewEncryptionConfigWithKey ¶
func NewEncryptionConfigWithKey(key, secret string) *EncryptionConfig
NewEncryptionConfigWithKey creates a new encryption configuration with provided key and secret
type HashAlgorithm ¶
type HashAlgorithm string
HashAlgorithm represents the available hashing algorithms
const ( Bcrypt HashAlgorithm = "bcrypt" Argon2 HashAlgorithm = "argon2" SHA256 HashAlgorithm = "sha256" SHA512 HashAlgorithm = "sha512" HMAC HashAlgorithm = "hmac" )
type HashConfig ¶
type HashConfig struct {
Algorithm HashAlgorithm `json:"algorithm"`
Cost int `json:"cost"` // For bcrypt
Memory uint32 `json:"memory"` // For argon2 (in KB)
Time uint32 `json:"time"` // For argon2
Threads uint8 `json:"threads"` // For argon2
KeyLength uint32 `json:"key_length"` // For argon2
Secret string `json:"secret"` // For HMAC
}
HashConfig holds configuration for hashing
func DefaultHashConfig ¶
func DefaultHashConfig() *HashConfig
DefaultHashConfig returns the default hash configuration
type HashResult ¶
type HashResult struct {
Hash string `json:"hash"`
Algorithm HashAlgorithm `json:"algorithm"`
Salt string `json:"salt,omitempty"`
Config *HashConfig `json:"config,omitempty"`
}
HashResult represents the result of a hashing operation
func Hash ¶
func Hash(password string, config *HashConfig) (*HashResult, error)
Hash hashes a password using the specified algorithm
type PaginationConfig ¶
type PaginationConfig struct {
DefaultPageSize int `json:"default_page_size"` // Default page size
MaxPageSize int `json:"max_page_size"` // Maximum allowed page size
MinPageSize int `json:"min_page_size"` // Minimum allowed page size
DefaultSortBy string `json:"default_sort_by"` // Default sort field
DefaultSortDir string `json:"default_sort_dir"` // Default sort direction
}
PaginationConfig holds configuration for pagination
func DefaultPaginationConfig ¶
func DefaultPaginationConfig() *PaginationConfig
DefaultPaginationConfig returns the default pagination configuration
type PaginationMeta ¶
type PaginationMeta struct {
Page int `json:"page"` // Current page
PageSize int `json:"page_size"` // Items per page
Total int64 `json:"total"` // Total number of items
TotalPages int `json:"total_pages"` // Total number of pages
HasNext bool `json:"has_next"` // Has next page
HasPrev bool `json:"has_prev"` // Has previous page
NextPage *int `json:"next_page"` // Next page number (null if no next page)
PrevPage *int `json:"prev_page"` // Previous page number (null if no prev page)
}
PaginationMeta represents pagination metadata
func CalculatePaginationMeta ¶
func CalculatePaginationMeta(page, pageSize int, total int64) PaginationMeta
CalculatePaginationMeta calculates pagination metadata
type PaginationRequest ¶
type PaginationRequest struct {
Page int `json:"page"` // Page number (1-based)
PageSize int `json:"page_size"` // Items per page
SortBy string `json:"sort_by"` // Field to sort by
SortDir string `json:"sort_dir"` // Sort direction (asc/desc)
}
PaginationRequest represents pagination parameters from request
func ParsePaginationRequest ¶
func ParsePaginationRequest(pageStr, pageSizeStr, sortBy, sortDir string, config *PaginationConfig) *PaginationRequest
ParsePaginationRequest parses pagination parameters from query parameters
type PaginationResponse ¶
type PaginationResponse struct {
Data interface{} `json:"data"` // The actual data
Meta PaginationMeta `json:"meta"` // Pagination metadata
}
PaginationResponse represents a paginated response
func CreatePaginationResponse ¶
func CreatePaginationResponse(data interface{}, page, pageSize int, total int64) *PaginationResponse
CreatePaginationResponse creates a paginated response
type PaginationResponseWithLinks ¶
type PaginationResponseWithLinks struct {
Data interface{} `json:"data"`
Meta PaginationMeta `json:"meta"`
Links map[string]interface{} `json:"links"`
}
PaginationResponseWithLinks represents a paginated response with links
func CreatePaginationResponseWithLinks ¶
func CreatePaginationResponseWithLinks(data interface{}, page, pageSize int, total int64, baseURL string) *PaginationResponseWithLinks
CreatePaginationResponseWithLinks creates a paginated response with navigation links
type SortConfig ¶
type SortConfig struct {
AllowedFields []string `json:"allowed_fields"` // Fields that can be sorted
DefaultField string `json:"default_field"` // Default sort field
DefaultDir string `json:"default_dir"` // Default sort direction
}
SortConfig holds configuration for sorting
func CreateSortConfig ¶
func CreateSortConfig(allowedFields []string, defaultField, defaultDir string) *SortConfig
CreateSortConfig creates a sort configuration with allowed fields
func DefaultSortConfig ¶
func DefaultSortConfig() *SortConfig
DefaultSortConfig returns the default sort configuration
type SortField ¶
type SortField struct {
Field string `json:"field"` // Field name to sort by
Direction string `json:"direction"` // Sort direction (asc/desc)
}
SortField represents a single sort field
func ParseSortRequest ¶
func ParseSortRequest(sortStr string, config *SortConfig) ([]SortField, error)
ParseSortRequest parses sort parameters from query string