miller

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

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

Go to latest
Published: Jun 2, 2019 License: BSD-3-Clause Imports: 8 Imported by: 0

README

miller

Package miller implements tamper resistant message signing and verification.

Usage

The default tag is used for this example. Tags can be used to namespace your tokens. For example, if you issue account confirmation and recovery tokens, different tags can be used so that confirmation tokens cannot be used for account recovery.

t := miller.New(miller.DefaultTag, []byte("secret"))
token, err := t.Sign("foo")
if err != nil {
  log.Fatal(err)
}
var s string
err = t.Verify(token, &s)
if err != nil {
  log.Fatal(err)
}
// s == "foo"

Documentation

Overview

Package miller implements tamper resistant message signing and verification.

Index

Constants

View Source
const DefaultTag = "miller"

DefaultTag represents the default key derivation tag.

Variables

View Source
var (
	// ErrInvalid represents an unprocessable token error.
	ErrInvalid = errors.New("miller: invalid token")

	// ErrSignature represents an invalid signature error.
	ErrSignature = errors.New("miller: invalid signature")
)

Functions

func GenerateKey

func GenerateKey() []byte

GenerateKey returns a 256-bit key suitable for hashing with.

Types

type DefaultSerializer

type DefaultSerializer struct{}

DefaultSerializer is the default token serializer. This serializer leverages encoding/json for fast serialization.

func (DefaultSerializer) Decode

func (s DefaultSerializer) Decode(b []byte, v interface{}) error

Decode implements the Serializer interface.

func (DefaultSerializer) Encode

func (s DefaultSerializer) Encode(v interface{}) ([]byte, error)

Encode implements the Serializer interface.

type Option

type Option func(*Token)

Option represents a functional option for configuration.

func SetSerializer

func SetSerializer(s Serializer) Option

SetSerializer sets the serializer for token signing and verifying.

type Serializer

type Serializer interface {
	// Encode serializes v into a byte slice.
	Encode(v interface{}) ([]byte, error)

	// Decode deserializes b and stores the result in the value pointed to by v.
	Decode(b []byte, v interface{}) error
}

Serializer represents the ability to encode and decode data.

type Token

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

Token represents the configuration for signing and verifying token values.

func New

func New(tag string, key []byte, opts ...Option) *Token

New returns a new Token.

func (*Token) Sign

func (t *Token) Sign(v interface{}) (string, error)

Sign returns a token comprised of the serialized contents of v and the cryptographic signature later used to verify that the payload has not been tampered.

func (*Token) Verify

func (t *Token) Verify(token string, v interface{}) error

Verify parses a token and returns an error if the token or signature is invalid. If the signature is valid, the decoded payload is stored in the value pointed to by v.

Jump to

Keyboard shortcuts

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