Skip to content

Commit c4aaffc

Browse files
committed
code split
1 parent d3d4f63 commit c4aaffc

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

compressor/compressor.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"path/filepath"
1212

1313
"file-compressor/utils"
14+
"file-compressor/encryption"
1415
)
1516

1617
// Compress compresses the specified files or folders into a single compressed file.
@@ -90,7 +91,7 @@ func Compress(filenameStrs []string, outputDir *string, password *string) error
9091
// Encrypt compressed content if password is provided
9192
if password != nil && *password != "" {
9293
var err error
93-
compressedFile.Content, err = utils.Encrypt(compressedFile.Content, *password)
94+
compressedFile.Content, err = encryption.Encrypt(compressedFile.Content, *password)
9495
if err != nil {
9596
return fmt.Errorf("encryption error: %w", err)
9697
}
@@ -160,7 +161,7 @@ func Decompress(filenameStrs []string, outputDir *string, password *string) erro
160161

161162
// Decrypt compressed content if password is provided
162163
if password != nil && *password != "" {
163-
compressedContent, err = utils.Decrypt(compressedContent, *password)
164+
compressedContent, err = encryption.Decrypt(compressedContent, *password)
164165
if err != nil {
165166
return fmt.Errorf("decryption error: %w", err)
166167
}

utils/encryption.go renamed to encryption/encryption.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package encryption
22

33
import (
44
"crypto/aes"
@@ -12,18 +12,6 @@ const (
1212
MetadataLength = 1 // Length of the metadata
1313
)
1414

15-
// Function to generate key from password
16-
func generateKey(password string) ([]byte, error) {
17-
key := []byte(password)
18-
if len(key) > 32 {
19-
return nil, errors.New("password too long")
20-
}
21-
for len(key) < 32 {
22-
key = append(key, '0')
23-
}
24-
return key, nil
25-
}
26-
2715
// Encrypt function
2816
func Encrypt(data []byte, password string) ([]byte, error) {
2917
var metadata byte

encryption/keygen.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package encryption
2+
3+
import (
4+
"errors"
5+
)
6+
7+
// Function to generate key from password
8+
func generateKey(password string) ([]byte, error) {
9+
key := []byte(password)
10+
if len(key) > 32 {
11+
return nil, errors.New("password too long")
12+
}
13+
for len(key) < 32 {
14+
key = append(key, '0')
15+
}
16+
return key, nil
17+
}

0 commit comments

Comments
 (0)