Documentation
¶
Overview ¶
Package encrypt provides a comprehensive collection of cryptographic functions with Rust-inspired error handling.
This package offers:
- Complete hash function suite: MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, SHA-512/256, FNV-1, FNV-1a (32/64/128-bit), CRC-32, CRC-64, Adler-32
- AES encryption/decryption with multiple modes (ECB, CBC, CTR)
- Support for both hex and base64 encoding formats
- Type-safe error handling using gust's Result[T] type for chainable operations
Examples ¶
// Hash operations (user can specify encoding)
hash := encrypt.SHA256([]byte("hello"), encrypt.EncodingHex)
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // hex-encoded hash string
}
// AES encryption with chainable operations
key := []byte("1234567890123456") // 16 bytes for AES-128
plaintext := []byte("secret data")
result := encrypt.EncryptAES(key, plaintext, encrypt.ModeCBC, encrypt.EncodingBase64).
Map(func(ciphertext []byte) []byte {
// Further processing if needed
return ciphertext
})
if result.IsOk() {
ciphertext := result.Unwrap()
// Decrypt
decrypted := encrypt.DecryptAES(key, ciphertext, encrypt.ModeCBC, encrypt.EncodingBase64)
if decrypted.IsOk() {
fmt.Println(string(decrypted.Unwrap()))
}
}
Index ¶
- func Adler32(data []byte) result.Result[uint32]
- func CRC32(data []byte) result.Result[uint32]
- func CRC64(data []byte) result.Result[uint64]
- func DecryptAES(key, ciphertext []byte, mode Mode, encoding Encoding) result.Result[[]byte]
- func EncryptAES(key, plaintext []byte, mode Mode, encoding Encoding) result.Result[[]byte]
- func FNV1_32(data []byte) result.Result[uint32]
- func FNV1_64(data []byte) result.Result[uint64]
- func FNV1_128(data []byte, encoding Encoding) result.Result[string]
- func FNV1a32(data []byte) result.Result[uint32]
- func FNV1a64(data []byte) result.Result[uint64]
- func FNV1a128(data []byte, encoding Encoding) result.Result[string]
- func MD5(data []byte, encoding Encoding) result.Result[string]
- func SHA1(data []byte, encoding Encoding) result.Result[string]
- func SHA224(data []byte, encoding Encoding) result.Result[string]
- func SHA256(data []byte, encoding Encoding) result.Result[string]
- func SHA384(data []byte, encoding Encoding) result.Result[string]
- func SHA512(data []byte, encoding Encoding) result.Result[string]
- func SHA512_224(data []byte, encoding Encoding) result.Result[string]
- func SHA512_256(data []byte, encoding Encoding) result.Result[string]
- type Encoding
- type Mode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Adler32 ¶
Adler32 returns the Adler-32 checksum of the data.
Examples ¶
hash := encrypt.Adler32([]byte("hello"))
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // uint32 value
}
func CRC32 ¶
CRC32 returns the CRC-32 checksum of the data.
Examples ¶
hash := encrypt.CRC32([]byte("hello"))
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // uint32 value
}
func CRC64 ¶
CRC64 returns the CRC-64 checksum of the data.
Examples ¶
hash := encrypt.CRC64([]byte("hello"))
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // uint64 value
}
func DecryptAES ¶
DecryptAES decrypts data using AES with the specified mode and encoding.
The key must be 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 respectively. The encoding must match the encoding used during encryption.
Examples ¶
key := []byte("1234567890123456")
ciphertext := []byte("encrypted data")
result := encrypt.DecryptAES(key, ciphertext, encrypt.ModeCBC, encrypt.EncodingBase64)
if result.IsOk() {
plaintext := result.Unwrap()
fmt.Println(string(plaintext))
}
func EncryptAES ¶
EncryptAES encrypts data using AES with the specified mode and encoding.
The key must be 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 respectively.
Examples ¶
key := []byte("1234567890123456") // 16 bytes for AES-128
plaintext := []byte("secret data")
result := encrypt.EncryptAES(key, plaintext, encrypt.ModeCBC, encrypt.EncodingBase64)
if result.IsOk() {
ciphertext := result.Unwrap()
// Use ciphertext...
}
func FNV1_32 ¶
FNV1_32 returns the 32-bit FNV-1 hash of the data.
Examples ¶
hash := encrypt.FNV1_32([]byte("hello"))
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // uint32 value
}
func FNV1_64 ¶
FNV1_64 returns the 64-bit FNV-1 hash of the data.
Examples ¶
hash := encrypt.FNV1_64([]byte("hello"))
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // uint64 value
}
func FNV1_128 ¶
FNV1_128 returns the 128-bit FNV-1 hash of the data as an encoded string.
Examples ¶
hash := encrypt.FNV1_128([]byte("hello"), encrypt.EncodingHex)
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // hex-encoded hash string
}
func FNV1a32 ¶
FNV1a32 returns the 32-bit FNV-1a hash of the data.
Examples ¶
hash := encrypt.FNV1a32([]byte("hello"))
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // uint32 value
}
func FNV1a64 ¶
FNV1a64 returns the 64-bit FNV-1a hash of the data.
Examples ¶
hash := encrypt.FNV1a64([]byte("hello"))
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // uint64 value
}
func FNV1a128 ¶
FNV1a128 returns the 128-bit FNV-1a hash of the data as an encoded string.
Examples ¶
hash := encrypt.FNV1a128([]byte("hello"), encrypt.EncodingHex)
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // hex-encoded hash string
}
func MD5 ¶
MD5 returns the MD5 checksum of the data as an encoded string.
Examples ¶
// Using default hex encoding (most common)
hash := encrypt.MD5([]byte("hello"), encrypt.EncodingHex)
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // hex-encoded hash string
}
// Using base64 encoding (more compact)
hash := encrypt.MD5([]byte("hello"), encrypt.EncodingBase64)
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // base64-encoded hash string
}
func SHA1 ¶
SHA1 returns the SHA1 checksum of the data as an encoded string.
Examples ¶
hash := encrypt.SHA1([]byte("hello"), encrypt.EncodingHex)
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // hex-encoded hash string
}
func SHA224 ¶
SHA224 returns the SHA224 checksum of the data as an encoded string.
Examples ¶
hash := encrypt.SHA224([]byte("hello"), encrypt.EncodingHex)
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // hex-encoded hash string
}
func SHA256 ¶
SHA256 returns the SHA256 checksum of the data as an encoded string.
Examples ¶
hash := encrypt.SHA256([]byte("hello"), encrypt.EncodingHex)
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // hex-encoded hash string
}
func SHA384 ¶
SHA384 returns the SHA384 checksum of the data as an encoded string.
Examples ¶
hash := encrypt.SHA384([]byte("hello"), encrypt.EncodingHex)
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // hex-encoded hash string
}
func SHA512 ¶
SHA512 returns the SHA512 checksum of the data as an encoded string.
Examples ¶
hash := encrypt.SHA512([]byte("hello"), encrypt.EncodingHex)
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // hex-encoded hash string
}
func SHA512_224 ¶
SHA512_224 returns the SHA-512/224 checksum of the data as an encoded string.
Examples ¶
hash := encrypt.SHA512_224([]byte("hello"), encrypt.EncodingHex)
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // hex-encoded hash string
}
func SHA512_256 ¶
SHA512_256 returns the SHA-512/256 checksum of the data as an encoded string.
Examples ¶
hash := encrypt.SHA512_256([]byte("hello"), encrypt.EncodingHex)
if hash.IsOk() {
fmt.Println(hash.Unwrap()) // hex-encoded hash string
}
Types ¶
type Encoding ¶
type Encoding int
Encoding represents the encoding format for encrypted data and hash output.