From 921cb9e55f64094763b1b1d70ccd93bb17f99f57 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 10 Jul 2017 14:18:53 +0200 Subject: [PATCH] add hash list and use it in multihash command --- multihash.go | 10 ++++++++++ multihash_test.go | 14 ++++++++++++++ opts/opts.go | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/multihash.go b/multihash.go index 39fa9e8..e21d35a 100644 --- a/multihash.go +++ b/multihash.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "math" + "sort" b58 "github.com/jbenet/go-base58" ) @@ -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 @@ -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", diff --git a/multihash_test.go b/multihash_test.go index e100293..1c27164 100644 --- a/multihash_test.go +++ b/multihash_test.go @@ -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 { diff --git a/opts/opts.go b/opts/opts.go index c905c9b..8b6861a 100644 --- a/opts/opts.go +++ b/opts/opts.go @@ -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.