Skip to content

Commit

Permalink
fix: header size in decrypt, and add file listing status update
Browse files Browse the repository at this point in the history
  • Loading branch information
mrf345 committed Sep 6, 2024
1 parent 9a2c2f5 commit db4d316
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cmd/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var encryptCmd = &cobra.Command{
sl.Quiet = beQuiet
inputPath, outputPath := []string{args[0]}, args[1]

if outputFile, err = os.OpenFile(outputPath, os.O_RDWR|os.O_CREATE, 0776); err != nil {
if outputFile, err = os.OpenFile(outputPath, os.O_RDWR|os.O_CREATE, 0755); err != nil {
utils.PrintErrsAndExit((&slErrs.ErrInvalidOutputPath{
Path: outputPath,
Err: err,
Expand Down
2 changes: 1 addition & 1 deletion safelock/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ func (sl *Safelock) decryptFiles(
}

go sl.updateProgressStatus(ctx, "Decrypting", calc)
defer slReader.cancel()

fileHandler := getExtractFileHandler(outputPath)

if err = sl.Archival.Extract(ctx, reader, nil, fileHandler); err != nil {
return fmt.Errorf("cannot extract archive file > %w", err)
}

slReader.cancel()
return
}

Expand Down
27 changes: 25 additions & 2 deletions safelock/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ func (sl *Safelock) encryptFiles(
slWriter *safelockWriter,
calc *utils.PercentCalculator,
) (err error) {
sl.updateStatus("Listing and preparing files ", 5.0)

var files []archiver.File
var filesMap = make(map[string]string, len(inputPaths))
var cancelListingStatus = sl.updateListingStatus(ctx, 1.0, calc.Start)

for _, path := range inputPaths {
filesMap[path] = ""
Expand All @@ -126,6 +125,7 @@ func (sl *Safelock) encryptFiles(
calc.InputSize += int(file.Size())
}

cancelListingStatus()
sl.updateProgressStatus(ctx, "Encrypting", calc)
}()

Expand All @@ -138,6 +138,29 @@ func (sl *Safelock) encryptFiles(
return
}

func (sl *Safelock) updateListingStatus(ctx context.Context, start, end float64) (cancel context.CancelFunc) {
ctx, cancel = context.WithCancel(ctx)

go func() {
for {
select {
case <-ctx.Done():
return
default:
if start >= end {
return
}

start += 1
sl.updateStatus("Listing and preparing files", start)
time.Sleep(time.Second / 2)
}
}
}()

return
}

func (sl *Safelock) updateProgressStatus(ctx context.Context, act string, calc *utils.PercentCalculator) {
for {
select {
Expand Down
10 changes: 5 additions & 5 deletions safelock/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ func newReader(
}
}

func (sr *safelockReader) setSize() {
sr.size = sr.calc.InputSize
sr.setHeaderSize()
}

func (sr *safelockReader) ReadHeader() (err error) {
sr.setSize()

Expand Down Expand Up @@ -79,6 +74,11 @@ func (sr *safelockReader) ReadHeader() (err error) {
return
}

func (sr *safelockReader) setSize() {
sr.size = sr.calc.InputSize
sr.headerSize = sr.config.getHeaderSizeOut(sr.size)
}

func (sr *safelockReader) Read(chunk []byte) (read int, err error) {
var blockSize int
var block string
Expand Down
4 changes: 0 additions & 4 deletions safelock/readerWriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ type safelockReaderWriterBase struct {
err error
}

func (srw *safelockReaderWriterBase) setHeaderSize() {
srw.headerSize = srw.config.getHeaderSize(srw.size)
}

func (srw *safelockReaderWriterBase) diffSize() int64 {
return int64(srw.size - srw.headerSize)
}
Expand Down
20 changes: 16 additions & 4 deletions safelock/safelock.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,24 @@ type EncryptionConfig struct {
GcmBufferSize int
}

func (ec *EncryptionConfig) getHeaderSize(fileSize int) int {
if ec.HeaderRatio > fileSize/100 {
func (ec *EncryptionConfig) getHeaderSizeIn(fileSize int) int {
size := fileSize / ec.HeaderRatio

if ec.HeaderRatio > size {
return ec.HeaderRatio
}

return (fileSize + size) / ec.HeaderRatio
}

func (ec *EncryptionConfig) getHeaderSizeOut(fileSize int) int {
size := fileSize / ec.HeaderRatio

if ec.HeaderRatio > size {
return ec.HeaderRatio
}

return int(fileSize) / ec.HeaderRatio
return fileSize / ec.HeaderRatio
}

// archiving and compression configuration settings
Expand Down Expand Up @@ -78,7 +90,7 @@ func New() *Safelock {
KeyLength: 48,
NonceLength: 12,
MinPasswordLength: 8,
HeaderRatio: 7400,
HeaderRatio: 1024 * 4,
GcmBufferSize: 500,
Hash: sha512.New384,
},
Expand Down
10 changes: 5 additions & 5 deletions safelock/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ func newWriter(
}
}

func (sw *safelockWriter) setSize() {
sw.size = sw.calc.OutputSize
sw.setHeaderSize()
}

func (sw *safelockWriter) Write(chunk []byte) (written int, err error) {
encrypted := sw.asyncGcm.encryptChunk(chunk)

Expand Down Expand Up @@ -74,3 +69,8 @@ func (sw *safelockWriter) WriteHeader() (err error) {

return
}

func (sw *safelockWriter) setSize() {
sw.size = sw.calc.OutputSize
sw.headerSize = sw.config.getHeaderSizeIn(sw.size)
}
18 changes: 9 additions & 9 deletions utils/percentCalculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (
type PercentCalculator struct {
OutputSize int
InputSize int
start float64
end float64
Start float64
End float64
}

// create new instance of [utils.PercentCalculator] for input paths
func NewPathsCalculator(start float64) *PercentCalculator {
return &PercentCalculator{
start: start,
end: 100.0,
Start: start,
End: 100.0,
}
}

// create new instance of [utils.PercentCalculator] for [io.Seeker]
func NewSeekerCalculator(inputSeeker io.Seeker, start float64) (pc *PercentCalculator, err error) {
pc = &PercentCalculator{
start: start,
end: 100.0,
Start: start,
End: 100.0,
}
err = pc.setInputSeekerSize(inputSeeker)
return
Expand All @@ -38,11 +38,11 @@ func (pc *PercentCalculator) setInputSeekerSize(seeker io.Seeker) (err error) {

// get current completion percentage
func (pc *PercentCalculator) GetPercent() float64 {
percent := pc.start + (float64(pc.OutputSize) / float64(pc.InputSize) * pc.end)
percent := pc.Start + (float64(pc.OutputSize) / float64(pc.InputSize) * pc.End)

if pc.end > percent {
if pc.End > percent {
return percent
}

return pc.end
return pc.End
}

0 comments on commit db4d316

Please sign in to comment.