encrypt

package
v1.20.3 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 16 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Adler32

func Adler32(data []byte) result.Result[uint32]

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

func CRC32(data []byte) result.Result[uint32]

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

func CRC64(data []byte) result.Result[uint64]

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

func DecryptAES(key, ciphertext []byte, mode Mode, encoding Encoding) result.Result[[]byte]

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

func EncryptAES(key, plaintext []byte, mode Mode, encoding Encoding) result.Result[[]byte]

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

func FNV1_32(data []byte) result.Result[uint32]

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

func FNV1_64(data []byte) result.Result[uint64]

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

func FNV1_128(data []byte, encoding Encoding) result.Result[string]

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

func FNV1a32(data []byte) result.Result[uint32]

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

func FNV1a64(data []byte) result.Result[uint64]

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

func FNV1a128(data []byte, encoding Encoding) result.Result[string]

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

func MD5(data []byte, encoding Encoding) result.Result[string]

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

func SHA1(data []byte, encoding Encoding) result.Result[string]

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

func SHA224(data []byte, encoding Encoding) result.Result[string]

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

func SHA256(data []byte, encoding Encoding) result.Result[string]

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

func SHA384(data []byte, encoding Encoding) result.Result[string]

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

func SHA512(data []byte, encoding Encoding) result.Result[string]

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

func SHA512_224(data []byte, encoding Encoding) result.Result[string]

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

func SHA512_256(data []byte, encoding Encoding) result.Result[string]

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.

const (
	// EncodingHex uses hexadecimal encoding (default, most common).
	// This is the standard format used by command-line tools like md5sum, sha256sum.
	EncodingHex Encoding = iota
	// EncodingBase64 uses Base64URL encoding (more compact, URL-safe).
	EncodingBase64
)

type Mode

type Mode int

Mode represents the AES encryption mode.

const (
	// ModeECB uses Electronic Codebook mode (not recommended for most use cases).
	ModeECB Mode = iota
	// ModeCBC uses Cipher Block Chaining mode (recommended for most use cases).
	ModeCBC
	// ModeCTR uses Counter mode (suitable for streaming).
	ModeCTR
)

Jump to

Keyboard shortcuts

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