Skip to content

Commit

Permalink
Added sharness test of cli
Browse files Browse the repository at this point in the history
Small fixes

Changed environment variable

TRAVIS_BUILD_DIR -> PWD

License: MIT
Signed-off-by: Trond Bråthen <tabrath@gmail.com>

Added sharness tests

License: MIT
Signed-off-by: Trond Bråthen <tabrath@gmail.com>

removed comments

Added supoorts for keccak-224/384/512

gx release 1.0.3
  • Loading branch information
tabrath committed Mar 9, 2017
1 parent d2cd43e commit 9294aaf
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.2: QmbZ6Cee2uHjG7hf19qLHppgKDRtaG4CVtMzdmK9VCVqLu
1.0.3: QmPtbDoz8i593CoReBAzgXdx5PTAN66xAofpendZb922kp
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ before_install:
script:
- go vet
- go test -race -coverprofile=coverage.txt -covermode=atomic
- make sharness

after_success:
- bash <(curl -s https://codecov.io/bash)
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ deps: gx covertools
gx --verbose install --global
gx-go rewrite

sharness: deps
git clone https://github.com/multiformats/multihash.git $(PWD)/sharness
cd $(PWD)/multihash && go build -v . && ls && chmod +x ./multihash
export MULTIHASH_BIN="$(PWD)/multihash/multihash" && export TEST_EXPENSE=1 && make -j1 -C $(PWD)/sharness/tests/sharness

publish:
gx-go rewrite --undo

14 changes: 14 additions & 0 deletions multihash.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
BLAKE2S_MAX = 0xb260

DBL_SHA2_256 = 0x56

MURMUR3 = 0x22
)

func init() {
Expand Down Expand Up @@ -77,7 +79,11 @@ var Names = map[string]uint64{
"sha2-512": SHA2_512,
"sha3": SHA3,
"dbl-sha2-256": DBL_SHA2_256,
"murmur3": MURMUR3,
"keccak-224": KECCAK_224,
"keccak-256": KECCAK_256,
"keccak-384": KECCAK_384,
"keccak-512": KECCAK_512,
}

// Codes maps a hash code to it's name
Expand All @@ -87,7 +93,11 @@ var Codes = map[uint64]string{
SHA2_512: "sha2-512",
SHA3: "sha3",
DBL_SHA2_256: "dbl-sha2-256",
MURMUR3: "murmur3",
KECCAK_224: "keccak-224",
KECCAK_256: "keccak-256",
KECCAK_384: "keccak-384",
KECCAK_512: "keccak-512",
}

// DefaultLengths maps a hash code to it's default length
Expand All @@ -97,7 +107,11 @@ var DefaultLengths = map[uint64]int{
SHA2_512: 64,
SHA3: 64,
DBL_SHA2_256: 32,
KECCAK_224: 28,
KECCAK_256: 32,
MURMUR3: 4,
KECCAK_384: 48,
KECCAK_512: 64,
}

func uvarint(buf []byte) (uint64, []byte, error) {
Expand Down
5 changes: 5 additions & 0 deletions multihash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ var tCodes = map[uint64]string{
0x13: "sha2-512",
0x14: "sha3",
0x56: "dbl-sha2-256",
0x22: "murmur3",
0x1A: "keccak-224",
0x1B: "keccak-256",
0x1C: "keccak-384",
0x1D: "keccak-512",
}

type TestCase struct {
Expand All @@ -32,6 +36,7 @@ var testCases = []TestCase{
TestCase{"2c26b46b", 0x12, "sha2-256"},
TestCase{"2c26b46b68ffc68ff99b453c1d30413413", 0xb240, "blake2b-512"},
TestCase{"f00ba4", 0x1b, "keccak-256"},
TestCase{"243ddb9e", 0x22, "murmur3"},
}

func (tc TestCase) Multihash() (Multihash, error) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"language": "go",
"license": "MIT",
"name": "go-multihash",
"version": "1.0.2"
"releaseCmd": "git commit -a -m \"gx release $VERSION\"",
"version": "1.0.3"
}

41 changes: 39 additions & 2 deletions sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"errors"
"fmt"

keccak "github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/spaolacci/murmur3"
blake2b "golang.org/x/crypto/blake2b"
blake2s "golang.org/x/crypto/blake2s"
sha3 "golang.org/x/crypto/sha3"
keccak "leb.io/hashland/keccakpg"
)

var ErrSumNotSupported = errors.New("Function not implemented. Complain to lib maintainer.")
Expand Down Expand Up @@ -64,12 +65,20 @@ func Sum(data []byte, code uint64, length int) (Multihash, error) {
d = sumSHA256(data)
case SHA2_512:
d = sumSHA512(data)
case KECCAK_224:
d = sumKeccak224(data)
case KECCAK_256:
d = sumKeccak256(data)
case KECCAK_384:
d = sumKeccak384(data)
case KECCAK_512:
d = sumKeccak512(data)
case SHA3:
d, err = sumSHA3(data)
case DBL_SHA2_256:
d = sumSHA256(sumSHA256(data))
case MURMUR3:
d, err = sumMURMUR3(data)
default:
return m, ErrSumNotSupported
}
Expand Down Expand Up @@ -102,8 +111,26 @@ func sumSHA512(data []byte) []byte {
return a[0:64]
}

func sumKeccak224(data []byte) []byte {
h := keccak.New224()
h.Write(data)
return h.Sum(nil)
}

func sumKeccak256(data []byte) []byte {
h := keccak.NewKeccak256()
h := keccak.New256()
h.Write(data)
return h.Sum(nil)
}

func sumKeccak384(data []byte) []byte {
h := keccak.New384()
h.Write(data)
return h.Sum(nil)
}

func sumKeccak512(data []byte) []byte {
h := keccak.New512()
h.Write(data)
return h.Sum(nil)
}
Expand All @@ -115,3 +142,13 @@ func sumSHA3(data []byte) ([]byte, error) {
}
return h.Sum(nil), nil
}

