@@ -33,22 +33,33 @@ func main() {
3333 flag .Var (& files , "file" , "Files to encrypt or decrypt (can be specified multiple times)" )
3434 flag .Var (& files , "f" , "Files to encrypt or decrypt (shorthand)" )
3535
36- key := flag .String ("key" , "" , "Path to the key file" )
37- flag .StringVar (key , "k" , "" , "Path to the key file (shorthand)" )
36+ var key string
37+ flag .StringVar (& key , "key" , "" , "Path to the key file" )
38+ flag .StringVar (& key , "k" , "" , "Path to the key file (shorthand)" )
3839
39- password := flag .String ("password" , "" , "Password for encryption/decryption" )
40- flag .StringVar (password , "p" , "" , "Password for encryption/decryption (shorthand)" )
40+ var password string
41+ flag .StringVar (& password , "password" , "" , "Password for encryption/decryption" )
42+ flag .StringVar (& password , "p" , "" , "Password for encryption/decryption (shorthand)" )
4143
4244 generateKeys := flag .Bool ("generate-keys" , false , "Generate a new RSA key pair" )
4345 keyBaseName := flag .String ("key-name" , "key" , "Base name for the generated key files" )
4446
4547 flag .Parse ()
46- // Add handling for remaining arguments
48+ // Add remaining arguments as files only if they don't start with "-"
49+ // This prevents treating flags like "-k" as files
4750 remainingArgs := flag .Args ()
48- if len (remainingArgs ) > 0 {
49- files = append (files , remainingArgs ... )
51+ for _ , arg := range remainingArgs {
52+ if ! strings .HasPrefix (arg , "-" ) {
53+ files = append (files , arg )
54+ }
5055 }
5156 logger .LogDebug ("Parsed command line flags" )
57+
58+ // Debug flag values
59+ logger .LogDebug (fmt .Sprintf ("Encrypt: %v, Decrypt: %v" , * encrypt , * decrypt ))
60+ logger .LogDebug (fmt .Sprintf ("Key: '%s', Password: '%s'" , key , password ))
61+ logger .LogDebug (fmt .Sprintf ("Files: %v" , files ))
62+ logger .LogDebug (fmt .Sprintf ("Generate Keys: %v, Key Base Name: %s" , * generateKeys , * keyBaseName ))
5263
5364 if * generateKeys && ! * encrypt && len (files ) == 0 {
5465 if err := handleGenerateKeys (* keyBaseName , logger ); err != nil {
@@ -59,7 +70,7 @@ func main() {
5970 os .Exit (0 )
6071 }
6172
62- if err := validateFlags (* encrypt , * decrypt , files , * key , * password , * generateKeys ); err != nil {
73+ if err := validateFlags (* encrypt , * decrypt , files , key , password , * generateKeys ); err != nil {
6374 logger .LogError (err .Error ())
6475 flag .Usage ()
6576 os .Exit (1 )
@@ -74,10 +85,10 @@ func main() {
7485 outputFiles , err = handleGenerateAndEncrypt (* keyBaseName , files , logger )
7586 } else if * encrypt {
7687 operation = "Encryption"
77- outputFiles , err = handleEncryption (files , * key , * password , logger )
88+ outputFiles , err = handleEncryption (files , key , password , logger )
7889 } else {
7990 operation = "Decryption"
80- outputFiles , err = handleDecryption (files , * key , * password , logger )
91+ outputFiles , err = handleDecryption (files , key , password , logger )
8192 }
8293
8394 if err != nil {
@@ -172,65 +183,96 @@ func handleGenerateAndEncrypt(keyBaseName string, files []string, logger *loggin
172183 return outputFiles , nil
173184}
174185
175- func handleEncryption ( files [] string , key , password string , logger * logging. Logger ) ([] string , error ) {
176- logger . LogInfo ( "Starting file encryption" )
177- var encryptor crypto. Encryptor
186+ // initializeCrypto initializes either an encryptor or decryptor based on the provided parameters
187+ func initializeCrypto ( isEncryption bool , key , password string , logger * logging. Logger ) ( interface {}, error ) {
188+ var result interface {}
178189 var err error
190+ operationType := map [bool ]string {true : "encryptor" , false : "decryptor" }[isEncryption ]
179191
180192 if key != "" {
181- encryptor , err = crypto .NewRSAEncryptor (key )
193+ if isEncryption {
194+ result , err = crypto .NewRSAEncryptor (key )
195+ } else {
196+ result , err = crypto .NewRSADecryptor (key )
197+ }
182198 } else {
183- encryptor , err = crypto .NewPasswordEncryptor (password )
199+ if isEncryption {
200+ result , err = crypto .NewPasswordEncryptor (password )
201+ } else {
202+ result , err = crypto .NewPasswordDecryptor (password )
203+ }
184204 }
185- logger .LogDebugf ("Initialized encryptor: %T" , encryptor )
186-
205+
187206 if err != nil {
188- return nil , fmt .Errorf ("error initializing encryptor : %v" , err )
207+ return nil , fmt .Errorf ("error initializing %s : %v" , operationType , err )
189208 }
209+
210+ logger .LogDebugf ("Initialized %s: %T" , operationType , result )
211+ return result , nil
212+ }
190213
214+ // processFiles handles the common file processing logic for both encryption and decryption
215+ func processFiles (
216+ files []string ,
217+ isEncryption bool ,
218+ cryptoProcessor interface {},
219+ logger * logging.Logger ,
220+ ) ([]string , error ) {
221+ operation := map [bool ]string {true : "encryption" , false : "decryption" }[isEncryption ]
191222 outputFiles := make ([]string , 0 , len (files ))
223+
224+ logger .LogInfo (fmt .Sprintf ("Found %d files to process" , len (files )))
225+
192226 for _ , file := range files {
193- if hash , err := crypto .CalculateFileHash (file ); err == nil {
194- logger .LogDebug (fmt .Sprintf ("Original file hash for %s: %s" , file , hash ))
227+ var outputFile string
228+ var processErr error
229+
230+ logger .LogInfo (fmt .Sprintf ("Starting %s of file: %s" , strings .ToLower (operation ), file ))
231+
232+ if isEncryption {
233+ // For encryption operations
234+ if hash , err := crypto .CalculateFileHash (file ); err == nil {
235+ logger .LogDebug (fmt .Sprintf ("Original file hash for %s: %s" , file , hash ))
236+ }
237+ outputFile = file + ".enc"
238+ processErr = fileops .EncryptFile (file , outputFile , cryptoProcessor .(crypto.Encryptor ), logger )
239+ } else {
240+ // For decryption operations
241+ outputFile = strings .TrimSuffix (file , ".enc" )
242+ processErr = fileops .DecryptFile (file , outputFile , cryptoProcessor .(crypto.Decryptor ), logger )
195243 }
196-
197- outputFile := file + ".enc"
198- if err := fileops .EncryptFile (file , outputFile , encryptor , logger ); err != nil {
199- return outputFiles , fmt .Errorf ("failed to encrypt %s: %w" , file , err )
244+
245+ if processErr != nil {
246+ if ! isEncryption && strings .Contains (processErr .Error (), "file integrity check failed" ) {
247+ return outputFiles , fmt .Errorf ("security error while decrypting %s: %v" , file , processErr )
248+ }
249+ return outputFiles , fmt .Errorf ("failed to %s %s: %w" , operation , file , processErr )
200250 }
251+
201252 outputFiles = append (outputFiles , outputFile )
202253 }
203-
254+
204255 return outputFiles , nil
205256}
206257
207- func handleDecryption (files []string , key , password string , logger * logging.Logger ) ([]string , error ) {
208- logger .LogInfo ("Starting file decryption" )
209- var decryptor crypto.Decryptor
210- var err error
211-
212- if key != "" {
213- decryptor , err = crypto .NewRSADecryptor (key )
214- } else {
215- decryptor , err = crypto .NewPasswordDecryptor (password )
216- }
217- logger .LogDebugf ("Initialized decryptor: %T" , decryptor )
218-
258+ func handleEncryption (files []string , key , password string , logger * logging.Logger ) ([]string , error ) {
259+ logger .LogInfo ("Starting file encryption" )
260+
261+ encryptor , err := initializeCrypto (true , key , password , logger )
219262 if err != nil {
220- return nil , fmt . Errorf ( "error initializing decryptor: %v" , err )
263+ return nil , err
221264 }
265+
266+ return processFiles (files , true , encryptor , logger )
267+ }
222268
223- outputFiles := make ([]string , 0 , len (files ))
224- for _ , file := range files {
225- outputFile := strings .TrimSuffix (file , ".enc" )
226- if err := fileops .DecryptFile (file , outputFile , decryptor , logger ); err != nil {
227- if strings .Contains (err .Error (), "file integrity check failed" ) {
228- return outputFiles , fmt .Errorf ("security error while decrypting %s: %v" , file , err )
229- }
230- return outputFiles , fmt .Errorf ("failed to decrypt %s: %w" , file , err )
231- }
232- outputFiles = append (outputFiles , outputFile )
269+ func handleDecryption (files []string , key , password string , logger * logging.Logger ) ([]string , error ) {
270+ logger .LogInfo ("Starting file decryption" )
271+
272+ decryptor , err := initializeCrypto (false , key , password , logger )
273+ if err != nil {
274+ return nil , err
233275 }
234-
235- return outputFiles , nil
276+
277+ return processFiles ( files , false , decryptor , logger )
236278}
0 commit comments