Skip to content

Commit 865f089

Browse files
committed
Add AlgIDToString method to HashEntry
This change allows us to get the string equivalent of the algorithm ID. Signed-off-by: SabreenKaur <[email protected]>
1 parent d074e2d commit 865f089

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

hashentry.go

+10
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ func (h *HashEntry) codify(v string) error {
176176
return nil
177177
}
178178

179+
// AlgIDToString provides a conversion from the algorithm ID to the string
180+
// representation of the algorithm
181+
func (h *HashEntry) AlgIDToString() string {
182+
sAlg, ok := algToString[h.HashAlgID]
183+
if !ok {
184+
return fmt.Sprintf("alg-id(%d)", h.HashAlgID)
185+
}
186+
return sAlg
187+
}
188+
179189
// MarshalJSON provides the custom JSON marshaler for the HashEntry type
180190
func (h HashEntry) MarshalJSON() ([]byte, error) {
181191
s, err := h.stringify()

hashentry_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,39 @@ func TestHashEntry_MarshalJSON(t *testing.T) {
172172
}
173173
}
174174

175+
func TestHashEntry_AlgIDToString(t *testing.T) {
176+
tests := []struct {
177+
name string
178+
testVector HashEntry
179+
expected string
180+
}{
181+
{
182+
name: "good stuff",
183+
testVector: HashEntry{
184+
HashAlgID: 1,
185+
HashValue: []byte{0xde, 0xad, 0xbe, 0xef},
186+
},
187+
expected: "sha-256",
188+
},
189+
{
190+
name: "unknown hash algo",
191+
testVector: HashEntry{
192+
HashAlgID: 1000,
193+
HashValue: []byte{0xde, 0xad, 0xbe, 0xef},
194+
},
195+
expected: "alg-id(1000)",
196+
},
197+
}
198+
199+
for _, test := range tests {
200+
t.Run(test.name, func(t *testing.T) {
201+
u := test.testVector
202+
actual := u.AlgIDToString()
203+
assert.Equal(t, test.expected, actual)
204+
})
205+
}
206+
}
207+
175208
func TestHashEntry_Set_OK(t *testing.T) {
176209
tvs := []struct {
177210
alg uint64

0 commit comments

Comments
 (0)