Commit d4b42f8e authored by Jeromy's avatar Jeromy
Browse files

fixes for sha3

parent 8f79df77
......@@ -5,7 +5,7 @@
"gxDependencies": [
{
"name": "go-multiaddr-net",
"hash": "QmanZCL6SXRfafiUEMCBLq2QR171uQSdXQ8YAdHXLd8Cwr",
"hash": "QmU5s159q8cZuM1f9Vqti4LHu6y8zyVc5dxv2py81sdp6Q",
"version": "1.0.0"
}
],
......
......@@ -9,7 +9,7 @@ import (
"strconv"
"strings"
mh "QmdeauTdyf38KDQB4Cc4CurPWRRb5pej27NCXPA7kbPTJy/go-multihash"
mh "QmdsKjp5fcCT8PZ8JBMcdFsCbbmKwSLCU5xXbsnwb5DMxy/go-multihash"
)
func stringToBytes(s string) ([]byte, error) {
......
......@@ -5,7 +5,7 @@
"gxDependencies": [
{
"name": "go-multihash",
"hash": "QmdeauTdyf38KDQB4Cc4CurPWRRb5pej27NCXPA7kbPTJy",
"hash": "QmdsKjp5fcCT8PZ8JBMcdFsCbbmKwSLCU5xXbsnwb5DMxy",
"version": "1.0.0"
}
],
......
language: go
go:
- 1.3
- release
- tip
script:
- make test
env: TEST_VERBOSE=1
The MIT License (MIT)
Copyright (c) 2014 Juan Batiz-Benet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
test: go_test other_tests
other_tests:
cd test && make test
go_test: go_deps
go test -race -cpu=5 -v ./...
go_deps:
go get golang.org/x/crypto/sha3
go get github.com/jbenet/go-base58
# go-multihash
![travis](https://travis-ci.org/jbenet/go-multihash.svg)
[multihash](//github.com/jbenet/multihash) implementation in Go.
## Example
```go
package main
import (
"encoding/hex"
"fmt"
"github.com/jbenet/go-multihash"
)
func main() {
// ignores errors for simplicity.
// don't do that at home.
buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
mhbuf, _ := multihash.EncodeName(buf, "sha1");
mhhex := hex.EncodeToString(mhbuf)
fmt.Printf("hex: %v\n", mhhex);
o, _ := multihash.Decode(mhbuf);
mhhex = hex.EncodeToString(o.Digest);
fmt.Printf("obj: %v 0x%x %d %s\n", o.Name, o.Code, o.Length, mhhex);
}
```
Run [test/foo.go](test/foo.go)
```
> cd test/
> go build
> ./test
hex: 11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
obj: sha1 0x11 20 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
```
## License
MIT
package multihash
import (
"fmt"
"io"
)
// Reader is an io.Reader wrapper that exposes a function
// to read a whole multihash, parse it, and return it.
type Reader interface {
io.Reader
ReadMultihash() (Multihash, error)
}
// Writer is an io.Writer wrapper that exposes a function
// to write a whole multihash.
type Writer interface {
io.Writer
WriteMultihash(Multihash) error
}
// NewReader wraps an io.Reader with a multihash.Reader
func NewReader(r io.Reader) Reader {
return &mhReader{r}
}
// NewWriter wraps an io.Writer with a multihash.Writer
func NewWriter(w io.Writer) Writer {
return &mhWriter{w}
}
type mhReader struct {
r io.Reader
}
func (r *mhReader) Read(buf []byte) (n int, err error) {
return r.r.Read(buf)
}
func (r *mhReader) ReadMultihash() (Multihash, error) {
mhhdr := make([]byte, 2)
if _, err := io.ReadFull(r.r, mhhdr); err != nil {
return nil, err
}
// first byte is the algo, the second is the length.
// (varints someday...)
length := uint(mhhdr[1])
if length > 127 {
return nil, fmt.Errorf("varints not yet supported (length is %d)", length)
}
buf := make([]byte, length+2)
buf[0] = mhhdr[0]
buf[1] = mhhdr[1]
if _, err := io.ReadFull(r.r, buf[2:]); err != nil {
return nil, err
}
return Cast(buf)
}
type mhWriter struct {
w io.Writer
}
func (w *mhWriter) Write(buf []byte) (n int, err error) {
return w.w.Write(buf)
}
func (w *mhWriter) WriteMultihash(m Multihash) error {
_, err := w.w.Write([]byte(m))
return err
}
package multihash
import (
"bytes"
"io"
"testing"
)
func TestReader(t *testing.T) {
var buf bytes.Buffer
for _, tc := range testCases {
m, err := tc.Multihash()
if err != nil {
t.Fatal(err)
}
buf.Write([]byte(m))
}
r := NewReader(&buf)
for _, tc := range testCases {
h, err := tc.Multihash()
if err != nil {
t.Fatal(err)
}
h2, err := r.ReadMultihash()
if err != nil {
t.Error(err)
continue
}
if !bytes.Equal(h, h2) {
t.Error("h and h2 should be equal")
}
}
}
func TestWriter(t *testing.T) {
var buf bytes.Buffer
w := NewWriter(&buf)
for _, tc := range testCases {
m, err := tc.Multihash()
if err != nil {
t.Error(err)
continue
}
if err := w.WriteMultihash(m); err != nil {
t.Error(err)
continue
}
buf2 := make([]byte, len(m))
if _, err := io.ReadFull(&buf, buf2); err != nil {
t.Error(err)
continue
}
if !bytes.Equal(m, buf2) {
t.Error("m and buf2 should be equal")
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment