fix piece id derivation (#1462)
This commit is contained in:
parent
af889f1554
commit
0560f076a5
@ -68,8 +68,9 @@ func (id PieceID2) Bytes() []byte { return id[:] }
|
||||
func (id PieceID2) Derive(storagenodeID NodeID) PieceID2 {
|
||||
// TODO: should the secret / content be swapped?
|
||||
mac := hmac.New(sha512.New, id.Bytes())
|
||||
_, _ = mac.Write(storagenodeID.Bytes()) // on hash.Hash write never returns an error
|
||||
var derived PieceID2
|
||||
copy(derived[:], mac.Sum(storagenodeID.Bytes()))
|
||||
copy(derived[:], mac.Sum(nil))
|
||||
return derived
|
||||
}
|
||||
|
||||
|
@ -41,15 +41,21 @@ func TestEncode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDerivePieceID(t *testing.T) {
|
||||
pieceid := storj.NewPieceID()
|
||||
a := testplanet.MustPregeneratedIdentity(0).ID
|
||||
b := testplanet.MustPregeneratedIdentity(1).ID
|
||||
a := storj.NewPieceID()
|
||||
b := storj.NewPieceID()
|
||||
|
||||
apieceid := pieceid.Derive(a)
|
||||
bpieceid := pieceid.Derive(b)
|
||||
n0 := testplanet.MustPregeneratedIdentity(0).ID
|
||||
n1 := testplanet.MustPregeneratedIdentity(1).ID
|
||||
|
||||
assert.NotEqual(t, apieceid, bpieceid)
|
||||
assert.NotEqual(t, a.Derive(n0), a.Derive(n1), "a(n0) != a(n1)")
|
||||
assert.NotEqual(t, b.Derive(n0), b.Derive(n1), "b(n0) != b(n1)")
|
||||
assert.NotEqual(t, a.Derive(n0), b.Derive(n0), "a(n0) != b(n0)")
|
||||
assert.NotEqual(t, a.Derive(n1), b.Derive(n1), "a(n1) != b(n1)")
|
||||
|
||||
assert.Equal(t, apieceid, pieceid.Derive(a))
|
||||
assert.Equal(t, bpieceid, pieceid.Derive(b))
|
||||
// idempotent
|
||||
assert.Equal(t, a.Derive(n0), a.Derive(n0), "a(n0)")
|
||||
assert.Equal(t, a.Derive(n1), a.Derive(n1), "a(n1)")
|
||||
|
||||
assert.Equal(t, b.Derive(n0), b.Derive(n0), "b(n0)")
|
||||
assert.Equal(t, b.Derive(n1), b.Derive(n1), "b(n1)")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user