keygrip

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2020 License: MIT Imports: 6 Imported by: 4

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 {
	sync.RWMutex
	// contains filtered or unexported fields
}

Keygrip keygrip struct

func New

func New(keys []string) *Keygrip

New Create a new keygrip

func (*Keygrip) AddKey

func (kg *Keygrip) AddKey(key string)

AddKey add key for hash

func (*Keygrip) Index

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

Index get the valid index of key

func (*Keygrip) Keys

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

Keys get the key of keygrip

func (*Keygrip) RemoveAllKeys

func (kg *Keygrip) RemoveAllKeys()

RemoveAllKeys remove all keys

func (*Keygrip) RemoveKey

func (kg *Keygrip) RemoveKey(key string)

RemoveKey remove key for hash

func (*Keygrip) Sign

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

Sign get the sign of data

func (*Keygrip) Verify

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

Verify verify the data is valid

Jump to

Keyboard shortcuts

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