Skip to content

Commit

Permalink
Add AlgIDToString method to HashEntry
Browse files Browse the repository at this point in the history
This change allows us to get the string equivalent of the
algorithm ID.

Signed-off-by: SabreenKaur <[email protected]>
  • Loading branch information
SabreenKaur committed Apr 12, 2023
1 parent d074e2d commit 865f089
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions hashentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
33 changes: 33 additions & 0 deletions hashentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 865f089

Please sign in to comment.