From 5229b8f5f5ef8d3e91fe2ab1f415131337161577 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 4 Oct 2015 11:03:40 +0200 Subject: [PATCH] Add BlockNoPlainOff() and BlockNoCipherOff() + test Also, fix key, it is now []byte, not [16]byte --- cryptfs/content_test.go | 26 ++++++++++++++++++++++++-- cryptfs/cryptfs_content.go | 10 ++++++++++ cryptfs/names_test.go | 6 ++++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/cryptfs/content_test.go b/cryptfs/content_test.go index b53eeeb..8fd33c6 100644 --- a/cryptfs/content_test.go +++ b/cryptfs/content_test.go @@ -19,7 +19,7 @@ func TestSplitRange(t *testing.T) { testRange{65444, 54}, testRange{6654, 8945}) - var key [16]byte + key := make([]byte, 16) f := NewCryptFS(key, true) for _, r := range(ranges) { @@ -42,7 +42,7 @@ func TestCiphertextRange(t *testing.T) { testRange{65444, 54}, testRange{6654, 8945}) - var key [16]byte + key := make([]byte, 16) f := NewCryptFS(key, true) for _, r := range(ranges) { @@ -58,3 +58,25 @@ func TestCiphertextRange(t *testing.T) { } } } + +func TestBlockNo(t *testing.T) { + key := make([]byte, 16) + f := NewCryptFS(key, true) + + b := f.BlockNoCipherOff(788) + if b != 0 { + t.Errorf("actual: %d", b) + } + b = f.BlockNoCipherOff(f.CipherBS()) + if b != 1 { + t.Errorf("actual: %d", b) + } + b = f.BlockNoPlainOff(788) + if b != 0 { + t.Errorf("actual: %d", b) + } + b = f.BlockNoPlainOff(f.PlainBS()) + if b != 1 { + t.Errorf("actual: %d", b) + } +} diff --git a/cryptfs/cryptfs_content.go b/cryptfs/cryptfs_content.go index a903e02..ab5ec78 100644 --- a/cryptfs/cryptfs_content.go +++ b/cryptfs/cryptfs_content.go @@ -219,3 +219,13 @@ func (be *CryptFS) MergeBlocks(oldData []byte, newData []byte, offset int) []byt } return out[0:outLen] } + +// Get the block number at plain-text offset +func (be *CryptFS) BlockNoPlainOff(plainOffset uint64) uint64 { + return plainOffset / be.plainBS +} + +// Get the block number at ciphter-text offset +func (be *CryptFS) BlockNoCipherOff(cipherOffset uint64) uint64 { + return cipherOffset / be.cipherBS +} diff --git a/cryptfs/names_test.go b/cryptfs/names_test.go index 30ecfb9..7e20690 100644 --- a/cryptfs/names_test.go +++ b/cryptfs/names_test.go @@ -11,7 +11,7 @@ func TestTranslatePath(t *testing.T) { s = append(s, "foo12312312312312312313123123123") s = append(s, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890") - var key [16]byte + key := make([]byte, 16) fs := NewCryptFS(key, true) for _, n := range(s) { @@ -32,8 +32,10 @@ func TestPad16(t *testing.T) { s = append(s, []byte("foo")) s = append(s, []byte("12345678901234567")) s = append(s, []byte("12345678901234567abcdefg")) - var key [16]byte + + key := make([]byte, 16) fs := NewCryptFS(key, true) + for i := range(s) { orig := s[i] padded := fs.pad16(orig)