auto seed

This commit is contained in:
Roy Olav Purser 2022-02-27 20:21:58 +01:00
parent a6c329054e
commit 640491d424
Signed by: roypur
GPG Key ID: E14D26A036F21656

37
sign.go
View File

@ -7,7 +7,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt"
"github.com/ugorji/go/codec" "github.com/ugorji/go/codec"
"hash/crc64" "hash/crc64"
"strings" "strings"
@ -51,21 +50,13 @@ func (tc TokenCoder) SeedHex() string {
return hex.EncodeToString(tc.Seed()) return hex.EncodeToString(tc.Seed())
} }
func NewTokenCoder(seed []byte, pubKeys ...[]byte) (tc TokenCoder, err error) { func NewTokenCoderWithSeed(seed []byte, pubKeys ...[]byte) (tc TokenCoder, err error) {
tc.pubKeys = make([]ed25519.PublicKey, len(pubKeys)+1, len(pubKeys)+1)
if len(seed) == 0 {
seed = make([]byte, ed25519.SeedSize, ed25519.SeedSize)
_, err = rand.Read(seed)
if err != nil {
return
}
}
if len(seed) != ed25519.SeedSize { if len(seed) != ed25519.SeedSize {
err = errors.New("Incorrect seed size") err = errors.New("Incorrect seed size")
return return
} }
tc.pubKeys = make([]ed25519.PublicKey, len(pubKeys)+1, len(pubKeys)+1)
tc.privKey = ed25519.NewKeyFromSeed(seed) tc.privKey = ed25519.NewKeyFromSeed(seed)
tc.pubKeys[len(pubKeys)] = tc.privKey.Public().(ed25519.PublicKey) tc.pubKeys[len(pubKeys)] = tc.privKey.Public().(ed25519.PublicKey)
@ -80,7 +71,16 @@ func NewTokenCoder(seed []byte, pubKeys ...[]byte) (tc TokenCoder, err error) {
return return
} }
func NewTokenCoderHex(seed string, pubKeys ...string) (tc TokenCoder, err error) { func NewTokenCoder(pubKeys ...[]byte) (tc TokenCoder, err error) {
seed := make([]byte, ed25519.SeedSize, ed25519.SeedSize)
_, err = rand.Read(seed)
if err != nil {
return
}
return NewTokenCoderWithSeed(seed, pubKeys...)
}
func NewTokenCoderHexWithSeed(seed string, pubKeys ...string) (tc TokenCoder, err error) {
var rawSeed []byte var rawSeed []byte
rawPubKeys := make([][]byte, len(pubKeys), len(pubKeys)) rawPubKeys := make([][]byte, len(pubKeys), len(pubKeys))
rawSeed, err = hex.DecodeString(seed) rawSeed, err = hex.DecodeString(seed)
@ -93,7 +93,18 @@ func NewTokenCoderHex(seed string, pubKeys ...string) (tc TokenCoder, err error)
return return
} }
} }
return NewTokenCoder(rawSeed, rawPubKeys...) return NewTokenCoderWithSeed(rawSeed, rawPubKeys...)
}
func NewTokenCoderHex(pubKeys ...string) (tc TokenCoder, err error) {
rawPubKeys := make([][]byte, len(pubKeys), len(pubKeys))
for pos, pubKey := range pubKeys {
rawPubKeys[pos], err = hex.DecodeString(pubKey)
if err != nil {
return
}
}
return NewTokenCoder(rawPubKeys...)
} }
func trim(r rune) bool { func trim(r rune) bool {