Debian Codegraph @

command/base64/base64.go

raw symbols SCIP
1 package base64
2
3 import (
4 "bytes"
5 "encoding/base64"
6 "fmt"
7 "os"
8 "strings"
9
10 "github.com/pkg/errors"
11 "github.com/smallstep/cli/command"
12 "github.com/smallstep/cli/utils"
13 "github.com/urfave/cli"
14 )
15
16 func init() {
17 cmd := cli.Command{
18 Name: "base64",
19 Action: command.ActionFunc(base64Action),
20 Usage: "encodes and decodes using base64 representation",
21 UsageText: `**step base64**
22 [**-d**|**--decode**] [**-r**|**--raw**] [**-u**|**--url**]`,
23 Description: `**step base64** implements base64 encoding as specified by RFC 4648.
24
25 ## Examples
26
27 Encode to base64 using the standard encoding:
28 '''
29 $ echo -n This is the string to encode | step base64
30 VGhpcyBpcyB0aGUgc3RyaW5nIHRvIGVuY29kZQ==
31 $ step base64 This is the string to encode
32 VGhpcyBpcyB0aGUgc3RyaW5nIHRvIGVuY29kZQ==
33 '''
34
35 Decode a base64 encoded string:
36 '''
37 $ echo VGhpcyBpcyB0aGUgc3RyaW5nIHRvIGVuY29kZQ== | step base64 -d
38 This is the string to encode
39 '''
40
41 Encode to base64 without padding:
42 '''
43 $ echo -n This is the string to encode | step base64 -r
44 VGhpcyBpcyB0aGUgc3RyaW5nIHRvIGVuY29kZQ
45 $ step base64 -r This is the string to encode
46 VGhpcyBpcyB0aGUgc3RyaW5nIHRvIGVuY29kZQ
47 '''
48
49 Encode to base64 using the url encoding:
50 '''
51 $ echo 'abc123$%^&*()_+-=~' | step base64 -u
52 YWJjMTIzJCVeJiooKV8rLT1-Cg==
53 '''
54
55 Decode an url encoded base64 string. The encoding type can be enforced
56 using the '-u' or '-r' flags, but it will be autodetected if they are not
57 passed:
58 '''
59 $ echo YWJjMTIzJCVeJiooKV8rLT1-Cg== | step base64 -d
60 abc123$%^&*()_+-=~
61 $ echo YWJjMTIzJCVeJiooKV8rLT1-Cg== | step base64 -d -u
62 abc123$%^&*()_+-=~
63 '''`,
64 Flags: []cli.Flag{
65 cli.BoolFlag{
66 Name: "d,decode",
67 Usage: "decode base64 input",
68 },
69 cli.BoolFlag{
70 Name: "r,raw",
71 Usage: "use the unpadded base64 encoding",
72 },
73 cli.BoolFlag{
74 Name: "u,url",
75 Usage: "use the encoding format typically used in URLs and file names",
76 },
77 },
78 }
79
80 command.Register(cmd)
81 }
82
83 func base64Action(ctx *cli.Context) error {
84 var err error
85 var data []byte
86 isDecode := ctx.Bool("decode")
87
88 if ctx.NArg() > 0 {
89 data = []byte(strings.Join(ctx.Args(), " "))
90 } else {
91 var prompt string
92 if isDecode {
93 prompt = "Please enter text to decode"
94 } else {
95 prompt = "Please enter text to encode"
96 }
97
98 if data, err = utils.ReadInput(prompt); err != nil {
99 return err
100 }
101 }
102
103 enc := getEncoder(ctx, data)
104 if isDecode {
105 b, err := enc.DecodeString(string(data))
106 if err != nil {
107 return errors.Wrap(err, "error decoding input")
108 }
109 os.Stdout.Write(b)
110 } else {
111 fmt.Println(enc.EncodeToString(data))
112 }
113
114 return nil
115 }
116
117 func getEncoder(ctx *cli.Context, data []byte) *base64.Encoding {
118 raw := ctx.Bool("raw")
119 url := ctx.Bool("url")
120 isDecode := ctx.Bool("decode")
121
122 // Detect encoding
123 if isDecode && !ctx.IsSet("raw") && !ctx.IsSet("url") {
124 raw = !bytes.HasSuffix(bytes.TrimSpace(data), []byte("="))
125 url = bytes.Contains(data, []byte("-")) || bytes.Contains(data, []byte("_"))
126 }
127
128 if raw {
129 if url {
130 return base64.RawURLEncoding
131 }
132 return base64.RawStdEncoding
133 }
134 if url {
135 return base64.URLEncoding
136 }
137
138 return base64.StdEncoding
139 }