keygrip

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2021 License: MIT Imports: 6 Imported by: 2

README

keygrip

Build Status

Keygrip is a module for signing and verifying data (such as cookies or URLs) through a rotating credential system, in which new server keys can be added and old ones removed regularly, without invalidating client credentials. It derives from crypto-utils/keygrip.

API

kg := keygrip.New(keyList []string)
kg := keygrip.New([]string{
    "key1",
    "key2",
})
Sign(data []byte)

Get the base64 digest(RawURLEncoding) on the first key in the keylist.

kg := keygrip.New([]string{
    "key1",
    "key2",
})
str := kg.Sign([]byte("tree.xie"))
// VOauNTAF3i24kD9EN5foGvhXNnI
fmt.Println(string(str))
Verify(data, digest []byte)

This loops through all of the keys currently in the keylist until the digest of the current key matches the given digest. Otherwise it will return false.

kg := keygrip.New([]string{
    "key1",
    "key2",
})
fmt.Println(kg.Verify([]byte("tree.xie"), []byte( "VOauNTAF3i24kD9EN5foGvhXNnI")))
Index(data, digest []byte)

This loops through all of the keys currently in the keylist until the digest of the current key matches the given digest, at which point the current index is returned. If no key is matched, -1 is returned.

kg := keygrip.New([]string{
    "key1",
    "key2",
})
fmt.Println(kg.Index([]byte("tree.xie"), []byte("VOauNTAF3i24kD9EN5foGvhXNnI")))
AddKey(key string)

Add key to the front of key list

kg := keygrip.New([]string{
    "key1",
    "key2",
})
kg.Add("key3")
RemoveKey(key string)

Remove key from key list

kg := keygrip.New([]string{
    "key1",
    "key2",
})
kg.Remove("key1")
Keys()

Get the key list

kg := keygrip.New([]string{
    "key1",
    "key2",
})
kg.Keys()
RemoveAllKeys()

Remove all keys

kg := keygrip.New([]string{
    "key1",
    "key2",
})
kg.RemoveAllKeys()

test

make test-cover

bench

make bench

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Keygrip

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

Keygrip keygrip struct

func New

func New(keys []string) *Keygrip

New returns a new keygrip

func NewRWMutex added in v1.2.0

func NewRWMutex(keys []string) *Keygrip

NewRWMutex returns a new keygrip with rw mutex

func (*Keygrip) AddKey

func (kg *Keygrip) AddKey(key string)

AddKey adds the key to keygrip

func (*Keygrip) Index

func (kg *Keygrip) Index(data, digest []byte) int

Index returns the index of the key which match digest. It will return -2 if the digest isn't raw url encoding. It will return -1 if no match key for the digest.

func (*Keygrip) Keys

func (kg *Keygrip) Keys() []string

Keys returns the key list of keygrip

func (*Keygrip) RemoveAllKeys

func (kg *Keygrip) RemoveAllKeys()

RemoveAllKeys removes all keys

func (*Keygrip) RemoveKey

func (kg *Keygrip) RemoveKey(key string)

RemoveKey removes the key from keygrip

func (*Keygrip) SetKeys added in v1.2.1

func (kg *Keygrip) SetKeys(keys []string)

SetKeys sets the keys of keygrip

func (*Keygrip) Sign

func (kg *Keygrip) Sign(data []byte) []byte

Sign returns the sign data using the first key

func (*Keygrip) Verify

func (kg *Keygrip) Verify(data, digest []byte) bool

Verify returns true if the disgest is created by keygrip

Jump to

Keyboard shortcuts

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