eitherbox

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2021 License: MIT Imports: 4 Imported by: 0

README

eitherbox

Go Reference Go Report Card CircleCI codecov

A NaCL based secret box that can be opened with either of two keys.

Example:

package main

import (
	"crypto/rand"
	"fmt"

	"github.com/mrobinsn/eitherbox"
	"golang.org/x/crypto/nacl/box"
)

func main() {
	// Create keys for Alice
	alicePublic, alicePrivate, _ := box.GenerateKey(rand.Reader)

	// Create keys for Bob
	bobPublic, bobPrivate, _ := box.GenerateKey(rand.Reader)

	// Create keys for Eve
	evePublic, evePrivate, _ := box.GenerateKey(rand.Reader)

	secret := []byte("hello world")

	ebox := eitherbox.Encrypt(secret, alicePublic, bobPublic)

	// Alice can decrypt
	aliceMsg, _ := ebox.Decrypt(alicePublic, alicePrivate)

	// Bob can decrypt
	bobMsg, _ := ebox.Decrypt(bobPublic, bobPrivate)

	// Eve can't decrypt
	eveMsg, _ := ebox.Decrypt(evePublic, evePrivate)

	fmt.Println("Alice got:", string(aliceMsg))
	fmt.Println("Bob got:", string(bobMsg))
	fmt.Println("Eve got:", string(eveMsg))
	// Output: Alice got: hello world
	// Bob got: hello world
	// Eve got:
}

Documentation

Overview

Package eitherbox provides a cryptographically secure composition of NaCL secretbox that can be opened by either of two key holders.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Box

type Box []byte

Box is a cryptographically secure box that can be opened with one of two keys.

func Encrypt

func Encrypt(b []byte, k1, k2 Key) Box

Encrypt encrypts `b` into a package that can be decrypted by either public key. Public/private keypairs should be generated with box.GenerateKey(..).

Example
// Create keys for Alice
alicePublic, alicePrivate, _ := box.GenerateKey(rand.Reader)

// Create keys for Bob
bobPublic, bobPrivate, _ := box.GenerateKey(rand.Reader)

// Create keys for Eve
evePublic, evePrivate, _ := box.GenerateKey(rand.Reader)

secret := []byte("hello world")

twokeyBox := Encrypt(secret, alicePublic, bobPublic)

// Alice can decrypt
aliceMsg, _ := twokeyBox.Decrypt(alicePublic, alicePrivate)

// Bob can decrypt
bobMsg, _ := twokeyBox.Decrypt(bobPublic, bobPrivate)

// Eve can't decrypt
eveMsg, _ := twokeyBox.Decrypt(evePublic, evePrivate)

fmt.Println("Alice got:", string(aliceMsg))
fmt.Println("Bob got:", string(bobMsg))
fmt.Println("Eve got:", string(eveMsg))
Output:

Alice got: hello world
Bob got: hello world
Eve got:

func (Box) Decrypt

func (b Box) Decrypt(pub, prv Key) ([]byte, error)

Decrypt returns the original plaintext, given that this key is one of the original two keys used to create the box.

type Key

type Key *[32]byte

Key represents a NaCL compatible key

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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