diff --git a/README.md b/README.md index 84d024c..3865970 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,15 @@ devd convert to_upper_case [input] # devd c uppercase aa ``` +#### Encode/Decode base64 + +```bash +devd convert encode_base64 [input] +# devd c base64 123 +devd convert decode_base64 [base64] +# devd c decode_base64 TVRJeg== +``` + ### Hashing tools ```bash diff --git a/cmd/convert/decode_base64.go b/cmd/convert/decode_base64.go new file mode 100644 index 0000000..7cd055b --- /dev/null +++ b/cmd/convert/decode_base64.go @@ -0,0 +1,25 @@ +package convert + +import ( + "encoding/base64" + "fmt" + libutils "github.com/EscanBE/go-lib/utils" + "github.com/spf13/cobra" +) + +// GetDecodeBase64CaseCmd creates a helper command that decode base64 +func GetDecodeBase64CaseCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "decode_base64 [base64]", + Short: "Decode base64", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + data, err := base64.StdEncoding.DecodeString(args[0]) + libutils.ExitIfErr(err, "failed to decode base64") + + fmt.Println(string(data)) + }, + } + + return cmd +} diff --git a/cmd/convert/encode_base64.go b/cmd/convert/encode_base64.go new file mode 100644 index 0000000..290903d --- /dev/null +++ b/cmd/convert/encode_base64.go @@ -0,0 +1,23 @@ +package convert + +import ( + "encoding/base64" + "fmt" + "github.com/spf13/cobra" + "strings" +) + +// GetEncodeBase64CaseCmd creates a helper command that encode input into base64 +func GetEncodeBase64CaseCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "encode_base64 [text]", + Aliases: []string{"base64"}, + Short: "Encode input into base64", + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(base64.StdEncoding.EncodeToString([]byte(strings.Join(args, " ")))) + }, + } + + return cmd +} diff --git a/cmd/convert/root.go b/cmd/convert/root.go index abd6edd..fd1ffc3 100644 --- a/cmd/convert/root.go +++ b/cmd/convert/root.go @@ -19,6 +19,8 @@ func Commands() *cobra.Command { GetConvertSolcSignatureCmd(), GetConvertToLowerCaseCmd(), GetConvertToUpperCaseCmd(), + GetDecodeBase64CaseCmd(), + GetEncodeBase64CaseCmd(), ) return cmd