-
Notifications
You must be signed in to change notification settings - Fork 14
/
fmt.go
179 lines (139 loc) · 4.04 KB
/
fmt.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package object
import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-sdk-go/checksum"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
)
var (
errCheckSumMismatch = errors.New("payload checksum mismatch")
errCheckSumNotSet = errors.New("payload checksum is not set")
errIncorrectID = errors.New("incorrect object identifier")
)
// CalculatePayloadChecksum calculates and returns checksum of
// object payload bytes.
func CalculatePayloadChecksum(payload []byte) checksum.Checksum {
var res checksum.Checksum
checksum.Calculate(&res, checksum.SHA256, payload)
return res
}
// CalculateAndSetPayloadChecksum calculates checksum of current
// object payload and writes it to the object.
func CalculateAndSetPayloadChecksum(obj *Object) {
obj.SetPayloadChecksum(
CalculatePayloadChecksum(obj.Payload()),
)
}
// VerifyPayloadChecksum checks if payload checksum in the object
// corresponds to its payload.
func VerifyPayloadChecksum(obj *Object) error {
actual := CalculatePayloadChecksum(obj.Payload())
cs, set := obj.PayloadChecksum()
if !set {
return errCheckSumNotSet
}
if !bytes.Equal(cs.Value(), actual.Value()) {
return errCheckSumMismatch
}
return nil
}
// CalculateID calculates identifier for the object.
func CalculateID(obj *Object) (oid.ID, error) {
var id oid.ID
id.SetSHA256(sha256.Sum256(obj.ToV2().GetHeader().StableMarshal(nil)))
return id, nil
}
// CalculateAndSetID calculates identifier for the object
// and writes the result to it.
func CalculateAndSetID(obj *Object) error {
id, err := CalculateID(obj)
if err != nil {
return err
}
obj.SetID(id)
return nil
}
// VerifyID checks if identifier in the object corresponds to
// its structure.
func VerifyID(obj *Object) error {
id, err := CalculateID(obj)
if err != nil {
return err
}
oID, set := obj.ID()
if !set {
return errOIDNotSet
}
if !id.Equals(oID) {
return errIncorrectID
}
return nil
}
// CalculateAndSetSignature signs id with provided key and sets that signature to
// the object.
func CalculateAndSetSignature(signer neofscrypto.Signer, obj *Object) error {
oID, set := obj.ID()
if !set {
return errOIDNotSet
}
sig, err := oID.CalculateIDSignature(signer)
if err != nil {
return err
}
obj.SetSignature(&sig)
return nil
}
// VerifyIDSignature verifies object ID signature.
func (o *Object) VerifyIDSignature() bool {
m := (*object.Object)(o)
sigV2 := m.GetSignature()
if sigV2 == nil {
return false
}
idV2 := m.GetObjectID()
if idV2 == nil {
return false
}
var sig neofscrypto.Signature
return sig.ReadFromV2(*sigV2) == nil && sig.Verify(idV2.StableMarshal(nil))
}
// SetIDWithSignature sets object identifier and signature.
func SetIDWithSignature(signer neofscrypto.Signer, obj *Object) error {
if err := CalculateAndSetID(obj); err != nil {
return fmt.Errorf("could not set identifier: %w", err)
}
if err := CalculateAndSetSignature(signer, obj); err != nil {
return fmt.Errorf("could not set signature: %w", err)
}
return nil
}
// SetVerificationFields calculates and sets all verification fields of the object.
func SetVerificationFields(signer neofscrypto.Signer, obj *Object) error {
CalculateAndSetPayloadChecksum(obj)
return SetIDWithSignature(signer, obj)
}
// CheckVerificationFields checks all verification fields of the object.
func CheckVerificationFields(obj *Object) error {
if err := CheckHeaderVerificationFields(obj); err != nil {
return fmt.Errorf("invalid header structure: %w", err)
}
if err := VerifyPayloadChecksum(obj); err != nil {
return fmt.Errorf("invalid payload checksum: %w", err)
}
return nil
}
var errInvalidSignature = errors.New("invalid signature")
// CheckHeaderVerificationFields checks all verification fields except payload.
func CheckHeaderVerificationFields(obj *Object) error {
if !obj.VerifyIDSignature() {
return errInvalidSignature
}
if err := VerifyID(obj); err != nil {
return fmt.Errorf("invalid identifier: %w", err)
}
return nil
}