encrypt

package module
v0.0.0-...-8a9390f Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: BSD-3-Clause Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Encrypt - Go Cryptography Toolkit

The encrypt package is a lightweight, extensible, and secure Go cryptography library providing a unified interface for multiple encryption and hashing algorithms.
By prepending a unique 3-byte prefix to generated ciphertexts and hashes, this package enables seamless decryption and password comparison without requiring you to separately store the encryption mode or hashing algorithm in your database.

Key Features

  • Unified Interface: Standardized Encrypt(), Decrypt(), CreateHash(), and CompareHash() functions across all supported algorithms.
  • Smart Auto-Prefixing: Ciphertexts and hashes automatically include a 3-byte identifier (e.g., x0F for AES-GCM). The Decrypt and CompareHash functions read this prefix to automatically determine the correct algorithm.
  • Authenticated Encryption: Native support for modern AEAD ciphers (AES-GCM, XChaCha20-Poly1305) and Branca tokens. Branca tokens support optional TTL expirations.
  • Custom AEAD Support: The Cipher mode allows you to wrap any standard cipher.AEAD implementation.
  • Secure Hashing: Configurable password hashing using Argon2id and Bcrypt.
  • Timing Attack Prevention: Implements subtle.ConstantTimeCompare for safe, constant-time hash verification when using SHA-256.

Installation

You can install this package using go get:

go get github.com/pat3icki/encrypt

Supported Algorithms & Prefixes

The package uses the following internal 3-byte prefixes to seamlessly route decryption and validation requests:

Algorithm Operation Prefix Mode Constant
AES-256 (CFB) Encryption x00 EncryptModeAES256
AES-256-GCM Encryption x0F EncryptModeAES256GCM
XChaCha20-Poly1305 Encryption x10 EncryptModeXChaCha_Poly1305
Branca Encryption x11 EncryptModeBranca
Custom AEAD Cipher Encryption hc1 EncryptModeCipher
Bcrypt Hashing h00 HashModeBcrypt
Argon2id Hashing h0F HashModeArgon2
SHA-256 Hashing h10 HashModeSHA256

Usage Examples

1. Encryption & Decryption

The package handles secure nonce and Initialization Vector (IV) generation automatically using crypto/rand.

package main

import (
	"fmt"
	"log"

	"github.com/pat3icki/encrypt"
)

func main() {
	// Key must be exactly 32 bytes for AES-256, XChaCha20-Poly1305, and Branca.
	key := []byte("12345678901234567890123456789012")
	data := []byte("hello, secret world!")

	// Initialize your chosen mode (e.g., AES-GCM)
	mode := &encrypt.AES_GCM{Key: key}

	// Encrypt the data
	ciphertext, err := encrypt.Encrypt(mode, data)
	if err != nil {
		log.Fatalf("Encryption failed: %v", err)
	}

	// Decrypt (The package automatically reads the 'x0F' prefix to route the decryption)
	plaintext, decMode, err := encrypt.Decrypt(mode, ciphertext)
	if err != nil {
		log.Fatalf("Decryption failed: %v", err)
	}

	fmt.Printf("Decrypted: %s\n", plaintext)
	fmt.Printf("Detected Mode: %v\n", decMode)
}
2. Password Hashing & Verification

You can easily hash passwords using algorithms like Argon2id. The CreateHash function takes generic options (opts), which accept a pointer to Argon2Params for Argon2id, or an int cost factor for Bcrypt.

package main

import (
	"fmt"
	"log"

	"github.com/pat3icki/encrypt"
)

func main() {
	password := []byte("my_super_secure_password")

	// Hash using Argon2id. Passing nil uses DefaultArgon2Params.
	hashed, err := encrypt.CreateHash(encrypt.HashModeArgon2, password, (*encrypt.Argon2Params)(nil))
	if err != nil {
		log.Fatalf("Hashing failed: %v", err)
	}

	// Compare password (reads the 'h0F' prefix to know it should use Argon2id)
	err = encrypt.CompareHash(password, hashed)
	if err != nil {
		if err == encrypt.ErrInvalidCredentials {
			log.Fatal("Invalid password!")
		}
		log.Fatal(err)
	}

	fmt.Println("Password matched successfully!")
}
3. Simple SHA-256 Checksum

If you just need a quick hex-encoded SHA-256 digest, you can use the CheckSum utility:

data := []byte("hello")
checksum := encrypt.CheckSum(data)
// returns: "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

API Reference

Encryption Types
type Encryptor interface {
    Mode() Mode
    _encrypt(data []byte) ([]byte, error)
    _decrypt(cipher []byte) ([]byte, error)
}
Type Fields Description
AES256 Key []byte AES-256-CFB encryption
AES_GCM Key []byte AES-256-GCM authenticated encryption
XChaCha_Poly1305 Key []byte XChaCha20-Poly1305 authenticated encryption
Branca Password string, Expire uint32 Branca token encoding with optional TTL
Cipher cipher.AEAD Generic wrapper for any cipher.AEAD implementation
Encryption Functions
Function Signature Description
Encrypt Encrypt(mode Encryptor, data []byte) ([]byte, error) Encrypts data and prepends the mode prefix
Decrypt Decrypt(mode Encryptor, data []byte) ([]byte, Mode, error) Reads the prefix and decrypts the ciphertext
CheckSum CheckSum(data []byte) string Returns a hex-encoded SHA-256 digest
Hashing Types

const (
    HashModeBcrypt 
    HashModeArgon2
    HashModeSHA256
)

