Compare commits

...

2 Commits

Author SHA1 Message Date
74ffaa6d22
improve print 2022-04-03 17:02:09 +02:00
e5e9cfd9f5
change json encoder 2022-04-03 16:52:26 +02:00

28
sign.go
View File

@ -6,7 +6,6 @@ import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"github.com/ugorji/go/codec"
"hash/crc64"
@ -22,6 +21,11 @@ type tokenData struct {
Payload []byte `codec:"p"`
}
type tokenPrint struct {
Signature string `codec:"signature"`
Payload interface{} `codec:"payload"`
}
type TokenCoder struct {
valid bool
privKey ed25519.PrivateKey
@ -52,7 +56,7 @@ func (tc TokenCoder) SeedHex() string {
}
func Format(token string) (txt string, err error) {
var payload interface{}
var tp tokenPrint
var data []byte
data, err = b64.DecodeString(strings.TrimFunc(token, trim))
if err != nil {
@ -60,10 +64,14 @@ func Format(token string) (txt string, err error) {
}
buf := bytes.NewBuffer(nil)
var handle codec.CborHandle
var cHandle codec.CborHandle
var jHandle codec.JsonHandle
jHandle.HTMLCharsAsIs = true
jHandle.MapKeyAsString = true
jHandle.Indent = 4
buf.Write(data)
dec := codec.NewDecoder(buf, &handle)
dec := codec.NewDecoder(buf, &cHandle)
var td tokenData
err = dec.Decode(&td)
@ -71,18 +79,14 @@ func Format(token string) (txt string, err error) {
return
}
tp.Signature = hex.EncodeToString(td.Signature)
buf.Reset()
buf.Write(td.Payload)
err = dec.Decode(&payload)
err = dec.Decode(&tp.Payload)
buf.Reset()
buf.Write([]byte("SIGNATURE=" + hex.EncodeToString(td.Signature) + "\n"))
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
enc.SetIndent("", "\t")
err = enc.Encode(payload)
enc := codec.NewEncoder(buf, &jHandle)
err = enc.Encode(tp)
txt = buf.String()
return
}