Compare commits
No commits in common. "master" and "v0.1.3" have entirely different histories.
28
sign.go
28
sign.go
@ -6,6 +6,7 @@ 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"
|
||||||
@ -21,11 +22,6 @@ 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
|
||||||
@ -56,7 +52,7 @@ func (tc TokenCoder) SeedHex() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Format(token string) (txt string, err error) {
|
func Format(token string) (txt string, err error) {
|
||||||
var tp tokenPrint
|
var payload interface{}
|
||||||
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 {
|
||||||
@ -64,14 +60,10 @@ func Format(token string) (txt string, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
buf := bytes.NewBuffer(nil)
|
buf := bytes.NewBuffer(nil)
|
||||||
var cHandle codec.CborHandle
|
var handle 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, &cHandle)
|
dec := codec.NewDecoder(buf, &handle)
|
||||||
|
|
||||||
var td tokenData
|
var td tokenData
|
||||||
err = dec.Decode(&td)
|
err = dec.Decode(&td)
|
||||||
@ -79,14 +71,18 @@ 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(&tp.Payload)
|
err = dec.Decode(&payload)
|
||||||
|
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
enc := codec.NewEncoder(buf, &jHandle)
|
buf.Write([]byte("SIGNATURE=" + hex.EncodeToString(td.Signature) + "\n"))
|
||||||
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
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user