encryptor

package module
v0.0.0-...-dc36c19 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

README

Encryptor

Easily encrypt and decrypt data in Golang. Optionally encryption can either use a custom password or can randomly generate a key.

Currently Available
  • AES Encryption

    • AES-128
    • AES-192
    • AES-256
  • More to come...

AES Example
package main

import (
	"fmt"
	"github.com/brandenc40/encryptor"
)

func main() {
	enc := encryptor.NewAESEncryptor(encryptor.AES256)

	inputData := []byte("some file data")
	fmt.Println("input data:", string(inputData))

	// 1. Encrypt and decrypt with randomly generated key
	encryptedData, key, err := enc.Encrypt(inputData)
	if err != nil {
		panic(err)
	}
	decryptedData, err := enc.Decrypt(encryptedData, key)
	if err != nil {
		panic(err)
	}

	// 2. Encrypt and decrypt with password
	password := []byte("password123")
	// or you can use enc.GenerateKey() to generate a random key
	encryptedData, err = enc.EncryptWithPassword(decryptedData, password)
	if err != nil {
		panic(err)
	}
	decryptedData, err = enc.Decrypt(encryptedData, password)
	if err != nil {
		panic(err)
	}

	// Show the decryptedData is equal to the inputData
	fmt.Println("output data:", string(decryptedData))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AESBitSize

type AESBitSize int

AESBitSize is the bit size option for AES encryption

const (
	AES128 AESBitSize = iota
	AES192
	AES256
)

func (AESBitSize) KeySize

func (a AESBitSize) KeySize() int

KeySize is the size of key required for the given bit size

AES-128 = 16 characters
AES-192 = 24 characters
AES-256 = 32 characters

type AESEncryptor

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

func NewAESEncryptor

func NewAESEncryptor(bitSize AESBitSize) *AESEncryptor

NewAESEncryptor generates a new AES encryptor

func (*AESEncryptor) Decrypt

func (e *AESEncryptor) Decrypt(encryptedBytes []byte, key []byte) ([]byte, error)

Decrypt decrypts a []byte using a given key.

func (*AESEncryptor) Encrypt

func (e *AESEncryptor) Encrypt(inputBytes []byte) ([]byte, []byte, error)

Encrypt encrypts an input []byte and generates a random key. This function will return the encrypted input []byte and a key also as []byte.

func (*AESEncryptor) EncryptWithPassword

func (e *AESEncryptor) EncryptWithPassword(inputBytes []byte, password []byte) ([]byte, error)

EncryptWithPassword encrypts a []byte with a given password. The []byte will only be able to be decrypted by using the given password as a key.

func (*AESEncryptor) GenerateKey

func (e *AESEncryptor) GenerateKey() []byte

GenerateKey generates a random key with the correct length for the chosen bit size.

AES-128 = 16 characters
AES-192 = 24 characters
AES-256 = 32 characters

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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