simplebox

package module
v0.0.0-...-84e9865 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2015 License: MIT Imports: 3 Imported by: 2

README

simplebox

build status GoDoc

Package simplebox provides a simple, easy-to-use cryptographic API where all of the hard decisions have been made for you in advance. The backing cryptography is XSalsa20 and Poly1305, which are known to be secure and fast.

This package is a Golang port of the RbNaCl module of the same name.

Installation and Usage

go get github.com/brandur/simplebox

Please see godoc for usage information and examples.

Documentation

Overview

Package simplebox provides a simple, easy-to-use cryptographic API where all of the hard decisions have been made for you in advance. The backing cryptography is XSalsa20 and Poly1305, which are known to be secure and fast.

This package uses NaCl's secretbox under the hood, but also includes a simple yet secure nonce generation strategy. A 24-byte random nonce is generated from a secure source, used to encrypt a message, and prepended to the resulting ciphertex. When it's time for decryption, the message is split back into nonce and ciphertext, and the message is decrypted.

Thanks to the size of the nonce, the chance of a collision is negligible. For example, after encrypting 2^64 messages, the odds of there having been a repeated nonce is approximately 2^-64.

Note that although this strategy assures the confidentiality of your messages, it doesn't provide any protection against messages being reordered and replayed by an active adversary.

This idea is entirely based on the SimpleBox implementation included with RbNaCl: https://github.com/cryptosphere/rbnacl/wiki/SimpleBox

Index

Examples

Constants

View Source
const (
	// Length in bytes of a secret key used for encryption and decryption.
	KeySize = 32

	// Length in bytes of a nonce value (which must be unique and may be
	// random) used for encryption and decryption.
	NonceSize = 24
)

Variables

This section is empty.

Functions

This section is empty.

Types

type SimpleBox

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

SimpleBox provides a simple wrapper around NaCl's secretbox with a self-contained random nonce strategy.

Example
package main

import (
	"crypto/rand"
	"fmt"

	"github.com/brandur/simplebox"
)

func main() {
	message := "hello"

	var secretKey [simplebox.KeySize]byte
	rand.Reader.Read(secretKey[:])
	box := simplebox.NewFromSecretKey(&secretKey)

	// Encrypt
	ciphertext := box.Encrypt([]byte(message))

	// Decrypt
	decrypted, err := box.Decrypt(ciphertext)
	if err != nil {
		panic(err)
	}

	// Prints:
	//
	// Decrypted: hello
	fmt.Printf("Decrypted: %v\n", decrypted)
}
Output:

func NewFromSecretKey

func NewFromSecretKey(secretKey *[KeySize]byte) *SimpleBox

Creates a SimpleBox from a secret key.

func (*SimpleBox) Decrypt

func (b *SimpleBox) Decrypt(cipher []byte) ([]byte, error)

Decrypts the given ciphertext and returns plaintext. An appropriate error is included if decryption failed.

func (*SimpleBox) Encrypt

func (b *SimpleBox) Encrypt(plain []byte) []byte

Encrypts the given plaintext and returns ciphertext.

Jump to

Keyboard shortcuts

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