mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 03:42:21 +08:00
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
This commit is contained in:
@ -13,11 +13,7 @@ type Block struct {
|
||||
|
||||
// NewBlock creates a Block object from opaque data. It will hash the data.
|
||||
func NewBlock(data []byte) (*Block, error) {
|
||||
h, err := u.Hash(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Block{Data: data, Multihash: h}, nil
|
||||
return &Block{Data: data, Multihash: u.Hash(data)}, nil
|
||||
}
|
||||
|
||||
// Key returns the block's Multihash as a Key value.
|
||||
|
@ -23,12 +23,7 @@ func TestBlocks(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
h, err := u.Hash([]byte("beep boop"))
|
||||
if err != nil {
|
||||
t.Error("failed to hash data", err)
|
||||
return
|
||||
}
|
||||
|
||||
h := u.Hash([]byte("beep boop"))
|
||||
if !bytes.Equal(b.Multihash, h) {
|
||||
t.Error("Block Multihash and data multihash not equal")
|
||||
}
|
||||
|
@ -249,5 +249,5 @@ func KeyHash(k Key) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return u.Hash(kb)
|
||||
return u.Hash(kb), nil
|
||||
}
|
||||
|
@ -292,25 +292,15 @@ func IDFromPubKey(pk ci.PubKey) (peer.ID, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hash, err := u.Hash(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hash := u.Hash(b)
|
||||
return peer.ID(hash), nil
|
||||
}
|
||||
|
||||
// Determines which algorithm to use. Note: f(a, b) = f(b, a)
|
||||
func selectBest(myPrefs, theirPrefs string) (string, error) {
|
||||
// Person with greatest hash gets first choice.
|
||||
myHash, err := u.Hash([]byte(myPrefs))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
theirHash, err := u.Hash([]byte(theirPrefs))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
myHash := u.Hash([]byte(myPrefs))
|
||||
theirHash := u.Hash([]byte(theirPrefs))
|
||||
|
||||
cmp := bytes.Compare(myHash, theirHash)
|
||||
var firstChoiceArr, secChoiceArr []string
|
||||
|
@ -130,7 +130,7 @@ func (n *Node) Multihash() (mh.Multihash, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return u.Hash(b)
|
||||
return u.Hash(b), nil
|
||||
}
|
||||
|
||||
// Key returns the Multihash as a key, for maps.
|
||||
|
@ -42,16 +42,9 @@ func (p *ipnsPublisher) Publish(k ci.PrivKey, value string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
nameb, err := u.Hash(pkbytes)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
nameb := u.Hash(pkbytes)
|
||||
namekey := u.Key(nameb).Pretty()
|
||||
|
||||
ipnskey, err := u.Hash([]byte("/ipns/" + namekey))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ipnskey := u.Hash([]byte("/ipns/" + namekey))
|
||||
|
||||
// Store associated public key
|
||||
timectx, _ := context.WithDeadline(ctx, time.Now().Add(time.Second*4))
|
||||
|
@ -48,11 +48,7 @@ func TestRoutingResolve(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
pkhash, err := u.Hash(pubkb)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
pkhash := u.Hash(pubkb)
|
||||
res, err := resolve.Resolve(u.Key(pkhash).Pretty())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -45,10 +45,7 @@ func (r *RoutingResolver) Resolve(name string) (string, error) {
|
||||
|
||||
// use the routing system to get the name.
|
||||
// /ipns/<name>
|
||||
h, err := u.Hash([]byte("/ipns/" + name))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
h := u.Hash([]byte("/ipns/" + name))
|
||||
|
||||
ipnsKey := u.Key(h)
|
||||
val, err := r.routing.GetValue(ctx, ipnsKey)
|
||||
|
@ -68,7 +68,7 @@ func TestQueue(t *testing.T) {
|
||||
|
||||
func newPeerTime(t time.Time) *peer.Peer {
|
||||
s := fmt.Sprintf("hmmm time: %v", t)
|
||||
h, _ := u.Hash([]byte(s))
|
||||
h := u.Hash([]byte(s))
|
||||
return &peer.Peer{ID: peer.ID(h)}
|
||||
}
|
||||
|
||||
|
11
util/util.go
11
util/util.go
@ -54,8 +54,15 @@ func KeyFromDsKey(dsk ds.Key) Key {
|
||||
}
|
||||
|
||||
// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits
|
||||
func Hash(data []byte) (mh.Multihash, error) {
|
||||
return mh.Sum(data, mh.SHA2_256, -1)
|
||||
func Hash(data []byte) mh.Multihash {
|
||||
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)
|
||||
|
Reference in New Issue
Block a user