-
Notifications
You must be signed in to change notification settings - Fork 4
/
hex2base64.go
54 lines (46 loc) · 860 Bytes
/
hex2base64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"encoding/base64"
"encoding/hex"
"flag"
"fmt"
"log"
"os"
)
func main() {
log.SetFlags(0)
log.SetPrefix("hex2base64: ")
bflag := flag.Bool("b", false, "base64 to hex")
flag.Usage = usage
flag.Parse()
if flag.NArg() < 1 {
usage()
}
for _, str := range flag.Args() {
if !*bflag {
fmt.Printf("%s\n", hex2base64(str))
} else {
fmt.Printf("%s\n", base642hex(str))
}
}
}
func ck(err error) {
if err != nil {
log.Fatal(err)
}
}
func hex2base64(str string) string {
buf, err := hex.DecodeString(str)
ck(err)
return base64.StdEncoding.EncodeToString(buf)
}
func base642hex(str string) string {
buf, err := base64.StdEncoding.DecodeString(str)
ck(err)
return hex.EncodeToString(buf)
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: hex2base64 [options] value ...")
flag.PrintDefaults()
os.Exit(2)
}