Skip to content

Commit

Permalink
fix(seg65) (#4604)
Browse files Browse the repository at this point in the history
Co-authored-by: Acha Bill <achabill12@gmail.com>
  • Loading branch information
zelig and acha-bill committed Mar 15, 2024
1 parent 4f32231 commit 776f546
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 29 deletions.
11 changes: 5 additions & 6 deletions pkg/encryption/chunk_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,21 @@ func NewChunkEncrypter() ChunkEncrypter { return &chunkEncrypter{} }

func (c *chunkEncrypter) EncryptChunk(chunkData []byte) (Key, []byte, []byte, error) {
key := GenerateRandomKey(KeyLength)
encryptedSpan, err := newSpanEncryption(key).Encrypt(chunkData[:8])
encryptedSpan, err := NewSpanEncryption(key).Encrypt(chunkData[:8])
if err != nil {
return nil, nil, nil, err
}
encryptedData, err := newDataEncryption(key).Encrypt(chunkData[8:])
encryptedData, err := NewDataEncryption(key).Encrypt(chunkData[8:])
if err != nil {
return nil, nil, nil, err
}
return key, encryptedSpan, encryptedData, nil
}

func newSpanEncryption(key Key) Interface {
refSize := int64(swarm.HashSize + KeyLength)
return New(key, 0, uint32(swarm.ChunkSize/refSize), sha3.NewLegacyKeccak256)
func NewSpanEncryption(key Key) Interface {
return New(key, 0, uint32(swarm.ChunkSize/KeyLength), sha3.NewLegacyKeccak256)
}

func newDataEncryption(key Key) Interface {
func NewDataEncryption(key Key) Interface {
return New(key, int(swarm.ChunkSize), 0, sha3.NewLegacyKeccak256)
}
14 changes: 2 additions & 12 deletions pkg/encryption/store/decrypt_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/ethersphere/bee/pkg/file/redundancy"
storage "github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
"golang.org/x/crypto/sha3"
)

type decryptingStore struct {
Expand Down Expand Up @@ -73,22 +72,13 @@ func DecryptChunkData(chunkData []byte, encryptionKey encryption.Key) ([]byte, e
}

func decrypt(chunkData []byte, key encryption.Key) ([]byte, []byte, error) {
decryptedSpan, err := newSpanEncryption(key).Decrypt(chunkData[:swarm.SpanSize])
decryptedSpan, err := encryption.NewSpanEncryption(key).Decrypt(chunkData[:swarm.SpanSize])
if err != nil {
return nil, nil, err
}
decryptedData, err := newDataEncryption(key).Decrypt(chunkData[swarm.SpanSize:])
decryptedData, err := encryption.NewDataEncryption(key).Decrypt(chunkData[swarm.SpanSize:])
if err != nil {
return nil, nil, err
}
return decryptedSpan, decryptedData, nil
}

func newSpanEncryption(key encryption.Key) encryption.Interface {
refSize := int64(swarm.HashSize + encryption.KeyLength)
return encryption.New(key, 0, uint32(swarm.ChunkSize/refSize), sha3.NewLegacyKeccak256)
}

func newDataEncryption(key encryption.Key) encryption.Interface {
return encryption.New(key, int(swarm.ChunkSize), 0, sha3.NewLegacyKeccak256)
}
13 changes: 2 additions & 11 deletions pkg/file/splitter/internal/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ethersphere/bee/pkg/file"
storage "github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
"golang.org/x/crypto/sha3"
)

// maximum amount of file tree levels this file hasher component can handle
Expand Down Expand Up @@ -256,21 +255,13 @@ func (s *SimpleSplitterJob) encryptChunkData(chunkData []byte) ([]byte, encrypti

func (s *SimpleSplitterJob) encrypt(chunkData []byte) (encryption.Key, []byte, []byte, error) {
key := encryption.GenerateRandomKey(encryption.KeyLength)
encryptedSpan, err := s.newSpanEncryption(key).Encrypt(chunkData[:8])
encryptedSpan, err := encryption.NewSpanEncryption(key).Encrypt(chunkData[:8])
if err != nil {
return nil, nil, nil, err
}
encryptedData, err := s.newDataEncryption(key).Encrypt(chunkData[8:])
encryptedData, err := encryption.NewDataEncryption(key).Encrypt(chunkData[8:])
if err != nil {
return nil, nil, nil, err
}
return key, encryptedSpan, encryptedData, nil
}

func (s *SimpleSplitterJob) newSpanEncryption(key encryption.Key) encryption.Interface {
return encryption.New(key, 0, uint32(swarm.ChunkSize/s.refSize), sha3.NewLegacyKeccak256)
}

func (s *SimpleSplitterJob) newDataEncryption(key encryption.Key) encryption.Interface {
return encryption.New(key, int(swarm.ChunkSize), 0, sha3.NewLegacyKeccak256)
}

0 comments on commit 776f546

Please sign in to comment.