2019-12-09 15:09:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2020-06-02 18:12:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-12-09 15:09:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var base16Cmd = &cli.Command{
|
|
|
|
Name: "base16",
|
|
|
|
Description: "standard hex",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "decode",
|
|
|
|
Value: false,
|
|
|
|
Usage: "Decode the value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
var input io.Reader
|
|
|
|
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() == 0 {
|
2019-12-09 15:09:23 +00:00
|
|
|
input = os.Stdin
|
|
|
|
} else {
|
|
|
|
input = strings.NewReader(cctx.Args().First())
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
bytes, err := io.ReadAll(input)
|
2019-12-09 15:09:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if cctx.Bool("decode") {
|
|
|
|
decoded, err := hex.DecodeString(strings.TrimSpace(string(bytes)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(decoded))
|
|
|
|
} else {
|
|
|
|
encoded := hex.EncodeToString(bytes)
|
|
|
|
fmt.Println(encoded)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|