A comprehensive Go library providing various hashing, encoding, and utility functions with support for any
type input.
go get github.com/getevo/hash
- Hash Functions: CRC32, CRC64, FNV32, FNV64, MD5, SHA1, SHA256, SHA384, SHA512
- File Hashing: Hash files directly from disk
- HMAC Functions: HMAC with MD5, SHA1, SHA256
- Encoding/Decoding: Base64, URL-safe Base64, Base32, Hex, Binary, ASCII
- Conversion Utilities: Hex↔Decimal, Binary↔Hex, ASCII↔Hex
- Security Utilities: Cryptographically secure random generation, constant-time comparison
- URL Utilities: URL encoding/decoding
- General Utilities: UUID generation, timestamps, random strings
import "github.com/getevo/hash"
// Basic hash functions
hash.CRC32("hello world") // Returns uint32
hash.CRC32String("hello world") // Returns hex string
hash.MD5("hello world") // Returns hex string
hash.SHA256("hello world") // Returns hex string
// Works with any type
hash.MD5(12345)
hash.SHA1([]byte("data"))
hash.CRC32(true)
// Hash files directly
crc, err := hash.CRC32File("/path/to/file")
md5Hash, err := hash.MD5File("/path/to/file")
sha1Hash, err := hash.SHA1File("/path/to/file")
// HMAC with different algorithms
hmacMD5 := hash.HMACMD5("secret-key", "data")
hmacSHA1 := hash.HMACSHA1("secret-key", "data")
hmacSHA256 := hash.HMACSHA256("secret-key", "data")
// Base64
encoded := hash.Base64Encode("hello")
decoded, err := hash.Base64Decode(encoded)
// URL-safe Base64
urlEncoded := hash.URLBase64Encode("hello")
urlDecoded, err := hash.URLBase64Decode(urlEncoded)
// Base32
b32Encoded := hash.Base32Encode("hello")
b32Decoded, err := hash.Base32Decode(b32Encoded)
// Hex and Decimal
hexStr := hash.Decimal2Hex(255) // "ff"
decimal, err := hash.Hex2Decimal("ff") // 255
// Binary and Hex
binStr, err := hash.Hex2Binary("41") // "01000001"
hexStr := hash.Binary2Hex("01000001") // "41"
// ASCII and Hex
hexStr := hash.ASCII2Hex("A") // "41"
ascii, err := hash.Hex2ASCII("41") // "A"
// Cryptographically secure random generation
randomBytes := hash.RandomBytes(16) // []byte of 16 random bytes
randomHex := hash.SecureRandom(32) // 32-char random hex string
// Constant-time hash comparison (prevents timing attacks)
isEqual := hash.CompareHash(hash1, hash2)
// URL encoding/decoding
encoded := hash.URLEscape("hello world!") // "hello+world%21"
decoded, err := hash.URLUnescape(encoded) // "hello world!"
// UUID generation
uuid := hash.UUID() // "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
// Unix timestamp
timestamp := hash.Timestamp() // "1640995200"
// Random hex string (not cryptographically secure)
randomStr := hash.Random(16) // 16-char random hex string
Function | Input | Output | Description |
---|---|---|---|
CRC32(v any) |
any | uint32 |
CRC32 checksum |
CRC32String(v any) |
any | string |
CRC32 as hex string |
CRC64(v any) |
any | uint64 |
CRC64 checksum |
CRC64String(v any) |
any | string |
CRC64 as hex string |
FNV32(v any) |
any | uint32 |
FNV-1a 32-bit hash |
FNV32String(v any) |
any | string |
FNV-1a 32-bit as hex string |
FNV64(v any) |
any | uint64 |
FNV-1a 64-bit hash |
FNV64String(v any) |
any | string |
FNV-1a 64-bit as hex string |
MD5(v any) |
any | string |
MD5 hash as hex string |
SHA1(v any) |
any | string |
SHA1 hash as hex string |
SHA256(v any) |
any | string |
SHA256 hash as hex string |
SHA384(v any) |
any | string |
SHA384 hash as hex string |
SHA512(v any) |
any | string |
SHA512 hash as hex string |
Function | Input | Output | Description |
---|---|---|---|
CRC32File(filepath string) |
string | uint32, error |
CRC32 of file |
CRC64File(filepath string) |
string | uint64, error |
CRC64 of file |
MD5File(filepath string) |
string | string, error |
MD5 of file |
SHA1File(filepath string) |
string | string, error |
SHA1 of file |
Function | Input | Output | Description |
---|---|---|---|
HMACMD5(key, data any) |
any, any | string |
HMAC-MD5 |
HMACSHA1(key, data any) |
any, any | string |
HMAC-SHA1 |
HMACSHA256(key, data any) |
any, any | string |
HMAC-SHA256 |
Function | Input | Output | Description |
---|---|---|---|
Base64Encode(v any) |
any | string |
Base64 encoding |
Base64Decode(v any) |
any | string, error |
Base64 decoding |
URLBase64Encode(v any) |
any | string |
URL-safe Base64 encoding |
URLBase64Decode(v any) |
any | string, error |
URL-safe Base64 decoding |
Base32Encode(v any) |
any | string |
Base32 encoding |
Base32Decode(v any) |
any | string, error |
Base32 decoding |
Function | Input | Output | Description |
---|---|---|---|
Hex2Decimal(v any) |
any | int64, error |
Hex string to decimal |
Decimal2Hex(v any) |
any | string |
Decimal to hex string |
Binary2Hex(v any) |
any | string |
Binary string to hex |
Hex2Binary(v any) |
any | string, error |
Hex to binary string |
ASCII2Hex(v any) |
any | string |
ASCII to hex string |
Hex2ASCII(v any) |
any | string, error |
Hex to ASCII string |
Function | Input | Output | Description |
---|---|---|---|
RandomBytes(length int) |
int | []byte |
Cryptographically secure random bytes |
SecureRandom(length int) |
int | string |
Cryptographically secure random hex string |
CompareHash(hash1, hash2 string) |
string, string | bool |
Constant-time hash comparison |
Function | Input | Output | Description |
---|---|---|---|
URLEscape(v any) |
any | string |
URL query escaping |
URLUnescape(v any) |
any | string, error |
URL query unescaping |
Function | Input | Output | Description |
---|---|---|---|
Random(length int) |
int | string |
Random hex string |
UUID() |
- | string |
Generate UUID v4 |
Timestamp() |
- | string |
Current Unix timestamp |
The library supports all common Go types through intelligent type switching:
string
,[]byte
int
,int8
,int16
,int32
,int64
uint
,uint8
,uint16
,uint32
,uint64
float32
,float64
bool
- Any other type (converted using
fmt.Sprintf("%v", v)
)
- Efficient
toBytes()
function avoids unnecessary string conversions - Type switching for optimal performance with common types
- Direct byte operations where possible
- Streaming file operations for large files
- Uses
crypto/rand
for cryptographically secure random generation - Provides constant-time hash comparison to prevent timing attacks
- HMAC functions use proper key-data separation
- File operations handle resources properly with defer statements
MIT License
Contributions are welcome! Please feel free to submit a Pull Request.