@cryptotaxi247 / kubo / commits / f3ae0e8e1

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

Juan Batiz-Benet committed Oct 6, 2014 at 02:26 UTC f3ae0e8e1b0663cec133b00ef7c0b805200c4b65
10 files changed +21 -47
blocks/blocks.go
+1 -5
@@ -13,11 +13,7 @@ type Block struct {
13
14 // NewBlock creates a Block object from opaque data. It will hash the data.
15 func NewBlock(data []byte) (*Block, error) {
16 - h, err := u.Hash(data)
17 - if err != nil {
18 - return nil, err
19 - }
20 - return &Block{Data: data, Multihash: h}, nil
16 + return &Block{Data: data, Multihash: u.Hash(data)}, nil
17 }
18
19 // Key returns the block's Multihash as a Key value.
blockservice/blocks_test.go
+1 -6
@@ -23,12 +23,7 @@ func TestBlocks(t *testing.T) {
23 return
24 }
25
26 - h, err := u.Hash([]byte("beep boop"))
27 - if err != nil {
28 - t.Error("failed to hash data", err)
29 - return
30 - }
31 -
26 + h := u.Hash([]byte("beep boop"))
27 if !bytes.Equal(b.Multihash, h) {
28 t.Error("Block Multihash and data multihash not equal")
29 }
crypto/key.go
+1 -1
@@ -249,5 +249,5 @@ func KeyHash(k Key) ([]byte, error) {
249 if err != nil {
250 return nil, err
251 }
252 - return u.Hash(kb)
252 + return u.Hash(kb), nil
253 }
crypto/spipe/handshake.go
+3 -13
@@ -292,25 +292,15 @@ func IDFromPubKey(pk ci.PubKey) (peer.ID, error) {
292 if err != nil {
293 return nil, err
294 }
295 - hash, err := u.Hash(b)
296 - if err != nil {
297 - return nil, err
298 - }
295 + hash := u.Hash(b)
296 return peer.ID(hash), nil
297 }
298
299 // Determines which algorithm to use. Note: f(a, b) = f(b, a)
300 func selectBest(myPrefs, theirPrefs string) (string, error) {
301 // Person with greatest hash gets first choice.
305 - myHash, err := u.Hash([]byte(myPrefs))
306 - if err != nil {
307 - return "", err
308 - }
309 -
310 - theirHash, err := u.Hash([]byte(theirPrefs))
311 - if err != nil {
312 - return "", err
313 - }
302 + myHash := u.Hash([]byte(myPrefs))
303 + theirHash := u.Hash([]byte(theirPrefs))
304
305 cmp := bytes.Compare(myHash, theirHash)
306 var firstChoiceArr, secChoiceArr []string
merkledag/merkledag.go
+1 -1
@@ -130,7 +130,7 @@ func (n *Node) Multihash() (mh.Multihash, error) {
130 return nil, err
131 }
132
133 - return u.Hash(b)
133 + return u.Hash(b), nil
134 }
135
136 // Key returns the Multihash as a key, for maps.
namesys/publisher.go
+2 -9
@@ -42,16 +42,9 @@ func (p *ipnsPublisher) Publish(k ci.PrivKey, value string) error {
42 return nil
43 }
44
45 - nameb, err := u.Hash(pkbytes)
46 - if err != nil {
47 - return nil
48 - }
45 + nameb := u.Hash(pkbytes)
46 namekey := u.Key(nameb).Pretty()
50 -
51 - ipnskey, err := u.Hash([]byte("/ipns/" + namekey))
52 - if err != nil {
53 - return err
54 - }
47 + ipnskey := u.Hash([]byte("/ipns/" + namekey))
48
49 // Store associated public key
50 timectx, _ := context.WithDeadline(ctx, time.Now().Add(time.Second*4))
namesys/resolve_test.go
+1 -5
@@ -48,11 +48,7 @@ func TestRoutingResolve(t *testing.T) {
48 t.Fatal(err)
49 }
50
51 - pkhash, err := u.Hash(pubkb)
52 - if err != nil {
53 - t.Fatal(err)
54 - }
55 -
51 + pkhash := u.Hash(pubkb)
52 res, err := resolve.Resolve(u.Key(pkhash).Pretty())
53 if err != nil {
54 t.Fatal(err)
namesys/routing.go
+1 -4
@@ -45,10 +45,7 @@ func (r *RoutingResolver) Resolve(name string) (string, error) {
45
46 // use the routing system to get the name.
47 // /ipns/<name>
48 - h, err := u.Hash([]byte("/ipns/" + name))
49 - if err != nil {
50 - return "", err
51 - }
48 + h := u.Hash([]byte("/ipns/" + name))
49
50 ipnsKey := u.Key(h)
51 val, err := r.routing.GetValue(ctx, ipnsKey)
peer/queue/queue_test.go
+1 -1
@@ -68,7 +68,7 @@ func TestQueue(t *testing.T) {
68
69 func newPeerTime(t time.Time) *peer.Peer {
70 s := fmt.Sprintf("hmmm time: %v", t)
71 - h, _ := u.Hash([]byte(s))
71 + h := u.Hash([]byte(s))
72 return &peer.Peer{ID: peer.ID(h)}
73 }
74
util/util.go
+9 -2
@@ -54,8 +54,15 @@ func KeyFromDsKey(dsk ds.Key) Key {
54 }
55
56 // Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits
57 -func Hash(data []byte) (mh.Multihash, error) {
58 - return mh.Sum(data, mh.SHA2_256, -1)
57 +func Hash(data []byte) mh.Multihash {
58 + h, err := mh.Sum(data, mh.SHA2_256, -1)
59 + if err != nil {
60 + // this error can be safely ignored (panic) because multihash only fails
61 + // from the selection of hash function. If the fn + length are valid, it
62 + // won't error.
63 + panic("multihash failed to hash using SHA2_256.")
64 + }
65 + return h
66 }
67
68 // IsValidHash checks whether a given hash is valid (b58 decodable, len > 0)