Skip to content

Commit 0b7b9ea

Browse files
patapenka-alexeybigbes
authored andcommitted
api: implement namer
Closes TNTP-4190
1 parent c36b4a8 commit 0b7b9ea

File tree

14 files changed

+839
-39
lines changed

14 files changed

+839
-39
lines changed

crypto/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Package crypto implements crypto interfaces.
1+
// Package verification implements verification interfaces.
22
package crypto
33

44
// Signer implements high-level API for package signing.

crypto/rsa.go renamed to crypto/rsapss.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ func NewRSAPSS(privKey rsa.PrivateKey, pubKey rsa.PublicKey) RSAPSS {
2929
}
3030

3131
// Name implements SignerVerifier interface.
32-
func (r *RSAPSS) Name() string {
32+
func (r RSAPSS) Name() string {
3333
return "RSASSA-PSS"
3434
}
3535

3636
// Sign generates SHA-256 digest and signs it using RSASSA-PSS.
37-
func (r *RSAPSS) Sign(data []byte) ([]byte, error) {
37+
func (r RSAPSS) Sign(data []byte) ([]byte, error) {
3838
digest, err := r.hasher.Hash(data)
3939
if err != nil {
4040
return []byte{}, fmt.Errorf("failed to get hash: %w", err)
@@ -52,7 +52,7 @@ func (r *RSAPSS) Sign(data []byte) ([]byte, error) {
5252
}
5353

5454
// Verify compares data with signature.
55-
func (r *RSAPSS) Verify(data []byte, signature []byte) error {
55+
func (r RSAPSS) Verify(data []byte, signature []byte) error {
5656
digest, err := r.hasher.Hash(data)
5757
if err != nil {
5858
return fmt.Errorf("failed to get hash: %w", err)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/stretchr/testify/require"
9+
910
"github.com/tarantool/go-storage/crypto"
1011
)
1112

hasher/hasher_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ func TestSHA256Hasher(t *testing.T) {
6161
t.Parallel()
6262

6363
tests := []struct {
64-
name string
65-
in []byte
66-
out string
64+
name string
65+
in []byte
66+
expected string
6767
}{
6868
{"empty", []byte(""), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
6969
{"abc", []byte("abc"), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"},
@@ -77,7 +77,7 @@ func TestSHA256Hasher(t *testing.T) {
7777

7878
result, _ := h.Hash(test.in)
7979

80-
assert.Equal(t, test.out, hex.EncodeToString(result))
80+
assert.Equal(t, test.expected, hex.EncodeToString(result))
8181
})
8282
}
8383
}

kv/kv.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ type KeyValue struct {
99
Key []byte
1010
// Value is the serialized representation of the value.
1111
Value []byte
12-
1312
// ModRevision is the revision number of the last modification to this key.
1413
ModRevision int64
1514
}

marshaller/marshaller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ var ErrUnmarshall = errors.New("failed to unmarshal")
1717
// implements one time for all objects.
1818
// Required for `integrity.Storage` to set marshalling format for any type object
1919
// and as recommendation for developers of `Storage` wrappers.
20-
type DefaultMarshaller interface {
20+
type DefaultMarshaller interface { //nolint:iface
2121
Marshal(data any) ([]byte, error)
2222
Unmarshal(data []byte, out any) error
2323
}
2424

2525
// Marshallable - custom object serialization, implements for each object.
2626
// Required for `integrity.Storage` type to set marshalling format to specific object
2727
// and as recommendation for developers of `Storage` wrappers.
28-
type Marshallable interface {
29-
Marshal() ([]byte, error)
28+
type Marshallable interface { //nolint:iface
29+
Marshal(data any) ([]byte, error)
3030
Unmarshal(data []byte, out any) error
3131
}
3232

3333
// YAMLMarshaller struct represent realization.
3434
type YAMLMarshaller struct{}
3535

3636
// NewYamlMarshaller creates new NewYamlMarshaller object.
37-
func NewYamlMarshaller() YAMLMarshaller {
37+
func NewYamlMarshaller() Marshallable {
3838
return YAMLMarshaller{}
3939
}
4040

namer/error.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package namer
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// InvalidKeyError represents an error for invalid key format.
8+
type InvalidKeyError struct {
9+
Key string
10+
Problem string
11+
}
12+
13+
func (e InvalidKeyError) Error() string {
14+
return fmt.Sprintf("invalid key '%s': %s", e.Key, e.Problem)
15+
}
16+
17+
func errInvalidKey(key string, problem string) error {
18+
return InvalidKeyError{
19+
Key: key,
20+
Problem: problem,
21+
}
22+
}
23+
24+
// InvalidNameError represents an error for invalid name format.
25+
type InvalidNameError struct {
26+
Name string
27+
Problem string
28+
}
29+
30+
func (e InvalidNameError) Error() string {
31+
return fmt.Sprintf("invalid name '%s': %s", e.Name, e.Problem)
32+
}
33+
34+
func errInvalidName(name string, problem string) error {
35+
return InvalidNameError{
36+
Name: name,
37+
Problem: problem,
38+
}
39+
}

namer/error_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package namer_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/tarantool/go-storage/namer"
9+
)
10+
11+
func TestInvalidNameError_Error(t *testing.T) {
12+
t.Parallel()
13+
14+
err := namer.InvalidNameError{Name: "name", Problem: "problem"}
15+
assert.Equal(t, "invalid name 'name': problem", err.Error())
16+
}
17+
18+
func TestInvalidKeyError_Error(t *testing.T) {
19+
t.Parallel()
20+
21+
err := namer.InvalidKeyError{Key: "name", Problem: "problem"}
22+
assert.Equal(t, "invalid key 'name': problem", err.Error())
23+
}

namer/key.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package namer
2+
3+
// KeyType represents key types.
4+
type KeyType int
5+
6+
const (
7+
// KeyTypeValue represents data type.
8+
KeyTypeValue KeyType = iota + 1
9+
// KeyTypeHash represents hash of the data type.
10+
KeyTypeHash
11+
// KeyTypeSignature represents signature of the data type.
12+
KeyTypeSignature
13+
)
14+
15+
// Key defines the minimal interface required by keys.
16+
type Key interface {
17+
Name() string // Get object name.
18+
Type() KeyType // Get key type.
19+
Property() string // Get metadata (e.g., algorithm version).
20+
Build() string // Reconstruct raw key string.
21+
}
22+
23+
// DefaultKey implements default realization.
24+
type DefaultKey struct {
25+
name string // Object identifier.
26+
keyType KeyType // Type of object (hash/signature/value).
27+
property string // Additional metadata (version/algorithm).
28+
raw string // Raw key string.
29+
}
30+
31+
// NewDefaultKey returns new Key object.
32+
func NewDefaultKey(name string, keytype KeyType, property string, raw string) DefaultKey {
33+
return DefaultKey{
34+
name: name,
35+
keyType: keytype,
36+
property: property,
37+
raw: raw,
38+
}
39+
}
40+
41+
// Name returns name of the key.
42+
func (k DefaultKey) Name() string {
43+
return k.name
44+
}
45+
46+
// Type returns type of the key.
47+
func (k DefaultKey) Type() KeyType {
48+
return k.keyType
49+
}
50+
51+
// Property returns property of the key.
52+
func (k DefaultKey) Property() string {
53+
return k.property
54+
}
55+
56+
// Build reconstructs key string.
57+
func (k DefaultKey) Build() string {
58+
return k.raw
59+
}

namer/key_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package namer_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/tarantool/go-storage/namer"
10+
)
11+
12+
const (
13+
defaultName = "all"
14+
defaultType = namer.KeyTypeHash
15+
defaultProperty = "sha256"
16+
defaultRaw = "/config/hash/sha256/all"
17+
)
18+
19+
func TestNewDefaultKey(t *testing.T) {
20+
a := namer.NewDefaultKey(defaultName, defaultType, defaultProperty, defaultRaw)
21+
require.NotEmpty(t, a)
22+
}
23+
24+
func TestDefaultKey_Build(t *testing.T) {
25+
a := namer.NewDefaultKey(defaultName, defaultType, defaultProperty, defaultRaw)
26+
require.Equal(t, defaultRaw, a.Build())
27+
}
28+
29+
func TestDefaultKey_Name(t *testing.T) {
30+
a := namer.NewDefaultKey(defaultName, defaultType, defaultProperty, defaultRaw)
31+
assert.Equal(t, defaultName, a.Name())
32+
}
33+
34+
func TestDefaultKey_Type(t *testing.T) {
35+
a := namer.NewDefaultKey(defaultName, defaultType, defaultProperty, defaultRaw)
36+
assert.Equal(t, defaultType, a.Type())
37+
}
38+
39+
func TestDefaultKey_Property(t *testing.T) {
40+
a := namer.NewDefaultKey(defaultName, defaultType, defaultProperty, defaultRaw)
41+
assert.Equal(t, defaultProperty, a.Property())
42+
}

0 commit comments

Comments
 (0)