encrypt

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 8 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.

Documentation

Overview

Package encrypt provides AES-256-GCM authenticated encryption and decryption.

The encrypted output is encoded as hex then base64, with the nonce prepended to the ciphertext. This package uses crypto/rand for nonce generation, ensuring each encryption produces unique output even for identical plaintext.

Key requirements: AES-256 requires a 32-byte key. AES-128 (16-byte) and AES-192 (24-byte) keys are also accepted by the underlying cipher.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecryptAES256GCM

func DecryptAES256GCM(text, key []byte) ([]byte, error)

DecryptAES256GCM decrypts ciphertext that was encrypted with EncryptAES256GCM using the same 32-byte key.

func EncryptAES256GCM

func EncryptAES256GCM(text, key []byte) ([]byte, error)

EncryptAES256GCM encrypts plaintext using AES-256-GCM with the provided 32-byte key. The result is base64-encoded and includes the nonce prepended to the ciphertext.

Example
package main

import (
	"fmt"

	"github.com/alpardfm/go-toolkit/encrypt"
)

func main() {
	// AES-256 requires a 32-byte key
	key := []byte("01234567890123456789012345678901")
	plaintext := []byte("hello, world!")

	// Encrypt the plaintext
	ciphertext, err := encrypt.EncryptAES256GCM(plaintext, key)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	// Decrypt it back
	decrypted, err := encrypt.DecryptAES256GCM(ciphertext, key)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	fmt.Println(string(decrypted))

}
Output:
hello, world!

Types

This section is empty.

Jump to

Keyboard shortcuts

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