type Argon2Params struct {
    Memory      uint32 // The amount of memory used by the algorithm (in kibibytes)
    Iterations  uint32 // The number of iterations over the memory
    Parallelism uint8  // The number of threads (or lanes) used by the algorithm
    SaltLength  uint32 // Length of the random salt
    KeyLength   uint32 // Length of the generated key
}
Hashing Functions
Function Signature Description
CreateHash CreateHash[T Hashable](mode Mode, data []byte, opts T) ([]byte, error) Creates a prefixed hash using the specified algorithm
CompareHash CompareHash(data []byte, hashed_data []byte) error Verifies data against a prefixed hash
Generic Constraints

The CreateHash function accepts the following generic types via the Hashable constraint (int | *Argon2Params):

Algorithm opts Type Description
HashModeBcrypt int The cost factor (e.g., 10-14)
HashModeArgon2 *Argon2Params Argon2id parameters (pass nil for defaults)
HashModeSHA256 int Ignored placeholder
Default Argon2id Parameters
var DefaultArgon2Params = &Argon2Params{
    Memory:      64 * 1024,          // 64 MiB
    Iterations:  1,
    Parallelism: uint8(runtime.NumCPU()),
    SaltLength:  16,
    KeyLength:   32,
}

Security Considerations

  • Key Length: Always ensure encryption keys are exactly 32 bytes long for AES256, XChaCha_Poly1305, and Branca.
  • Ciphertext Length: Decryption functions will return an error if the cipher data is shorter than the 3-byte prefix length.
  • Argon2id Defaults: The DefaultArgon2Params use 64 MiB of memory, 1 iteration, parallelism based on runtime.NumCPU(), a 16-byte salt, and a 32-byte key.
  • Constant-Time Comparison: SHA-256 hash verification uses subtle.ConstantTimeCompare to mitigate timing attacks.
  • Random Nonce/IV Generation: All AEAD modes and CFB automatically generate secure random nonces and IVs using crypto/rand.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultArgon2Params = Argon2{
	Memory:      64 * 1024,
	Iterations:  1,
	Parallelism: uint8(runtime.NumCPU()),
	SaltLength:  16,
	KeyLength:   32,
}
View Source
var (
	ErrInvalidCredentials = errors.New("crypto hashing: hashedPassword is not the hash of the given password")
)

Functions

func BytesToString

func BytesToString(b []byte) string

func CheckSum

func CheckSum(data []byte) string

Hash computes the SHA-256 digest of the input data and returns it as a hex string.

func CompareHash

func CompareHash(data []byte, hashed_data []byte) error

TODO: UPDATE

func CreateHash

func CreateHash(hasher Hasher, data []byte) ([]byte, error)

CreateHash creates a hash of the data using the specified hash algorithm.

func Encrypt

func Encrypt(mode Encryptor, data []byte) ([]byte, error)

Encrypt encrypts data based on the selected mode and prepends a 3-character identifier.

func StringToBytes

func StringToBytes(s string) []byte

Types

type AES_GCM

type AES_GCM struct {
	Key []byte
}

func (*AES_GCM) Mode

func (*AES_GCM) Mode() Mode

func (*AES_GCM) Prefix

func (ag *AES_GCM) Prefix() string

type Argon2

type Argon2 struct {
	// The amount of memory used by the algorithm (in kibibytes).
	Memory uint32

	// The number of iterations over the memory.
	Iterations uint32

	// The number of threads (or lanes) used by the algorithm.
	// Recommended value is between 1 and runtime.NumCPU().
	Parallelism uint8

	// Length of the random salt. 16 bytes is recommended for password hashing.
	SaltLength uint32

	// Length of the generated key. 16 bytes or more is recommended.
	KeyLength uint32
}

func (Argon2) Mode

func (Argon2) Mode() Mode

func (Argon2) Prefix

func (ag Argon2) Prefix() string

type Bcrypt

type Bcrypt struct {
	Cost int
}

func (Bcrypt) Mode

func (Bcrypt) Mode() Mode

func (Bcrypt) Prefix

func (b Bcrypt) Prefix() string

type Branca

type Branca struct {
	Password string
	Expire   uint32
}

func (*Branca) Mode

func (b *Branca) Mode() Mode

func (*Branca) Prefix

func (b *Branca) Prefix() string

type Cipher

type Cipher struct {
	cipher.AEAD
}

func (*Cipher) Mode

func (c *Cipher) Mode() Mode

func (*Cipher) Prefix

func (c *Cipher) Prefix() string

type Encryptor

type Encryptor interface {
	Mode() Mode
	Prefix() string
	// contains filtered or unexported methods
}

type Hasher

type Hasher interface {
	Mode() Mode
	Prefix() string
	// contains filtered or unexported methods
}

type Mode

type Mode int
const (
	NoneMode                    Mode = iota
	EncryptModeAESGCM                // Prefix: x0F (using GCM mode)
	EncryptModeXChaCha_Poly1305      // Prefix: x10
	EncryptModeBranca                // Prefix: x11
	EncryptModeCipher

	HashModeBcrypt
	HashModeArgon2
	HashModeSHA256
)

func Decrypt

func Decrypt(mode Encryptor, data []byte) ([]byte, Mode, error)

Decrypt reads the 3-character identifier to determine the mode and decrypts the data.

type XChaCha_Poly1305

type XChaCha_Poly1305 struct {
	Key []byte
}

func (*XChaCha_Poly1305) Mode

func (*XChaCha_Poly1305) Mode() Mode

func (*XChaCha_Poly1305) Prefix

func (xcha *XChaCha_Poly1305) Prefix() string

Jump to

Keyboard shortcuts

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