forked from aidantwoods/go-paseto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v3_payloads.go
59 lines (43 loc) · 1.28 KB
/
v3_payloads.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package paseto
import (
"github.com/pkg/errors"
)
type v3PublicPayload struct {
message []byte
signature [96]byte
}
func (p v3PublicPayload) bytes() []byte {
return append(p.message, p.signature[:]...)
}
func newV3PublicPayload(bytes []byte) (v3PublicPayload, error) {
signatureOffset := len(bytes) - 96
if signatureOffset < 0 {
return v3PublicPayload{}, errors.New("Payload is not long enough to be a valid Paseto message")
}
message := make([]byte, len(bytes)-96)
copy(message, bytes[:signatureOffset])
var signature [96]byte
copy(signature[:], bytes[signatureOffset:])
return v3PublicPayload{message, signature}, nil
}
type v3LocalPayload struct {
nonce [32]byte
cipherText []byte
tag [48]byte
}
func (p v3LocalPayload) bytes() []byte {
return append(append(p.nonce[:], p.cipherText...), p.tag[:]...)
}
func newV3LocalPayload(bytes []byte) (v3LocalPayload, error) {
if len(bytes) <= 32+48 {
return v3LocalPayload{}, errors.New("Payload is not long enough to be a valid Paseto message")
}
macOffset := len(bytes) - 48
var nonce [32]byte
copy(nonce[:], bytes[0:32])
cipherText := make([]byte, macOffset-32)
copy(cipherText, bytes[32:macOffset])
var tag [48]byte
copy(tag[:], bytes[macOffset:])
return v3LocalPayload{nonce, cipherText, tag}, nil
}