Skip to content

Commit

Permalink
encode/decode base64
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbcdev committed Apr 10, 2024
1 parent 5a75341 commit 1d33ce3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions cmd/convert/decode_base64.go
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 23 additions & 0 deletions cmd/convert/encode_base64.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions cmd/convert/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func Commands() *cobra.Command {
GetConvertSolcSignatureCmd(),
GetConvertToLowerCaseCmd(),
GetConvertToUpperCaseCmd(),
GetDecodeBase64CaseCmd(),
GetEncodeBase64CaseCmd(),
)

return cmd
Expand Down

0 comments on commit 1d33ce3

Please sign in to comment.