object: implement SignedData() and AddSignKey() methods on IntegrityHeader

This commit is contained in:
Leonard Lyubich 2020-05-18 13:44:32 +03:00
parent ab796b81d2
commit 96a6bb4842
4 changed files with 64 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package object package object
import ( import (
"crypto/ecdsa"
"encoding/binary" "encoding/binary"
"io" "io"
@ -259,3 +260,13 @@ func addressSize(addr Address) int {
func addressBytes(addr Address) []byte { func addressBytes(addr Address) []byte {
return append(addr.CID.Bytes(), addr.ObjectID.Bytes()...) return append(addr.CID.Bytes(), addr.ObjectID.Bytes()...)
} }
// SignedData returns the result of the ChecksumSignature field getter.
func (m IntegrityHeader) SignedData() ([]byte, error) {
return m.GetHeadersChecksum(), nil
}
// AddSignKey calls the ChecksumSignature field setter with signature argument.
func (m *IntegrityHeader) AddSignKey(sign []byte, _ *ecdsa.PublicKey) {
m.SetSignature(sign)
}

View file

@ -219,3 +219,21 @@ func testData(t *testing.T, sz int) []byte {
return data return data
} }
func TestIntegrityHeaderSignMethods(t *testing.T) {
// create new IntegrityHeader
s := new(IntegrityHeader)
// set test headers checksum
s.SetHeadersChecksum([]byte{1, 2, 3})
data, err := s.SignedData()
require.NoError(t, err)
require.Equal(t, data, s.GetHeadersChecksum())
// add signature
sig := []byte{4, 5, 6}
s.AddSignKey(sig, nil)
require.Equal(t, sig, s.GetSignature())
}

View file

@ -391,3 +391,18 @@ func Stringify(dst io.Writer, obj *Object) error {
func (m *HeadRequest) SetFullHeaders(v bool) { func (m *HeadRequest) SetFullHeaders(v bool) {
m.FullHeaders = v m.FullHeaders = v
} }
// GetSignature is a ChecksumSignature field getter.
func (m IntegrityHeader) GetSignature() []byte {
return m.ChecksumSignature
}
// SetSignature is a ChecksumSignature field setter.
func (m *IntegrityHeader) SetSignature(v []byte) {
m.ChecksumSignature = v
}
// SetHeadersChecksum is a HeadersChecksum field setter.
func (m *IntegrityHeader) SetHeadersChecksum(v []byte) {
m.HeadersChecksum = v
}

View file

@ -199,3 +199,23 @@ func TestObject_Copy(t *testing.T) {
require.Equal(t, token, h.GetValue().(*Header_Token).Token) require.Equal(t, token, h.GetValue().(*Header_Token).Token)
}) })
} }
func TestIntegrityHeaderGettersSetters(t *testing.T) {
t.Run("headers checksum", func(t *testing.T) {
data := []byte{1, 2, 3}
v := new(IntegrityHeader)
v.SetHeadersChecksum(data)
require.Equal(t, data, v.GetHeadersChecksum())
})
t.Run("headers checksum", func(t *testing.T) {
data := []byte{1, 2, 3}
v := new(IntegrityHeader)
v.SetSignature(data)
require.Equal(t, data, v.GetSignature())
})
}