Commit f3ae0e8e authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

u.Hash - error

the u.Hash error can be safely ignored (panic) because multihash
only fails from the selection of hash function. If the fn + length
are valid, it won't error.

cc @whyrusleeping
parent af1ce6ee
...@@ -13,11 +13,7 @@ type Block struct { ...@@ -13,11 +13,7 @@ type Block struct {
// NewBlock creates a Block object from opaque data. It will hash the data. // NewBlock creates a Block object from opaque data. It will hash the data.
func NewBlock(data []byte) (*Block, error) { func NewBlock(data []byte) (*Block, error) {
h, err := u.Hash(data) return &Block{Data: data, Multihash: u.Hash(data)}, nil
if err != nil {
return nil, err
}
return &Block{Data: data, Multihash: h}, nil
} }
// Key returns the block's Multihash as a Key value. // Key returns the block's Multihash as a Key value.
......
...@@ -23,12 +23,7 @@ func TestBlocks(t *testing.T) { ...@@ -23,12 +23,7 @@ func TestBlocks(t *testing.T) {
return return
} }
h, err := u.Hash([]byte("beep boop")) h := u.Hash([]byte("beep boop"))
if err != nil {
t.Error("failed to hash data", err)
return
}
if !bytes.Equal(b.Multihash, h) { if !bytes.Equal(b.Multihash, h) {
t.Error("Block Multihash and data multihash not equal") t.Error("Block Multihash and data multihash not equal")
} }
......
...@@ -249,5 +249,5 @@ func KeyHash(k Key) ([]byte, error) { ...@@ -249,5 +249,5 @@ func KeyHash(k Key) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return u.Hash(kb) return u.Hash(kb), nil
} }
...@@ -292,25 +292,15 @@ func IDFromPubKey(pk ci.PubKey) (peer.ID, error) { ...@@ -292,25 +292,15 @@ func IDFromPubKey(pk ci.PubKey) (peer.ID, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
hash, err := u.Hash(b) hash := u.Hash(b)
if err != nil {
return nil, err
}
return peer.ID(hash), nil return peer.ID(hash), nil
} }
// Determines which algorithm to use. Note: f(a, b) = f(b, a) // Determines which algorithm to use. Note: f(a, b) = f(b, a)
func selectBest(myPrefs, theirPrefs string) (string, error) { func selectBest(myPrefs, theirPrefs string) (string, error) {
// Person with greatest hash gets first choice. // Person with greatest hash gets first choice.
myHash, err := u.Hash([]byte(myPrefs)) myHash := u.Hash([]byte(myPrefs))
if err != nil { theirHash := u.Hash([]byte(theirPrefs))
return "", err
}
theirHash, err := u.Hash([]byte(theirPrefs))
if err != nil {
return "", err
}
cmp := bytes.Compare(myHash, theirHash) cmp := bytes.Compare(myHash, theirHash)
var firstChoiceArr, secChoiceArr []string var firstChoiceArr, secChoiceArr []string
......
...@@ -130,7 +130,7 @@ func (n *Node) Multihash() (mh.Multihash, error) { ...@@ -130,7 +130,7 @@ func (n *Node) Multihash() (mh.Multihash, error) {
return nil, err return nil, err
} }
return u.Hash(b) return u.Hash(b), nil
} }
// Key returns the Multihash as a key, for maps. // Key returns the Multihash as a key, for maps.
......
...@@ -42,16 +42,9 @@ func (p *ipnsPublisher) Publish(k ci.PrivKey, value string) error { ...@@ -42,16 +42,9 @@ func (p *ipnsPublisher) Publish(k ci.PrivKey, value string) error {
return nil return nil
} }
nameb, err := u.Hash(pkbytes) nameb := u.Hash(pkbytes)
if err != nil {
return nil
}
namekey := u.Key(nameb).Pretty() namekey := u.Key(nameb).Pretty()
ipnskey := u.Hash([]byte("/ipns/" + namekey))
ipnskey, err := u.Hash([]byte("/ipns/" + namekey))
if err != nil {
return err
}
// Store associated public key // Store associated public key
timectx, _ := context.WithDeadline(ctx, time.Now().Add(time.Second*4)) timectx, _ := context.WithDeadline(ctx, time.Now().Add(time.Second*4))
......
...@@ -48,11 +48,7 @@ func TestRoutingResolve(t *testing.T) { ...@@ -48,11 +48,7 @@ func TestRoutingResolve(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
pkhash, err := u.Hash(pubkb) pkhash := u.Hash(pubkb)
if err != nil {
t.Fatal(err)
}
res, err := resolve.Resolve(u.Key(pkhash).Pretty()) res, err := resolve.Resolve(u.Key(pkhash).Pretty())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
......
...@@ -45,10 +45,7 @@ func (r *RoutingResolver) Resolve(name string) (string, error) { ...@@ -45,10 +45,7 @@ func (r *RoutingResolver) Resolve(name string) (string, error) {
// use the routing system to get the name. // use the routing system to get the name.
// /ipns/<name> // /ipns/<name>
h, err := u.Hash([]byte("/ipns/" + name)) h := u.Hash([]byte("/ipns/" + name))
if err != nil {
return "", err
}
ipnsKey := u.Key(h) ipnsKey := u.Key(h)
val, err := r.routing.GetValue(ctx, ipnsKey) val, err := r.routing.GetValue(ctx, ipnsKey)
......
...@@ -68,7 +68,7 @@ func TestQueue(t *testing.T) { ...@@ -68,7 +68,7 @@ func TestQueue(t *testing.T) {
func newPeerTime(t time.Time) *peer.Peer { func newPeerTime(t time.Time) *peer.Peer {
s := fmt.Sprintf("hmmm time: %v", t) s := fmt.Sprintf("hmmm time: %v", t)
h, _ := u.Hash([]byte(s)) h := u.Hash([]byte(s))
return &peer.Peer{ID: peer.ID(h)} return &peer.Peer{ID: peer.ID(h)}
} }
......
...@@ -54,8 +54,15 @@ func KeyFromDsKey(dsk ds.Key) Key { ...@@ -54,8 +54,15 @@ func KeyFromDsKey(dsk ds.Key) Key {
} }
// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits // Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits
func Hash(data []byte) (mh.Multihash, error) { func Hash(data []byte) mh.Multihash {
return mh.Sum(data, mh.SHA2_256, -1) h, err := mh.Sum(data, mh.SHA2_256, -1)
if err != nil {
// this error can be safely ignored (panic) because multihash only fails
// from the selection of hash function. If the fn + length are valid, it
// won't error.
panic("multihash failed to hash using SHA2_256.")
}
return h
} }
// IsValidHash checks whether a given hash is valid (b58 decodable, len > 0) // IsValidHash checks whether a given hash is valid (b58 decodable, len > 0)
......
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