sym

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2021 License: MIT Imports: 7 Imported by: 0

README

Sym

gopherbadger-tag-do-not-edit

A small and lightweight symmetric encryption/decryption package.

Useful for encrypting and decrypting strings, bytes, and files.

Only external dependency is stretchr/testify for the unit tests.

Install

go get -u github.com/fuskovic/sym

Examples

Encrypt/Decrypt Strings

// The key needs to be either 16, 24, or 32 characters in length
key := os.Getenv("SYMMETRIC_KEY")

ciphertext, err := sym.EncryptString(key, "hello world")
if err != nil {
    // handle error
}

plaintext, err := sym.DecryptString(key, ciphertext)
if err != nil {
    // handle error
}

Encrypt/Decrypt Bytes

// The key needs to be either 16, 24, or 32 characters in length
key := os.Getenv("SYMMETRIC_KEY")

ciphertext, err := sym.EncryptBytes(key, []byte("hello world"))
if err != nil {
    // handle error
}

plaintext, err := sym.DecryptBytes(key, ciphertext)
if err != nil {
    // handle error
}

Encrypt/Decrypt Files

var (
    // The key needs to be either 16, 24, or 32 characters in length
    key                 = os.Getenv("SYMMETRIC_KEY")
    plaintextFilePath   = "/path/to/existing/file.txt"
    // New files get created and existing files get overwritten.
    encryptedFilePath   = "/path/to/new/or/existing/file.txt"
)

if err := sym.EncryptFile(key, plaintextFilePath, encryptedFilePath); err != nil {
    // handle error
}

in := encryptedFilePath
out := "/path/to/new/or/existing/file.txt"

if err := sym.DecryptFile(key, in, out); err != nil {
    // handle error
}

Documentation

Overview

Package sym is a simple and lightweight symmetric encryption/decryption pkg.

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyPayload = errors.New("empty payload")

ErrEmptyPayload is returned when the ciphertext string or bytes provided for an encrypt/decrypt operation are empty or nil.

View Source
var ErrInvalidIvLen = errors.New("cipher text does not contain a valid initialization vector length")

ErrInvalidIvLen is returned when the provided ciphertext for a decrypt operation is not at least the length of a valid initialization vector.

Functions

func DecryptBytes

func DecryptBytes(key string, ciphertextBytes []byte) ([]byte, error)

DecryptBytes uses key to return the decrypted content of 'ciphertextBytes'.

func DecryptFile

func DecryptFile(key, in, out string) error

DecryptFile opens in and uses key to decrypt it's ciphertext contents; writing the plaintext contents to out.

func DecryptString

func DecryptString(key, ciphertext string) (string, error)

DecryptString uses key to return the decrypted string contents of ciphertext.

func EncryptBytes

func EncryptBytes(key string, plaintextBytes []byte) ([]byte, error)

EncryptBytes uses key to encrypt plaintext; returning the ciphertext bytes.

func EncryptFile

func EncryptFile(key, in, out string) error

EncryptFile opens in and uses key to encrypt it's plaintext contents; writing the ciphertext contents to out.

func EncryptString

func EncryptString(key, plaintext string) (string, error)

EncryptString uses key to encrypt plaintext; returning the ciphertext string.

Types

This section is empty.

Jump to

Keyboard shortcuts

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