Skip to content

Commit 9294aaf

Browse files
committed
Added sharness test of cli
Small fixes Changed environment variable TRAVIS_BUILD_DIR -> PWD License: MIT Signed-off-by: Trond Bråthen <[email protected]> Added sharness tests License: MIT Signed-off-by: Trond Bråthen <[email protected]> removed comments Added supoorts for keccak-224/384/512 gx release 1.0.3
1 parent d2cd43e commit 9294aaf

File tree

9 files changed

+72
-5
lines changed

9 files changed

+72
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode/

.gx/lastpubver

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.2: QmbZ6Cee2uHjG7hf19qLHppgKDRtaG4CVtMzdmK9VCVqLu
1+
1.0.3: QmPtbDoz8i593CoReBAzgXdx5PTAN66xAofpendZb922kp

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ before_install:
1414
script:
1515
- go vet
1616
- go test -race -coverprofile=coverage.txt -covermode=atomic
17+
- make sharness
1718

1819
after_success:
1920
- bash <(curl -s https://codecov.io/bash)

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ deps: gx covertools
1010
gx --verbose install --global
1111
gx-go rewrite
1212

13+
sharness: deps
14+
git clone https://github.com/multiformats/multihash.git $(PWD)/sharness
15+
cd $(PWD)/multihash && go build -v . && ls && chmod +x ./multihash
16+
export MULTIHASH_BIN="$(PWD)/multihash/multihash" && export TEST_EXPENSE=1 && make -j1 -C $(PWD)/sharness/tests/sharness
17+
1318
publish:
1419
gx-go rewrite --undo
15-

multihash.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const (
4848
BLAKE2S_MAX = 0xb260
4949

5050
DBL_SHA2_256 = 0x56
51+
52+
MURMUR3 = 0x22
5153
)
5254

5355
func init() {
@@ -77,7 +79,11 @@ var Names = map[string]uint64{
7779
"sha2-512": SHA2_512,
7880
"sha3": SHA3,
7981
"dbl-sha2-256": DBL_SHA2_256,
82+
"murmur3": MURMUR3,
83+
"keccak-224": KECCAK_224,
8084
"keccak-256": KECCAK_256,
85+
"keccak-384": KECCAK_384,
86+
"keccak-512": KECCAK_512,
8187
}
8288

8389
// Codes maps a hash code to it's name
@@ -87,7 +93,11 @@ var Codes = map[uint64]string{
8793
SHA2_512: "sha2-512",
8894
SHA3: "sha3",
8995
DBL_SHA2_256: "dbl-sha2-256",
96+
MURMUR3: "murmur3",
97+
KECCAK_224: "keccak-224",
9098
KECCAK_256: "keccak-256",
99+
KECCAK_384: "keccak-384",
100+
KECCAK_512: "keccak-512",
91101
}
92102

93103
// DefaultLengths maps a hash code to it's default length
@@ -97,7 +107,11 @@ var DefaultLengths = map[uint64]int{
97107
SHA2_512: 64,
98108
SHA3: 64,
99109
DBL_SHA2_256: 32,
110+
KECCAK_224: 28,
100111
KECCAK_256: 32,
112+
MURMUR3: 4,
113+
KECCAK_384: 48,
114+
KECCAK_512: 64,
101115
}
102116

103117
func uvarint(buf []byte) (uint64, []byte, error) {

multihash_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ var tCodes = map[uint64]string{
1616
0x13: "sha2-512",
1717
0x14: "sha3",
1818
0x56: "dbl-sha2-256",
19+
0x22: "murmur3",
20+
0x1A: "keccak-224",
1921
0x1B: "keccak-256",
22+
0x1C: "keccak-384",
23+
0x1D: "keccak-512",
2024
}
2125

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

3742
func (tc TestCase) Multihash() (Multihash, error) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"language": "go",
3131
"license": "MIT",
3232
"name": "go-multihash",
33-
"version": "1.0.2"
33+
"releaseCmd": "git commit -a -m \"gx release $VERSION\"",
34+
"version": "1.0.3"
3435
}
3536

sum.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"errors"
88
"fmt"
99

10-
keccak "github.com/ethereum/go-ethereum/crypto/sha3"
10+
"github.com/spaolacci/murmur3"
1111
blake2b "golang.org/x/crypto/blake2b"
1212
blake2s "golang.org/x/crypto/blake2s"
1313
sha3 "golang.org/x/crypto/sha3"
14+
keccak "leb.io/hashland/keccakpg"
1415
)
1516

1617
var ErrSumNotSupported = errors.New("Function not implemented. Complain to lib maintainer.")
@@ -64,12 +65,20 @@ func Sum(data []byte, code uint64, length int) (Multihash, error) {
6465
d = sumSHA256(data)
6566
case SHA2_512:
6667
d = sumSHA512(data)
68+
case KECCAK_224:
69+
d = sumKeccak224(data)
6770
case KECCAK_256:
6871
d = sumKeccak256(data)
72+
case KECCAK_384:
73+
d = sumKeccak384(data)
74+
case KECCAK_512:
75+
d = sumKeccak512(data)
6976
case SHA3:
7077
d, err = sumSHA3(data)
7178
case DBL_SHA2_256:
7279
d = sumSHA256(sumSHA256(data))
80+
case MURMUR3:
81+
d, err = sumMURMUR3(data)
7382
default:
7483
return m, ErrSumNotSupported
7584
}
@@ -102,8 +111,26 @@ func sumSHA512(data []byte) []byte {
102111
return a[0:64]
103112
}
104113

114+
func sumKeccak224(data []byte) []byte {
115+
h := keccak.New224()
116+
h.Write(data)
117+
return h.Sum(nil)
118+
}
119+
105120
func sumKeccak256(data []byte) []byte {
106-
h := keccak.NewKeccak256()
121+
h := keccak.New256()
122+
h.Write(data)
123+
return h.Sum(nil)
124+
}
125+
126+
func sumKeccak384(data []byte) []byte {
127+
h := keccak.New384()
128+
h.Write(data)
129+
return h.Sum(nil)
130+
}
131+
132+
func sumKeccak512(data []byte) []byte {
133+
h := keccak.New512()
107134
h.Write(data)
108135
return h.Sum(nil)
109136
}
@@ -115,3 +142,13 @@ func sumSHA3(data []byte) ([]byte, error) {
115142
}
116143
return h.Sum(nil), nil
117144
}
145+
146+
func sumMURMUR3(data []byte) ([]byte, error) {
147+
number := murmur3.Sum32(data)
148+
bytes := make([]byte, 4)
149+
for i := range bytes {
150+
bytes[i] = byte(number & 0xff)
151+
number >>= 8
152+
}
153+
return bytes, nil
154+
}

