-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
v4_payloads.go
57 lines (41 loc) · 1.19 KB
/
v4_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
package paseto
import t "aidanwoods.dev/go-result"
type v4PublicPayload struct {
message []byte
signature [64]byte
}
func (p v4PublicPayload) bytes() []byte {
return append(p.message, p.signature[:]...)
}
func newV4PublicPayload(bytes []byte) t.Result[v4PublicPayload] {
signatureOffset := len(bytes) - 64
if signatureOffset < 0 {
return t.Err[v4PublicPayload](errorPayloadShort)
}
message := make([]byte, len(bytes)-64)
copy(message, bytes[:signatureOffset])
var signature [64]byte
copy(signature[:], bytes[signatureOffset:])
return t.Ok(v4PublicPayload{message, signature})
}
type v4LocalPayload struct {
nonce [32]byte
cipherText []byte
tag [32]byte
}
func (p v4LocalPayload) bytes() []byte {
return append(append(p.nonce[:], p.cipherText...), p.tag[:]...)
}
func newV4LocalPayload(bytes []byte) t.Result[v4LocalPayload] {
if len(bytes) <= 32+32 {
return t.Err[v4LocalPayload](errorPayloadShort)
}
macOffset := len(bytes) - 32
var nonce [32]byte
copy(nonce[:], bytes[0:32])
cipherText := make([]byte, macOffset-32)
copy(cipherText, bytes[32:macOffset])
var tag [32]byte
copy(tag[:], bytes[macOffset:])
return t.Ok(v4LocalPayload{nonce, cipherText, tag})
}