otp

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

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

Go to latest
Published: Apr 21, 2014 License: MPL-2.0 Imports: 1 Imported by: 0

README

go-otp Build Status

OTP ("One-Time Pad") Utility for Go

This simple Go package implements an OTP container to ease the process of creating and utilizing single-use tokens. One-time pads are useful in cryptology to create perfect forward secrecy systems. While this implementation does not deter you from re-using keys, it makes switching keys or "pages" out simple and easy, and lends itself well to the concept in general.

Internals

A likely use case in using OTP is generating enough "pages" of cryptographically secure random bytes to permit encrypting/decrypting many messages. In go-otp, you generate some random bytes, and then use them to create a otp.Pad. Within this pad, an internal pointer keeps track of used pads. This pointer may be passed in during creation of an otp.Pad, allowing the same pad data to be used until it has been exhausted. This makes it easy to pre-share many pages of OTP material in advance, and utilize the pages over time.

go-otp also provides Encrypt and Decrypt methods to allow seamless use of OTP pads. These methods implement combining the plaintext and crypto page using modular addition. The resulting ciphertext is information theoretically secure.

Usage

func NewPad(material []byte, pageSize int, startPage int) (*Pad, error)
type Pad
    func (p *Pad) TotalPages() int
    func (p *Pad) RemainingPages() int
    func (p *Pad) CurrentPage() int
    func (p *Pad) NextPage() error
    func (p *Pad) SetPage(page int) error
    func (p *Pad) Encrypt(payload []byte) ([]byte, error)
    func (p *Pad) Decrypt(payload []byte) ([]byte, error)

Example

Following is a basic usage example which creates enough page material to facilitate 2048 unique one-time use byte slices, each 16 bytes in size. The pad is then used to perform 2 encryption routines, and some status is printed at the end.

package main

import (
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"github.com/ryanuber/go-otp"
)

func main() {
	m := make([]byte, 16*2048)
	rand.Read(m)

	// Create a new pad with 16-byte pages. Set pointer to page 1.
	pad, err := otp.NewPad(m, 16, 1)
	if err != nil {
		fmt.Printf("%s", err)
		return
	}

	encrypted, _ := pad.Encrypt([]byte("this is a test"))
	fmt.Println(base64.StdEncoding.EncodeToString(encrypted))
	decrypted, _ := pad.Decrypt(encrypted)
	fmt.Printf("%s\n\n", decrypted)

	pad.NextPage()

	encrypted, _ = pad.Encrypt([]byte("this is a test"))
	fmt.Println(base64.StdEncoding.EncodeToString(encrypted))
	decrypted, _ = pad.Decrypt(encrypted)
	fmt.Printf("%s\n\n", decrypted)

	fmt.Printf("Total pages: %d\n", pad.TotalPages())
	fmt.Printf("Page pointer: %d\n", pad.CurrentPage())
	fmt.Printf("Remaining pages: %d\n", pad.RemainingPages())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pad

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

func NewPad

func NewPad(material []byte, pageSize int, startPage int) (*Pad, error)

NewPad creates a new "one-time pad" by accepting arbitrary bytes from the user and setting up pages from it. This method also allows passing the page pointer value to make resuming an existing pad easy.

func (*Pad) CurrentPage

func (p *Pad) CurrentPage() int

CurrentPage returns the current position of the page pointer

func (*Pad) Decrypt

func (p *Pad) Decrypt(payload []byte) ([]byte, error)

Decrypt will accept a byte slice and reverse the process taken by Encode to translate encrypted text back into raw bytes. It is required that the page pointer be set to the same position as it was during Encode().

func (*Pad) Encrypt

func (p *Pad) Encrypt(payload []byte) ([]byte, error)

Encrypt will take a byte slice and use modular addition to encrypt the payload using the current page.

func (*Pad) NextPage

func (p *Pad) NextPage() error

NextPage will advance the page pointer

func (*Pad) RemainingPages

func (p *Pad) RemainingPages() int

RemainingPages returns the number of unused pages in the pad

func (*Pad) SetPage

func (p *Pad) SetPage(page int) error

SetPage will set the page pointer

func (*Pad) TotalPages

func (p *Pad) TotalPages() int

TotalPages returns the number of pages in the pad

Jump to

Keyboard shortcuts

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