Skip to content

Commit

Permalink
refactor(core): refactor newProcessor func for better DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
70sh1 committed Feb 4, 2025
1 parent 4b6e644 commit 478f144
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,22 @@ type processor struct {

// Creates new ChaCha20-BLAKE2b processor with underlying "source" file.
func newProcessor(source *os.File, password string, mode Mode) (*processor, error) {
nonce := make([]byte, chacha20.NonceSize)
var err error
var headerSource io.Reader

if mode == Encryption {
_, err = io.ReadFull(rand.Reader, nonce)
headerSource = rand.Reader
} else {
_, err = io.ReadFull(source, nonce)
headerSource = source
}

nonce := make([]byte, chacha20.NonceSize)
_, err := io.ReadFull(headerSource, nonce)
if err != nil {
return nil, fmt.Errorf("error generating/reading nonce: %w", err)
}

salt := make([]byte, 16)
if mode == Encryption {
_, err = io.ReadFull(rand.Reader, salt)
} else {
_, err = io.ReadFull(source, salt)
}
_, err = io.ReadFull(headerSource, salt)
if err != nil {
return nil, fmt.Errorf("error generating/reading salt: %w", err)
}
Expand Down

0 comments on commit 478f144

Please sign in to comment.