signing

package module
v0.0.0-...-449fd9a Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2019 License: MIT Imports: 10 Imported by: 1

README

WARNING! This repo is a work in progress, things may be broken, api will change, tests and documentation are missing.

Signing

Utility for creating and restoring url-safe signed JSON objects.

Based on Django's signing.py utility.

Documentation

Read the documentation at godoc.org.

Usage

func ExampleNewSigner() {
	var key string = "v5kyWAnOBiEKVpAZmMC03BY18Fi6u1ALuZZUb0gnU7Q="
	var salt string = "Gm8uSwfozUKXXatEJLpBB1cNq0F0AR1U7LRaqeO+Tn8="
	var sep string = ":"

	signer, err := signing.NewSigner(key, sep, salt)
	if err != nil {
		panic(err)
	}

	signed := signer.Sign("the-quick-brown-fox")
	fmt.Print(signed)

	unsigned, err := signer.Unsign(signed)
	switch err {
	case signing.ErrBadSignature:
		fmt.Printf("Unsign error: %v", err)
	default:
		panic(err)
	}
	fmt.Print(unsigned)

	// Output:
	// the-quick-brown-fox:-maTzDzCZGpiiLqm6SZr0KkMfBo
}

Documentation

Overview

Utility for creating and restoring url-safe signed JSON objects.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidKey            = errors.New("Invalid key: key can not be an empty string")
	ErrInvalidSalt           = errors.New("Invalid salt: salt can not be an empty string")
	ErrUnsafeSignerSeparator = errors.New("Unsafe Signer separator: cannot be empty or consist of only A-z0-9-_=")
	ErrBadSignature          = errors.New("Signature not valid")
)
View Source
var (
	ErrSignatureExpired = errors.New("Signature expired")
)

Functions

func Base64Hmac

func Base64Hmac(salt, value, key []byte) string

func SaltedHmac

func SaltedHmac(salt, value, secret []byte) []byte

Types

type SignUnsigner

type SignUnsigner interface {
	Sign(value string) string
	Unsign(value string) (string, error)
}

type Signer

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

func NewSigner

func NewSigner(key, sep, salt string) (*Signer, error)
Example
package main

import (
	"fmt"

	"github.com/enricofoltran/signing"
)

func main() {
	var key string = "v5kyWAnOBiEKVpAZmMC03BY18Fi6u1ALuZZUb0gnU7Q="
	var salt string = "Gm8uSwfozUKXXatEJLpBB1cNq0F0AR1U7LRaqeO+Tn8="
	var sep string = ":"

	signer, err := signing.NewSigner(key, sep, salt)
	if err != nil {
		panic(err)
	}

	signed := signer.Sign("the-quick-brown-fox")
	fmt.Print(signed)

	unsigned, err := signer.Unsign(signed)
	switch err {
	case signing.ErrBadSignature:
		fmt.Printf("Unsign error: %v", err)
	default:
		panic(err)
	}
	fmt.Print(unsigned)

}
Output:

the-quick-brown-fox:-maTzDzCZGpiiLqm6SZr0KkMfBo

func (*Signer) Sign

func (ps *Signer) Sign(value string) string

func (*Signer) Unsign

func (ps *Signer) Unsign(signed string) (string, error)

type TimestampSigner

type TimestampSigner struct {
	Signer
}

func NewTimestampSigner

func NewTimestampSigner(key, sep, salt string) (*TimestampSigner, error)

func (*TimestampSigner) Sign

func (ts *TimestampSigner) Sign(value string) string

func (*TimestampSigner) UnsignMaxAge

func (ts *TimestampSigner) UnsignMaxAge(signed string, maxAge time.Duration) (string, error)

Jump to

Keyboard shortcuts

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