sym

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2025 License: MIT Imports: 18 Imported by: 0

README

Sym

Go Reference Go Report Card gopherbadger-tag-do-not-edit CI

Can be used as an importable pkg or as a CLI.

Use as a CLI

go install github.com/fuskovic/sym/cmd/sym@latest

Examples

Generate a Key

// default key size is 16
sym keygen

// other valid sizes include 24, and 32
sym keygen --size 32

// output to a file
sym keygen -o key.txt

Encrypt a file

sym encrypt \
    -f key.txt \
    -i inputfile.txt \
    -o outputfile.enc

Encrypt a dir

sym encrypt \
    -f key.txt \
    -i testdir \
    -o encrypted_dir.enc

Decrypt a file

sym decrypt \
    -f key.txt \
    -i ciphertext.enc \
    -o plaintext.txt

Decrypt a dir

sym decrypt \
    -f key.txt \
    -i encrypted_dir.enc \
    -o decrypted_dir

Use as a library

go get -u github.com/fuskovic/sym

Examples

Generate a Key


    // use KeyGen if you want a specific size
    key, err := sym.KeyGen(32)
    if err != nil {
        // handle err
    }

    // or use MustKeyGen to generate a default size (16) key
    key := sym.MustKeyGen()

Use key from file


    key, err := sym.KeyFromFilePath("/path/to/my.key")
    if err != nil {
        // handle err
    }

Encrypt/Decrypt Strings


    key := sym.MustKeyGen()

    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 Files


    key := sym.MustKeyGen()

    err := sym.EncryptFile(key,
        "/path/to/existing/plaintext/file.txt",
        "/path/to/write/the/encrypted/file/to.enc",
    )

    if err != nil {
        // handle error
    }

    err = sym.DecryptFile(key,
        "/path/to/encrypted/file.enc",
        "/path/to/write/decrypted/file/to.txt",
    )

    if err != nil {
        // handle error
    }
    

Encrypt/Decrypt Dirs


    key := sym.MustKeyGen()

    err := sym.EncryptDir(key,
        "/path/to/existing/dir",
        "/path/to/write/encrypted/dir",
    )

    if err != nil {
        // handle error
    }

    err = sym.DecryptDir(key,
        "/path/to/encrypted/dir",
        "/path/to/write/decrypted/dir",
    )

    if 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.

View Source
var ErrNotDir = errors.New("not a directory")

Functions

func DecryptBytes

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

DecryptBytes uses key to return the decrypted content of ciphertextBytes.

func DecryptDir added in v1.3.0

func DecryptDir(key, src, dest string) error

DecryptDir uses key to decrypt src into a zip file. The zip file is then unzipped into dest.

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 plaintextBytes; returning the ciphertext bytes.

func EncryptDir added in v1.3.0

func EncryptDir(key, src, dest string) error

EncryptDir zips the dir at src and encrypts the zip file with key.

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.

func IsEncryptedDir added in v1.3.0

func IsEncryptedDir(key, src string) bool

IsEncryptedDir decrypts src using key and checks if it's a zip file.

func KeyFromFilePath added in v1.1.0

func KeyFromFilePath(path string) (string, error)

KeyFromFile reads the file at path, validates the key, and then returns it. If the file does not exist a non-nil error is returned. If the file exists and the key is invalid a non-nil error is returned.

func KeyGen added in v1.1.0

func KeyGen(size int) (string, error)

KeyGen generates a new key that can be used to encrypt/decrypt strings, byte-data, and files. If size is not 16, 32, or 32 a non-nil error will be returned.

func MustKeyGen added in v1.1.0

func MustKeyGen() string

MustKeyGen always generates a size 16 key.

Types

This section is empty.

Directories

Path Synopsis
cmd
sym

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