Documentation
¶
Overview ¶
Package utils provides common utilities for hashing, encryption, encoding, string manipulation, validation, time, pagination, sorting, file operations, and CLI helpers.
Hashing: HashPassword, CheckPasswordHash (bcrypt); HashPasswordArgon2, CheckPasswordArgon2 (Argon2); SHA256, SHA512, HMACSHA256, HMACSHA512; MD5File, SHA1File, SHA256File for files.
Encryption: EncryptAES256GCM, DecryptAES256GCM (AES-256-GCM, 32-byte key).
Encoding: EncodeBase64, DecodeBase64; EncodeBase64URL, DecodeBase64URL; EncodeBase64URLSafe, DecodeBase64URLSafe.
String: GenerateRandomString, GenerateRandomBytes, Slugify, Truncate, TruncateWords, Capitalize, TitleCase, SnakeCase, CamelCase, PascalCase, RemoveDuplicates, IsEmpty, IsNotEmpty, GenerateUUID, GenerateShortUUID.
Validation: IsValidEmail, IsValidPhone, IsValidURL, IsStrongPassword, IsValidUsername, IsValidSlug.
Time: ParseDuration, FormatDuration, GetTimezoneOffset, FormatTime, TimeAgo, StartOfDay, EndOfDay, StartOfWeek, EndOfWeek, StartOfMonth, EndOfMonth.
Pagination: PaginationMeta, PaginationLinks, PaginationResponse, NewPagination, GeneratePaginationLinks, ParsePaginationParams (Fiber).
Sorting: SortField, ParseSortParams, SortSlice (reflect-based).
File: GetFileExtension, GetFileSize, FileExists, EnsureDir, CopyFile, GetMIMEType, FormatFileSize.
CLI: PrintSuccess, PrintError, PrintWarning, PrintInfo, AskInput, AskConfirmation, ShowProgress.
Index ¶
- func AskConfirmation(prompt string) bool
- func AskInput(prompt string) string
- func CamelCase(s string) string
- func Capitalize(s string) string
- func CheckPasswordArgon2(password, hash string) bool
- func CheckPasswordHash(password, hash string) bool
- func CopyFile(src, dst string) error
- func DecodeBase64(data string) (string, error)
- func DecodeBase64URL(data string) (string, error)
- func DecodeBase64URLSafe(data string) (string, error)
- func DecryptAES256GCM(ciphertext, key string) (string, error)
- func EncodeBase64(data string) string
- func EncodeBase64URL(data string) string
- func EncodeBase64URLSafe(data string) string
- func EncryptAES256GCM(plaintext, key string) (string, error)
- func EndOfDay(t time.Time) time.Time
- func EndOfMonth(t time.Time) time.Time
- func EndOfWeek(t time.Time) time.Time
- func EnsureDir(dirPath string) error
- func FileExists(filePath string) bool
- func FormatDuration(d time.Duration) string
- func FormatFileSize(bytes int64) string
- func FormatTime(t time.Time, format string) string
- func GenerateRandomBytes(length int) []byte
- func GenerateRandomString(length int) string
- func GenerateShortUUID() string
- func GenerateUUID() string
- func GetFileExtension(filename string) string
- func GetFileSize(filePath string) (int64, error)
- func GetMIMEType(filePath string) string
- func GetTimezoneOffset(tz string) (int, error)
- func HMACSHA256(data, key string) string
- func HMACSHA512(data, key string) string
- func HashPassword(password string) (string, error)
- func HashPasswordArgon2(password string) (string, error)
- func IsEmpty(s string) bool
- func IsNotEmpty(s string) bool
- func IsStrongPassword(password string) bool
- func IsValidEmail(email string) bool
- func IsValidPhone(phone string) bool
- func IsValidSlug(slug string) bool
- func IsValidURL(url string) bool
- func IsValidUsername(username string) bool
- func MD5File(filePath string) (string, error)
- func ParseDuration(s string) (time.Duration, error)
- func ParsePaginationParams(c *fiber.Ctx) (page, perPage int)
- func PascalCase(s string) string
- func PrintError(message string)
- func PrintInfo(message string)
- func PrintSuccess(message string)
- func PrintWarning(message string)
- func RemoveDuplicates(slice []string) []string
- func SHA1File(filePath string) (string, error)
- func SHA256(data string) string
- func SHA256File(filePath string) (string, error)
- func SHA512(data string) string
- func ShowProgress(current, total int, message string)
- func Slugify(s string) string
- func SnakeCase(s string) string
- func SortSlice(slice interface{}, fields []SortField)
- func StartOfDay(t time.Time) time.Time
- func StartOfMonth(t time.Time) time.Time
- func StartOfWeek(t time.Time) time.Time
- func TimeAgo(t time.Time) string
- func TitleCase(s string) string
- func Truncate(s string, length int) string
- func TruncateWords(s string, wordCount int) string
- type PaginationLinks
- type PaginationMeta
- type PaginationResponse
- type SortField
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AskConfirmation ¶ added in v1.0.0
AskConfirmation returns true if user answers y/yes (case-insensitive).
func Capitalize ¶ added in v1.0.0
Capitalize uppercases the first rune of s.
func CheckPasswordArgon2 ¶ added in v1.0.0
CheckPasswordArgon2 returns true if password matches the Argon2 hash (combined salt+hash hex).
func CheckPasswordHash ¶ added in v1.0.0
CheckPasswordHash returns true if password matches the bcrypt hash.
func DecodeBase64 ¶ added in v1.0.0
DecodeBase64 decodes standard base64 data.
func DecodeBase64URL ¶ added in v1.0.0
DecodeBase64URL decodes URL-safe base64 data.
func DecodeBase64URLSafe ¶ added in v1.0.0
DecodeBase64URLSafe decodes URL-safe base64 (adds padding if needed).
func DecryptAES256GCM ¶ added in v1.0.0
DecryptAES256GCM decrypts base64-encoded ciphertext with the given 32-byte key (AES-256-GCM).
func EncodeBase64 ¶ added in v1.0.0
EncodeBase64 returns standard base64 encoding of data.
func EncodeBase64URL ¶ added in v1.0.0
EncodeBase64URL returns URL-safe base64 encoding (no padding issues in URLs).
func EncodeBase64URLSafe ¶ added in v1.0.0
EncodeBase64URLSafe returns URL-safe base64 without trailing padding.
func EncryptAES256GCM ¶ added in v1.0.0
EncryptAES256GCM encrypts plaintext with the given 32-byte key using AES-256-GCM; returns base64-encoded ciphertext.
func EndOfMonth ¶ added in v1.0.0
EndOfMonth returns the last day of t's month at 23:59:59.
func FileExists ¶ added in v1.0.0
FileExists returns true if filePath exists.
func FormatDuration ¶
FormatDuration formats d in a human-readable short form (s, m, h, d).
func FormatFileSize ¶ added in v1.0.0
FormatFileSize formats a byte count as human-readable (B, KB, MB, GB, TB, PB).
func FormatTime ¶ added in v1.0.0
FormatTime formats t according to the named format (rfc3339, rfc822, rfc1123, unix, unix_milli, unix_micro, unix_nano) or custom layout.
func GenerateRandomBytes ¶
GenerateRandomBytes returns n random bytes from crypto/rand.
func GenerateRandomString ¶
GenerateRandomString returns a random string of the given length from alphanumeric charset.
func GenerateShortUUID ¶ added in v1.0.0
func GenerateShortUUID() string
GenerateShortUUID returns the first 12 characters of a UUID without hyphens.
func GenerateUUID ¶ added in v1.0.0
func GenerateUUID() string
GenerateUUID returns a new UUID v4 string.
func GetFileExtension ¶ added in v1.0.0
GetFileExtension returns the lowercased file extension (e.g. ".txt").
func GetFileSize ¶ added in v1.0.0
GetFileSize returns the size in bytes of the file at filePath.
func GetMIMEType ¶ added in v1.0.0
GetMIMEType returns a MIME type for the file extension (or application/octet-stream).
func GetTimezoneOffset ¶ added in v1.0.0
GetTimezoneOffset returns the offset in seconds for the given timezone name.
func HMACSHA256 ¶ added in v1.0.0
HMACSHA256 returns HMAC-SHA256 of data with key as hex string.
func HMACSHA512 ¶ added in v1.0.0
HMACSHA512 returns HMAC-SHA512 of data with key as hex string.
func HashPassword ¶ added in v1.0.0
HashPassword hashes a password using bcrypt (DefaultCost).
func HashPasswordArgon2 ¶ added in v1.0.0
HashPasswordArgon2 hashes a password using Argon2id (salt + hash combined, hex-encoded).
func IsNotEmpty ¶
IsNotEmpty returns true if s has non-whitespace content.
func IsStrongPassword ¶
IsStrongPassword returns true if password has at least 8 chars and upper, lower, digit, special.
func IsValidEmail ¶
IsValidEmail returns true if email matches a common email pattern.
func IsValidPhone ¶ added in v1.0.0
IsValidPhone returns true if phone has 10–15 digits (after stripping non-digits).
func IsValidSlug ¶ added in v1.0.0
IsValidSlug returns true if slug is empty or matches [a-z0-9-]+.
func IsValidURL ¶
IsValidURL returns true if url looks like http(s) URL.
func IsValidUsername ¶ added in v1.0.0
IsValidUsername returns true if username is 3–20 chars and alphanumeric/underscore.
func ParseDuration ¶ added in v1.0.0
TODO:: need to improve this, ParseDuration parses a duration string (e.g. "1d", "3600", "1h30m").
func ParsePaginationParams ¶ added in v1.0.0
ParsePaginationParams reads page and per_page from Fiber query (defaults 1, 15; per_page capped at 100).
func PascalCase ¶ added in v1.0.0
PascalCase converts to PascalCase.
func PrintError ¶ added in v1.0.0
func PrintError(message string)
PrintError prints message with red cross.
func PrintInfo ¶ added in v1.0.0
func PrintInfo(message string)
PrintInfo prints message with blue info.
func PrintSuccess ¶ added in v1.0.0
func PrintSuccess(message string)
PrintSuccess prints message with green checkmark.
func PrintWarning ¶ added in v1.0.0
func PrintWarning(message string)
PrintWarning prints message with yellow warning.
func RemoveDuplicates ¶
RemoveDuplicates returns a new slice with duplicate strings removed (order preserved).
func SHA1File ¶ added in v1.0.0
SHA1File returns the SHA-1 hash of the file at filePath as hex string.
func SHA256File ¶ added in v1.0.0
SHA256File returns the SHA-256 hash of the file at filePath as hex string.
func ShowProgress ¶ added in v1.0.0
ShowProgress prints a progress bar for current/total with message (50-char bar).
func Slugify ¶ added in v1.0.0
Slugify converts a string to a URL-friendly slug (lowercase, hyphens).
func SortSlice ¶
func SortSlice(slice interface{}, fields []SortField)
SortSlice sorts slice in place by the given fields (struct field names).
func StartOfDay ¶ added in v1.0.0
StartOfDay returns t with time set to 00:00:00.
func StartOfMonth ¶ added in v1.0.0
StartOfMonth returns the first day of t's month at 00:00:00.
func StartOfWeek ¶ added in v1.0.0
StartOfWeek returns the start of the week (Monday 00:00:00).
func TruncateWords ¶
TruncateWords truncates to wordCount words and appends "..." if truncated.
Types ¶
type PaginationLinks ¶ added in v1.0.0
type PaginationLinks struct {
First string `json:"first"`
Last string `json:"last"`
Prev string `json:"prev"`
Next string `json:"next"`
}
PaginationLinks holds first/last/prev/next URLs.
func GeneratePaginationLinks ¶ added in v1.0.0
func GeneratePaginationLinks(baseURL string, meta PaginationMeta) PaginationLinks
GeneratePaginationLinks builds First/Last/Prev/Next URLs for the given baseURL and meta.
type PaginationMeta ¶
type PaginationMeta struct {
CurrentPage int `json:"current_page"`
PerPage int `json:"per_page"`
Total int64 `json:"total"`
LastPage int `json:"last_page"`
From int `json:"from"`
To int `json:"to"`
HasMore bool `json:"has_more"`
}
PaginationMeta holds pagination metadata.
func NewPagination ¶ added in v1.0.0
func NewPagination(page, perPage int, total int64) PaginationMeta
NewPagination builds PaginationMeta for the given page, perPage, and total.
type PaginationResponse ¶
type PaginationResponse struct {
Data interface{} `json:"data"`
Pagination PaginationMeta `json:"pagination"`
Links PaginationLinks `json:"links"`
}
PaginationResponse combines data with pagination and links.
type SortField ¶
SortField represents a field name and sort order (asc/desc).
func ParseSortParams ¶ added in v1.0.0
ParseSortParams parses a sort string like "name,-created_at" into SortField slice.