sum_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ var sumTestCases = []SumTestCase{
2727
SumTestCase{BLAKE2B_MAX - 32, 32, "foo", "a0e40220b8fe9f7f6255a6fa08f668ab632a8d081ad87983c77cd274e48ce450f0b349fd"},
2828
SumTestCase{BLAKE2B_MAX - 16, 32, "foo", "b0e40220e629ee880953d32c8877e479e3b4cb0a4c9d5805e2b34c675b5a5863c4ad7d64"},
2929
SumTestCase{BLAKE2S_MAX, 32, "foo", "e0e4022008d6cad88075de8f192db097573d0e829411cd91eb6ec65e8fc16c017edfdb74"},
30+
SumTestCase{MURMUR3, 4, "beep boop", "2204243ddb9e"},
31+
SumTestCase{KECCAK_224, -1, "beep boop", "1a1c2bd72cde2f75e523512999eb7639f17b699efe29bec342f5a0270896"},
3032
SumTestCase{KECCAK_256, 32, "foo", "1b2041b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d"},
33+
SumTestCase{KECCAK_384, -1, "beep boop", "1c300e2fcca40e861fc425a2503a65f4a4befab7be7f193e57654ca3713e85262b035e54d5ade93f9632b810ab88b04f7d84"},
34+
SumTestCase{KECCAK_512, -1, "beep boop", "1d40e161c54798f78eba3404ac5e7e12d27555b7b810e7fd0db3f25ffa0c785c438331b0fbb6156215f69edf403c642e5280f4521da9bd767296ec81f05100852e78"},
3135
}
3236

3337
func TestSum(t *testing.T) {

0 commit comments

Comments
 (0)