Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hash list and use it in multihash command #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions multihash.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"math"
"sort"

b58 "github.com/jbenet/go-base58"
)
Expand Down Expand Up @@ -81,6 +82,13 @@ func init() {
Codes[c] = name
DefaultLengths[c] = int(n)
}

// Initialize NamesList
NamesList = make([]string, 0, len(Names))
for k, _ := range Names {
NamesList = append(NamesList, k)
}
sort.Strings(NamesList)
}

// Names maps the name of a hash to the code
Expand All @@ -104,6 +112,8 @@ var Names = map[string]uint64{
"shake-256": SHAKE_256,
}

var NamesList []string

// Codes maps a hash code to it's name
var Codes = map[uint64]string{
ID: "id",
Expand Down
14 changes: 14 additions & 0 deletions multihash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ var testCases = []TestCase{
TestCase{"4bca2b137edc580fe50a88983ef860ebaca36c857b1f492839d6d7392452a63c82cbebc68e3b70a2a1480b4bb5d437a7cba6ecf9d89f9ff3ccd14cd6146ea7e7", 0x14, "sha3-512"},
}

func TestNamesList(t *testing.T) {
if len(NamesList) != len(Names) {
t.Fatal("lengths are different, NamesList: %d; Names: %d",
len(NamesList), len(Names))
}

for _, v := range NamesList {
_, ok := Names[v]
if !ok {
t.Fatal("entry: %s not contained in Names", v)
}
}
}

func (tc TestCase) Multihash() (Multihash, error) {
ob, err := hex.DecodeString(tc.hex)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion opts/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var FlagValues = struct {
Algorithms []string
}{
Encodings: []string{"raw", "hex", "base58", "base64"},
Algorithms: []string{"sha1", "sha2-256", "sha2-512", "sha3"},
Algorithms: mh.NamesList,
}

// SetupFlags adds multihash related options to given flagset.
Expand Down