From 747aa749f6c82e035c1a8e51c4083e3da00fef37 Mon Sep 17 00:00:00 2001 From: abdfnx Date: Fri, 18 Mar 2022 21:03:38 +0300 Subject: [PATCH] add `license` prop to secman brew config, add `encrypt` command --- .goreleaser.yml | 1 + app/encrypt.go | 75 +++++++++++++++++++++++++++++++++++++++++++++ app/opts.go | 8 +++++ cmd/secman/root.go | 1 + pkg/options/opts.go | 8 +++++ 5 files changed, 93 insertions(+) create mode 100755 app/encrypt.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 94ee4ed5..28f098c3 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -79,6 +79,7 @@ brews: name: homebrew-tap homepage: "https://secman.dev" description: "👊 Human-friendly and amazing TUI secrets manager" + license: "MIT" checksum: name_template: "checksums.txt" diff --git a/app/encrypt.go b/app/encrypt.go new file mode 100755 index 00000000..ca538785 --- /dev/null +++ b/app/encrypt.go @@ -0,0 +1,75 @@ +package app + +import ( + "io" + "os" + "fmt" + "crypto/aes" + "crypto/md5" + "crypto/rand" + "crypto/cipher" + "crypto/sha256" + "crypto/sha512" + + "github.com/spf13/cobra" +) + +func EncryptCMD() *cobra.Command { + cmd := &cobra.Command{ + Use: "encrypt", + Short: "Encrypt a value.", + Long: "Encrypt a value. with aes, sha256, and md5 algorithms.", + Run: func(cmd *cobra.Command, args []string) { + if EncryptOpts.AES { + if len(EncryptOpts.AESKey) < 32 { + fmt.Println("AES key must be 32 characters or longer.") + os.Exit(1) + } else { + text := []byte(args[0]) + key := []byte(EncryptOpts.AESKey) + + c, err := aes.NewCipher(key) + if err != nil { + fmt.Println(err) + } + + gcm, err := cipher.NewGCM(c) + if err != nil { + fmt.Println(err) + } + + nonce := make([]byte, gcm.NonceSize()) + + if _, err = io.ReadFull(rand.Reader, nonce); err != nil { + fmt.Println(err) + } + + fmt.Printf("%x\n", gcm.Seal(nonce, nonce, text, nil)) + } + } else if EncryptOpts.SHA256 { + hash := sha256.Sum256([]byte(args[0])) + + fmt.Printf("%x\n", hash) + } else if EncryptOpts.SHA512 { + hash := sha512.Sum512([]byte(args[0])) + + fmt.Printf("%x\n", hash) + } else if EncryptOpts.MD5 { + hash := md5.Sum([]byte(args[0])) + + fmt.Printf("%x\n", hash) + } else { + fmt.Println("No encryption algorithm selected.") + os.Exit(1) + } + }, + } + + cmd.Flags().BoolVarP(&EncryptOpts.AES, "aes", "a", false, "Encrypt with aes algorithm.") + cmd.Flags().BoolVarP(&EncryptOpts.SHA256, "sha256", "s", false, "Encrypt with sha256 algorithm.") + cmd.Flags().BoolVarP(&EncryptOpts.SHA512, "sha512", "S", false, "Encrypt with sha512 algorithm.") + cmd.Flags().BoolVarP(&EncryptOpts.MD5, "md5", "m", false, "Encrypt with md5 algorithm.") + cmd.Flags().StringVarP(&EncryptOpts.AESKey, "aes-key", "k", "", "Encrypt with aes key.") + + return cmd +} diff --git a/app/opts.go b/app/opts.go index 41c1f050..363f503a 100755 --- a/app/opts.go +++ b/app/opts.go @@ -26,3 +26,11 @@ var AuthOpts = options.AuthOptions{ var WhoamiOpts = options.WhoamiOptions{ ShowUser: false, } + +var EncryptOpts = options.EncryptOptions{ + AES: false, + SHA256: false, + SHA512: false, + MD5: false, + AESKey: "", +} diff --git a/cmd/secman/root.go b/cmd/secman/root.go index f702eabc..68d5624a 100755 --- a/cmd/secman/root.go +++ b/cmd/secman/root.go @@ -64,6 +64,7 @@ func Execute(f *factory.Factory, version string, buildDate string) *cobra.Comman rootCmd.AddCommand( app.AuthCMD(), app.DocsCMD, + app.EncryptCMD(), app.DoctorCMD(version), app.InitCMD, app.InfoCMD(version), diff --git a/pkg/options/opts.go b/pkg/options/opts.go index e1788521..00592b86 100755 --- a/pkg/options/opts.go +++ b/pkg/options/opts.go @@ -28,3 +28,11 @@ type RootOptions struct { type WhoamiOptions struct { ShowUser bool } + +type EncryptOptions struct { + AES bool + SHA256 bool + SHA512 bool + MD5 bool + AESKey string +} \ No newline at end of file