Documentation
¶
Overview ¶
Example (Plain) ¶
package main
import (
"encoding/hex"
"fmt"
"github.com/andreyvit/signedstrings"
)
var exampleKey = must(hex.DecodeString("d850af431064164d9a73891fa0a257ba91e5cb18a67de07d3507b8ccdc8781c2"))
func main() {
conf := signedstrings.Configuration{
Keys: [][]byte{exampleKey},
Sep: " :: ",
}
fmt.Println(conf.Sign("some text to sign"))
print(conf.Validate("some text to sign :: 2f9a0cb84617f6e394a22068504f59ba3e7903c4dc1fd995cc4a940ffeef90d8"))
print(conf.Validate(" :: "))
print(conf.Validate("some text to sign"))
print(conf.Validate("some text to sign :: 1111111111111111111111111111111111111111111111111111111111111111"))
}
func print(v any, err error) {
if err != nil {
fmt.Println("err: " + err.Error())
} else {
fmt.Println(v)
}
}
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
Output: some text to sign :: 2f9a0cb84617f6e394a22068504f59ba3e7903c4dc1fd995cc4a940ffeef90d8 some text to sign err: invalid string err: invalid string err: invalid signature
Example (Token) ¶
package main
import (
"encoding/hex"
"fmt"
"github.com/andreyvit/signedstrings"
)
var exampleKey = must(hex.DecodeString("d850af431064164d9a73891fa0a257ba91e5cb18a67de07d3507b8ccdc8781c2"))
func main() {
conf := signedstrings.Configuration{
Keys: [][]byte{exampleKey},
Prefixes: []string{"TOKEN-"},
}
fmt.Println(conf.Sign("foo"))
print(conf.Validate("TOKEN-foo-4bc019e2218479926f27694a281b8b2af30f86f5f522d0bbde31ab19bc730f39"))
print(conf.Validate(""))
print(conf.Validate("foo-4bc019e2218479926f27694a281b8b2af30f86f5f522d0bbde31ab19bc730f39"))
print(conf.Validate("TOKEN-foo-1111111111111111111111111111111111111111111111111111111111111111"))
}
func print(v any, err error) {
if err != nil {
fmt.Println("err: " + err.Error())
} else {
fmt.Println(v)
}
}
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
Output: TOKEN-foo-4bc019e2218479926f27694a281b8b2af30f86f5f522d0bbde31ab19bc730f39 foo err: invalid string err: invalid string err: invalid signature
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // Invalid is the error returned for incorrectly formatted messages. Invalid = errors.New("invalid string") // InvalidSig is the error returned for correctly formatted messages that // fail signature validation (i.e. have been corrupted or tampered with). InvalidSig = errors.New("invalid signature") )
View Source
var MinKeyLen = 32
Minimum acceptable length of secure **fully random** keys. Note that this is a variable, so you can adjust it if desired.
Functions ¶
This section is empty.
Types ¶
type Configuration ¶
type Configuration struct {
// Keys are accepted when validating signatures. The first key is the one used
// when signing new messages. Multiple valid keys allow for key rotation.
Keys Keys
// Prefixes are added in front of the tokens to help identify them.
// The first one is used for new tokens. Others are accepted when
// validating tokens to allow prefix changes.
//
// An empty prefix is a valid choice. Omitting this field is the same as
// specifying a single empty prefix.
Prefixes []string
// Sep is the separator between the data and the signature, a cosmetic choice.
// Defaults to a dash.
Sep string
}
func (*Configuration) Sign ¶
func (conf *Configuration) Sign(data string) string
Sign signs the given string (and adds a configured prefix if any).
type Keys ¶
type Keys [][]byte
Keys is a convenience type for a list of []byte keys. Can be used with flag.Var and its compatibles. Defines a sensible String().
Example ¶
package main
import (
"flag"
"fmt"
"github.com/andreyvit/signedstrings"
)
func main() {
var keys signedstrings.Keys
flags := flag.NewFlagSet("", flag.PanicOnError)
flags.Var(&keys, "keys", "explanation")
flags.Parse([]string{"-keys", "d850af431064164d9a73891fa0a257ba91e5cb18a67de07d3507b8ccdc8781c2,65ce238cb1b11d17a00c94c875394f500b05abd24c276a01691bdf9ce00d213c"})
fmt.Println(keys)
}
Output: d850af431064164d9a73891fa0a257ba91e5cb18a67de07d3507b8ccdc8781c2 65ce238cb1b11d17a00c94c875394f500b05abd24c276a01691bdf9ce00d213c
func ParseKeys ¶
ParseKeys parses a comma or whitespace-separated list of hex-encoded keys.
Example ¶
package main
import (
"encoding/hex"
"fmt"
"github.com/andreyvit/signedstrings"
)
func main() {
// WARNING: use longer keys, these are very short, for demonstration only
keys, err := signedstrings.ParseKeys("d850af431064164d9a73891fa0a257ba91e5cb18a67de07d3507b8ccdc8781c2 65ce238cb1b11d17a00c94c875394f500b05abd24c276a01691bdf9ce00d213c,,,283d54389c394ed33ba4146eff7b4133f7e393cb905d089a06798456a1cb7dcd")
if err != nil {
panic(err)
}
fmt.Println(hex.EncodeToString(keys[0]))
fmt.Println(hex.EncodeToString(keys[1]))
fmt.Println(hex.EncodeToString(keys[2]))
print(signedstrings.ParseKeys("zzz"))
print(signedstrings.ParseKeys("d850"))
}
func print(v any, err error) {
if err != nil {
fmt.Println("err: " + err.Error())
} else {
fmt.Println(v)
}
}
Output: d850af431064164d9a73891fa0a257ba91e5cb18a67de07d3507b8ccdc8781c2 65ce238cb1b11d17a00c94c875394f500b05abd24c276a01691bdf9ce00d213c 283d54389c394ed33ba4146eff7b4133f7e393cb905d089a06798456a1cb7dcd err: encoding/hex: invalid byte: U+007A 'z' err: 2-byte key is too short, need at least 32 bytes
Click to show internal directories.
Click to hide internal directories.