aesgcm

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2025 License: MIT Imports: 7 Imported by: 0

README

aes-gcm-go

Minimalistic, zero-dependencies, goroutine-safe, constant-memory AES-256-GCM encryption for Go.

Features

  • 🔐 AES-256-GCM: Strong, authenticated encryption
  • 🧵 Goroutine-safe: Safe to use across concurrent goroutines
  • 📦 Zero-dependencies: Uses only the Go standard library
  • 🔁 Constant memory: No allocations per operation beyond nonce and ciphertext

Installation

go get github.com/dimastofff/aes-gcm-go

Usage

openssl rand -base64 32
Encrypt and Decrypt
package main

import (
	"fmt"
	"log"

	"github.com/dimastofff/aes-gcm-go"
)

func main() {
	b64Key := "AeDG1FxUDTw9A3fAXgxW3L8+tJ8f4W/oEPSNz373W4g="

	c, err := aesgcm.New(b64Key)
	if err != nil {
		log.Fatalf("Failed to initialize cipher: %v", err)
	}

	// Encrypt
	plaintext := "Hello, AES-256-GCM!"
	ciphertext, err := c.Encrypt(plaintext)
	if err != nil {
		log.Fatalf("Encryption failed: %v", err)
	}
	fmt.Printf("Encrypted: %x\n", ciphertext)

	// Decrypt
	decrypted, err := c.Decrypt(ciphertext)
	if err != nil {
		log.Fatalf("Decryption failed: %v", err)
	}
	fmt.Printf("Decrypted: %s\n", decrypted)
}

API

New(b64Key string) (Cipher, error)

Initializes a new cipher using a base64-encoded 32-byte key. Returns a Cipher implementation or an error if the key is invalid.

Cipher Interface

Defines two primary methods:

  • Encrypt(string) ([]byte, error) for encrypting plaintext
  • Decrypt([]byte) (string, error) for decrypting ciphertext
Errors
  • ErrKeyInvalid: Returned if the provided key is not 32 bytes or is improperly formatted
  • ErrEncrypt: Indicates a failure during encryption
  • ErrDecrypt: Indicates a failure during decryption

How to run tests

go test -v .

How to run benchmarks

go test -bench=. ./

License

MIT

Documentation

Overview

Package aesgcm offers minimalistic AES-256-GCM authenticated encryption with constant-memory, goroutine-safe primitives.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyInvalid indicates that the encryption key is invalid or improperly formatted.
	ErrKeyInvalid = errors.New("aesgcm: invalid key")

	// ErrEncrypt indicates that the encryption process failed.
	ErrEncrypt = errors.New("aesgcm: encryption failed")

	// ErrDecrypt indicates that the decryption process failed.
	ErrDecrypt = errors.New("aesgcm: decryption failed")
)

Functions

This section is empty.

Types

type Cipher

type Cipher interface {
	// Encrypt takes a plaintext string and returns the encrypted data as a byte slice.
	Encrypt(plain string) ([]byte, error)

	// Decrypt takes a byte slice produced by Encrypt and returns the original plaintext string.
	Decrypt(raw []byte) (string, error)
}

Cipher defines the interface for high-level AES-256-GCM encryption and decryption.

func New

func New(b64Key string) (Cipher, error)

New creates a new AES-256-GCM cipher using a base64-encoded 32-byte secret key. Returns an instance of Cipher or an error if the key is invalid.

Jump to

Keyboard shortcuts

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