func sumMURMUR3(data []byte) ([]byte, error) {
number := murmur3.Sum32(data)
bytes := make([]byte, 4)
for i := range bytes {
bytes[i] = byte(number & 0xff)
number >>= 8
}
return bytes, nil
}
4 changes: 4 additions & 0 deletions sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ var sumTestCases = []SumTestCase{
SumTestCase{BLAKE2B_MAX - 32, 32, "foo", "a0e40220b8fe9f7f6255a6fa08f668ab632a8d081ad87983c77cd274e48ce450f0b349fd"},
SumTestCase{BLAKE2B_MAX - 16, 32, "foo", "b0e40220e629ee880953d32c8877e479e3b4cb0a4c9d5805e2b34c675b5a5863c4ad7d64"},
SumTestCase{BLAKE2S_MAX, 32, "foo", "e0e4022008d6cad88075de8f192db097573d0e829411cd91eb6ec65e8fc16c017edfdb74"},
SumTestCase{MURMUR3, 4, "beep boop", "2204243ddb9e"},
SumTestCase{KECCAK_224, -1, "beep boop", "1a1c2bd72cde2f75e523512999eb7639f17b699efe29bec342f5a0270896"},
SumTestCase{KECCAK_256, 32, "foo", "1b2041b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d"},
SumTestCase{KECCAK_384, -1, "beep boop", "1c300e2fcca40e861fc425a2503a65f4a4befab7be7f193e57654ca3713e85262b035e54d5ade93f9632b810ab88b04f7d84"},
SumTestCase{KECCAK_512, -1, "beep boop", "1d40e161c54798f78eba3404ac5e7e12d27555b7b810e7fd0db3f25ffa0c785c438331b0fbb6156215f69edf403c642e5280f4521da9bd767296ec81f05100852e78"},
}

func TestSum(t *testing.T) {
Expand Down

0 comments on commit 9294aaf

Please sign in to comment.