Skip to content

Commit 1d33ce3

Browse files
committed
encode/decode base64
1 parent 5a75341 commit 1d33ce3

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ devd convert to_upper_case [input]
110110
# devd c uppercase aa
111111
```
112112

113+
#### Encode/Decode base64
114+
115+
```bash
116+
devd convert encode_base64 [input]
117+
# devd c base64 123
118+
devd convert decode_base64 [base64]
119+
# devd c decode_base64 TVRJeg==
120+
```
121+
113122
### Hashing tools
114123

115124
```bash

cmd/convert/decode_base64.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package convert
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
libutils "github.com/EscanBE/go-lib/utils"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
// GetDecodeBase64CaseCmd creates a helper command that decode base64
11+
func GetDecodeBase64CaseCmd() *cobra.Command {
12+
cmd := &cobra.Command{
13+
Use: "decode_base64 [base64]",
14+
Short: "Decode base64",
15+
Args: cobra.ExactArgs(1),
16+
Run: func(cmd *cobra.Command, args []string) {
17+
data, err := base64.StdEncoding.DecodeString(args[0])
18+
libutils.ExitIfErr(err, "failed to decode base64")
19+
20+
fmt.Println(string(data))
21+
},
22+
}
23+
24+
return cmd
25+
}

cmd/convert/encode_base64.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package convert
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"github.com/spf13/cobra"
7+
"strings"
8+
)
9+
10+
// GetEncodeBase64CaseCmd creates a helper command that encode input into base64
11+
func GetEncodeBase64CaseCmd() *cobra.Command {
12+
cmd := &cobra.Command{
13+
Use: "encode_base64 [text]",
14+
Aliases: []string{"base64"},
15+
Short: "Encode input into base64",
16+
Args: cobra.MinimumNArgs(1),
17+
Run: func(cmd *cobra.Command, args []string) {
18+
fmt.Println(base64.StdEncoding.EncodeToString([]byte(strings.Join(args, " "))))
19+
},
20+
}
21+
22+
return cmd
23+
}

cmd/convert/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func Commands() *cobra.Command {
1919
GetConvertSolcSignatureCmd(),
2020
GetConvertToLowerCaseCmd(),
2121
GetConvertToUpperCaseCmd(),
22+
GetDecodeBase64CaseCmd(),
23+
GetEncodeBase64CaseCmd(),
2224
)
2325

2426
return cmd

0 commit comments

Comments
 (0)