cryptu

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

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

Go to latest
Published: Sep 3, 2018 License: MIT Imports: 8 Imported by: 0

README

cryptu GoDoc Build Status codecov Go Report Card

Base64 symmetric encryption wrappers for Go, with hooks to be used by dependency injection.

Encrypt To Base64

The package level functions EncryptToBase64 and DecryptFromBase64 use the crypto/aes package for encryption and encoding/base64.StdEncoding for the encoding.

// keys must be 16, 24, or 32 in length
key := "zqpf8VWyrUP9j1gC"
secret := "sensitive data"

encryptedValue, err := cryptu.EncryptToBase64(key, secret)

if err != nil {
  log.Fatal(err)
}

fmt.Println(encryptedValue)
// R+gUlOWekeVALBOntneoP7wQK2IOBiC3ddS+Rj2x

decodedValue, err := base64.StdEncoding.DecodeString(encryptedValue)

if err != nil {
  log.Fatal(err)
}

fmt.Println(string(decodedValue)) 
// G¦¦?¦@,¦¦w¦?¦+b ¦u?F=¦

decryptedSecret, err := cryptu.DecryptFromBase64(key, encryptedValue)

if err != nil {
  log.Fatal(err)
}

fmt.Println(decryptedSecret)
// sensitive data

A full example is available here

Dependency Injection

An interface is provided to wrap all top level package functions. This interface can be injected the into your code instead of calling the package functions directly. There are some predefined dependency definitions provided by the package. If you'd like to start using them you need only supply a definition for cryptu.Key and cryptu.Base64Encoding:


func NewKey() (cryptu.Key, error)                 { return cryptu.NewStrKey(os.Getenv("MY_KEY")) }
func NewEncoding() (cryptu.Base64Encoding, error) { return cryptu.NewBase64Encoding(base64.StdEncoding) }

var defs = []*di.Def{
    {NewKey, di.Singleton},
    {NewEncoding, di.Singleton},
}

func MyHandler(encoder cryptu.Base64) {
  encryptedValue, err := encoder.Encrypt("some value")
  // if err
  
  myResponse := &MyResponse{Secret: encryptedValue}
  // write response
}

func main() {
  resolver, err := di.NewResolver(errFn, cryptu.NewDiDefs(), defs)
  // if err
  
  handler, err := resolver.HttpHandler(MyHandler)
  // if err
  
  http.HandleFunc("/foo", handler)
  // listen and serve
}

A full example is available here

Documentation

Overview

Package cryptu contains injectable base64 symmetric encryption wrappers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecryptFromBase64

func DecryptFromBase64(cryptKey, text string) (string, error)

DecryptFromBase64 takes a string encoded with EncryptToBase64 unencodes it, decrypts it, and returns the plaintext string using encoding/base64.StdEncoding. cryptKey must be 16, 24, or 32 bytes long.

func EncryptToBase64

func EncryptToBase64(cryptKey, text string) (string, error)

EncryptToBase64 takes a plain text string, encrypts it using AES, and returns the base64 encoded version of the string using encoding/base64.StdEncoding.

cryptKey must be 16, 24, or 32 bytes long

Example
package main

import (
	"encoding/base64"
	"fmt"
	"log"

	"github.com/clavoie/cryptu"
)

func main() {
	// keys must be 16, 24, or 32 in length
	key := "zqpf8VWyrUP9j1gC"
	secret := "sensitive data"

	encryptedValue, err := cryptu.EncryptToBase64(key, secret)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(encryptedValue == secret)

	decodedValue, err := base64.StdEncoding.DecodeString(encryptedValue)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(decodedValue) == secret)

	decryptedSecret, err := cryptu.DecryptFromBase64(key, encryptedValue)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(decryptedSecret)

}
Output:

false
false
sensitive data

func NewDiDefs

func NewDiDefs() []*di.Def

NewDiDefs returns new dependency injection definitions for this package

Types

type Base64

type Base64 interface {
	// Decrypt decrypts a base64 encoded value and returns the decrypted
	// value.
	Decrypt(string) (string, error)

	// Encrypt encrypts a raw value and returns the encrypted base64 encoded
	// value.
	Encrypt(string) (string, error)
}

Base64 symmetrically encrypts / decrypts strings to and from base64 encoded values.

Example
package main

import (
	"encoding/base64"
	"fmt"
	"log"
	"net/http"

	"github.com/clavoie/cryptu"
	"github.com/clavoie/di"
)

func NewKey() (cryptu.Key, error) { return cryptu.NewStrKey("0dJFIeW64bTgtjTU") }
func NewEncoding() (cryptu.Base64Encoding, error) {
	return cryptu.NewBase64Encoding(base64.StdEncoding)
}

var defs = []*di.Def{
	{NewKey, di.Singleton},
	{NewEncoding, di.Singleton},
}

func errFn(err *di.ErrResolve, w http.ResponseWriter, r *http.Request) {}

func main() {
	resolver, err := di.NewResolver(errFn, cryptu.NewDiDefs(), defs)

	if err != nil {
		log.Fatal(err)
	}

	resolver.Invoke(func(encoder cryptu.Base64) {
		secret := "my secret"

		encryptedValue, err := encoder.Encrypt(secret)

		if err != nil {
			log.Fatal(err)
		}

		fmt.Println(encryptedValue == secret)

		decodedValue, err := base64.StdEncoding.DecodeString(encryptedValue)

		if err != nil {
			log.Fatal(err)
		}

		fmt.Println(string(decodedValue) == secret)

		decryptedSecret, err := encoder.Decrypt(encryptedValue)

		if err != nil {
			log.Fatal(err)
		}

		fmt.Println(decryptedSecret)
	})

}
Output:

false
false
my secret

func NewBase64

func NewBase64(cipher Symmetric, encoding Base64Encoding) Base64

NewBase64 returns a new implementation instance of Base64.

type Base64Encoding

type Base64Encoding interface {
	// Encoding returns the *Encoding value
	Encoding() *base64.Encoding
}

Base64Encoding is an injectable wrapper around the encoding used by Base64.

func NewBase64Encoding

func NewBase64Encoding(encoding *base64.Encoding) (Base64Encoding, error)

NewBase64Encoding returns a new instance of Base64Encoding from an existing encoding.

type Key

type Key interface {
	// Bytes returns the encryption key byte slice.
	Bytes() []byte
}

Key represents the symmetric encryption key used in operations by this package.

func NewKey

func NewKey(val []byte) (Key, error)

NewKey returns a new Key from a byte slice.

func NewStrKey

func NewStrKey(str string) (Key, error)

NewStrKey creates a new Key from a string.

type Symmetric

type Symmetric interface {
	// Decrypt decrypts a byte slice encrypted with Encrypt() and
	// returns the unencrypted result.
	Decrypt([]byte) ([]byte, error)

	// Encrypt encrypts the byte slice using the underlying
	// algorithm, returning the encrypted bytes.
	Encrypt([]byte) ([]byte, error)
}

Symmetric represents an implementation of a symmetric encryption algorithm.

func NewAes

func NewAes(key Key) (Symmetric, error)

NewAes returns a new instance of a Symmetric implemented using the crypto/aes package. The key must be 16, 24, or 32 bytes in length. Please see the crypto/aes package for details.

Jump to

Keyboard shortcuts

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