diff options
author | Rasmus Dahlberg <rasmus.dahlberg@kau.se> | 2021-02-23 11:07:00 +0100 |
---|---|---|
committer | Rasmus Dahlberg <rasmus.dahlberg@kau.se> | 2021-02-23 11:07:00 +0100 |
commit | 666e41811bdd46e24ced8ddd3448243ecb2f45c3 (patch) | |
tree | 57780b15196277896a5f143921caac8b70295850 /types | |
parent | 5358bb2f03202245b64d512c54170982da6ddc27 (diff) |
added namespace fingerprint
Diffstat (limited to 'types')
-rw-r--r-- | types/namespace.go | 12 | ||||
-rw-r--r-- | types/namespace_test.go | 38 |
2 files changed, 50 insertions, 0 deletions
diff --git a/types/namespace.go b/types/namespace.go index 8a2ad17..f221960 100644 --- a/types/namespace.go +++ b/types/namespace.go @@ -14,6 +14,8 @@ type NamespaceFormat tls.Enum const ( NamespaceFormatReserved NamespaceFormat = 0 NamespaceFormatEd25519V1 NamespaceFormat = 1 + + NamespaceFingerprintSize = 32 ) // Namespace references a versioned namespace based on a given format specifier @@ -50,6 +52,16 @@ func (n Namespace) String() string { } } +// Fingerprint returns a fixed-size namespace fingerprint that is unique. +func (n *Namespace) Fingerprint() (*[NamespaceFingerprintSize]byte, error) { + switch n.Format { + case NamespaceFormatEd25519V1: + return &n.Ed25519V1.Namespace, nil + default: + return nil, fmt.Errorf("unsupported NamespaceFormat: %v", n.Format) + } +} + // NewNamespaceEd25519V1 returns an new Ed25519V1 namespace based on a // verification key. func NewNamespaceEd25519V1(vk []byte) (*Namespace, error) { diff --git a/types/namespace_test.go b/types/namespace_test.go index da18b13..e7e89ad 100644 --- a/types/namespace_test.go +++ b/types/namespace_test.go @@ -97,6 +97,44 @@ func TestNamespaceString(t *testing.T) { } } +func TestFingerprint(t *testing.T) { + for _, table := range []struct { + description string + namespace *Namespace + wantErr bool + wantFpr [NamespaceFingerprintSize]byte + }{ + { + description: "invalid: no fingerprint for type", + namespace: &Namespace{ + Format: NamespaceFormatReserved, + }, + wantErr: true, + }, + { + description: "valid: ed25519_v1", + namespace: mustInitNamespaceEd25519V1(t, 0xaf), + wantFpr: func() (ret [NamespaceFingerprintSize]byte) { + for i, _ := range ret { + ret[i] = 0xaf + } + return + }(), + }, + } { + fpr, err := table.namespace.Fingerprint() + if got, want := err != nil, table.wantErr; got != want { + t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err) + } + if err != nil { + continue + } + if got, want := *fpr, table.wantFpr; !bytes.Equal(got[:], want[:]) { + t.Errorf("got fpr %v but wanted %v in test %q", got, want, table.description) + } + } +} + func TestVerify(t *testing.T) { var tests []testCaseNamespace tests = append(tests, test_cases_verify(t)...) |