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