From 865f089e82d596fea941b75a0ab2ee1591694bf3 Mon Sep 17 00:00:00 2001 From: SabreenKaur Date: Wed, 12 Apr 2023 10:17:02 +0100 Subject: [PATCH] Add AlgIDToString method to HashEntry This change allows us to get the string equivalent of the algorithm ID. Signed-off-by: SabreenKaur --- hashentry.go | 10 ++++++++++ hashentry_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/hashentry.go b/hashentry.go index 72860c3..a3a0a8c 100644 --- a/hashentry.go +++ b/hashentry.go @@ -176,6 +176,16 @@ func (h *HashEntry) codify(v string) error { return nil } +// AlgIDToString provides a conversion from the algorithm ID to the string +// representation of the algorithm +func (h *HashEntry) AlgIDToString() string { + sAlg, ok := algToString[h.HashAlgID] + if !ok { + return fmt.Sprintf("alg-id(%d)", h.HashAlgID) + } + return sAlg +} + // MarshalJSON provides the custom JSON marshaler for the HashEntry type func (h HashEntry) MarshalJSON() ([]byte, error) { s, err := h.stringify() diff --git a/hashentry_test.go b/hashentry_test.go index eefb783..7824298 100644 --- a/hashentry_test.go +++ b/hashentry_test.go @@ -172,6 +172,39 @@ func TestHashEntry_MarshalJSON(t *testing.T) { } } +func TestHashEntry_AlgIDToString(t *testing.T) { + tests := []struct { + name string + testVector HashEntry + expected string + }{ + { + name: "good stuff", + testVector: HashEntry{ + HashAlgID: 1, + HashValue: []byte{0xde, 0xad, 0xbe, 0xef}, + }, + expected: "sha-256", + }, + { + name: "unknown hash algo", + testVector: HashEntry{ + HashAlgID: 1000, + HashValue: []byte{0xde, 0xad, 0xbe, 0xef}, + }, + expected: "alg-id(1000)", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + u := test.testVector + actual := u.AlgIDToString() + assert.Equal(t, test.expected, actual) + }) + } +} + func TestHashEntry_Set_OK(t *testing.T) { tvs := []struct { alg uint64