service: implement a function for creating and storing a signature

This commit is contained in:
Leonard Lyubich 2020-05-04 19:33:18 +03:00
parent 0ffb1bd61d
commit f3e6caf7e7
3 changed files with 59 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package service
import (
"crypto/ecdsa"
"crypto/rand"
"errors"
"io"
@ -23,6 +24,21 @@ type testSignedDataReader struct {
d []byte
}
type testKeySigAccum struct {
d []byte
f func([]byte, *ecdsa.PublicKey)
}
func (s testKeySigAccum) SignedData() ([]byte, error) {
return s.d, nil
}
func (s testKeySigAccum) AddSignKey(sig []byte, key *ecdsa.PublicKey) {
if s.f != nil {
s.f(sig, key)
}
}
func testData(t *testing.T, sz int) []byte {
d := make([]byte, sz)
_, err := rand.Read(d)
@ -110,3 +126,22 @@ func TestDataSignature(t *testing.T) {
require.NoError(t, crypto.Verify(&sk.PublicKey, src.d, sig))
})
}
func TestAddSignatureWithKey(t *testing.T) {
// create test data
data := testData(t, 10)
// create test private key
sk := test.DecodeKey(0)
// create test signature accumulator
var s SignatureKeyAccumulator = &testKeySigAccum{
d: data,
f: func(sig []byte, key *ecdsa.PublicKey) {
require.Equal(t, &sk.PublicKey, key)
require.NoError(t, crypto.Verify(key, data, sig))
},
}
require.NoError(t, AddSignatureWithKey(s, sk))
}