ciphering

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2025 License: BSD-3-Clause Imports: 7 Imported by: 0

README


This package provides AES encryption and decryption utilities using AES-GCM mode. It supports both AES-128 and AES-256 encryption.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AES128

type AES128 struct {
	// contains filtered or unexported fields
}

AES128 provides AES encryption with a 128-bit key.

Example
package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/ciphering"
)

func main() {
	// Create a new AES-128 handler with a secret key
	aes128, err := ciphering.NewAES128("mysecret")
	if err != nil {
		fmt.Println(err)
	}

	// Data to be encrypted
	plaintext := []byte("This is a secret message")

	// Encrypt the data
	ciphertext, err := aes128.Encrypt(plaintext)
	if err != nil {
		fmt.Println(err)
	}

	// Decrypt the data
	decrypted, err := aes128.Decrypt(ciphertext)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Printf("Decrypted: %s\n", decrypted)

}
Output:

Decrypted: This is a secret message

func NewAES128

func NewAES128(secret string) (*AES128, error)

NewAES128 creates a new AES-128 handler using the provided secret. The secret is hashed with SHA-256 to derive a 128-bit key and AAD.

func (AES128) Decrypt

func (h AES128) Decrypt(b []byte) ([]byte, error)

Decrypt decrypts the input data using AES-GCM. It extracts the nonce from the input and decrypts the data.

func (AES128) Encrypt

func (h AES128) Encrypt(b []byte) ([]byte, error)

Encrypt encrypts the input data using AES-GCM. It generates a nonce, encrypts the data, and prepends the nonce to the output.

type AES256

type AES256 struct {
	// contains filtered or unexported fields
}

AES256 provides AES encryption with a 256-bit key.

Example
package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/ciphering"
)

func main() {
	// Create a new AES-256 handler with a secret key
	aes256, err := ciphering.NewAES256("anothersecret")
	if err != nil {
		fmt.Println(err)
	}

	// Data to be encrypted
	plaintext := []byte("Another secret message")

	// Encrypt the data
	ciphertext, err := aes256.Encrypt(plaintext)
	if err != nil {
		fmt.Println(err)
	}

	// Decrypt the data
	decrypted, err := aes256.Decrypt(ciphertext)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Printf("Decrypted: %s\n", decrypted)

}
Output:

Decrypted: Another secret message

func NewAES256

func NewAES256(secret string) (*AES256, error)

NewAES256 creates a new AES-256 handler using the provided secret. The secret is hashed with SHA-512 to derive a 256-bit key and AAD.

func (AES256) Decrypt

func (h AES256) Decrypt(b []byte) ([]byte, error)

Decrypt decrypts the input data using AES-GCM. It extracts the nonce from the input and decrypts the data.

func (AES256) Encrypt

func (h AES256) Encrypt(b []byte) ([]byte, error)

Encrypt encrypts the input data using AES-GCM. It generates a nonce, encrypts the data, and prepends the nonce to the output.

type Handler

type Handler interface {
	Encrypt([]byte) ([]byte, error)
	Decrypt([]byte) ([]byte, error)
}

Handler defines the contract for encryption and decryption methods.

Jump to

Keyboard shortcuts

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