gocrypto

package module
v0.0.0-...-6aed78a Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2019 License: MIT Imports: 9 Imported by: 3

README

go-crypto

A simple package for encrypting and decrypting with a shared key

Each encrypted payload contains a version and all other data required to decrypt with a shared encryption key

Example

package main

import (
  "fmt"

  "github.com/bhoriuchi/go-crypto"
)

func main() {
  data := []byte("Snape Kills Dumbledore")
  
  key, _ := gocrypto.NewSecretKey(512)
  encryptedData, _ := gocrypto.Encrypt(key, data)
  decryptedData, _ := gocrypto.Decrypt(key, encryptedData)

  fmt.Printf("%t \n", string(data) == string(decryptedData))
  // true
}

Versions

  • Version 1 uses AES-256-GCM encryption

Documentation

Index

Constants

View Source
const MaximumVersion = 1

MaximumVersion maximum supported encryption version

View Source
const MinimumVersion = 1

MinimumVersion minimum supported encryption version

View Source
const (
	Version1 = 1
)

standard GCM sizes

View Source
const VersionLength = 16

VersionLength length of byte array containing verison data

Variables

View Source
var ErrUnsupportedVersion = errors.New("unsupported encryption version")

ErrUnsupportedVersion unsupported encryption version

Functions

func Decrypt

func Decrypt(key, payload []byte) ([]byte, error)

Decrypt decrypts a payload and returns its bytes

func Encrypt

func Encrypt(key, data []byte, version ...*uint16) ([]byte, error)

Encrypt encrypts data with optional version

func MakePayload

func MakePayload(version uint16, encryptedData []byte) ([]byte, error)

MakePayload creates a payload containing the version and encrypted data

func NewSecretKey

func NewSecretKey(length int) ([]byte, error)

NewSecretKey generates a new secret key of specified length

func NewSecretKeyBase64

func NewSecretKeyBase64(length int) (string, error)

NewSecretKeyBase64 generates a new secret key of specified length

func SplitPayload

func SplitPayload(payload []byte) (uint16, []byte, error)

SplitPayload returns the version and encrypted data as separate variables by splitting the payload

func VersionBytes

func VersionBytes(version uint16) ([]byte, error)

VersionBytes converts a version to a byte array with range validation

Types

This section is empty.

Jump to

Keyboard shortcuts

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