fix piece id derivation (#1462)

This commit is contained in:
Egon Elbre 2019-03-12 19:35:42 +02:00 committed by GitHub
parent af889f1554
commit 0560f076a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 9 deletions

View File

@ -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
}

View File

@ -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)")
}