Closed x448 closed 4 years ago
Example decoding CWT (CBOR Web Token) using keyasint
and toarray
struct tags.
// Example from RFC 8392 A.3. Example Signed CWT
// Partial COSE header definition
type coseHeader struct {
Alg int `cbor:"1,keyasint"`
Kid []byte `cbor:"4,keyasint"`
IV []byte `cbor:"5,keyasint"`
}
// fxamacker/cbor v1.3 has "toarray" struct tag
type signedCWT struct {
_ struct{} `cbor:",toarray"`
Protected []byte
Unprotected coseHeader
Payload []byte
Signature []byte
}
// When cborData is []byte containing a signed CWT
// it can be decoded into signedCTW struct easily
var v signedCWT
if err := cbor.Unmarshal(cborData, &v); err != nil {
t.Fatal("Unmarshal:", err)
}
// That's it! You get fast decoding and easy-to-use struct.
Both SenML and CWT decoding examples are in pull request. Added to Usage section.
Add this to Usage section today? Last code-related commit has been fuzzing nonstop for days without single crash and speed is fast with this feature!
How to Decode SenML with fxamacker/cbor v1.3