13 "github.com/mr-tron/base58"
16 func checkSum(b []byte) []byte {
17 sh1, sh2 := sha256.New(), sha256.New()
19 sh2.Write(sh1.Sum(nil))
28 help = flag.Bool("h", false, "display this message")
29 lnBreak = flag.Int("b", 76, "break encoded string into num character lines. Use 0 to disable line wrapping")
30 input = flag.String("i", "-", `input file (use: "-" for stdin)`)
31 output = flag.String("o", "-", `output file (use: "-" for stdout)`)
32 decode = flag.Bool("d", false, `decode input`)
33 check = flag.Bool("k", false, `use sha256 check`)
34 useError = flag.Bool("e", false, `write error to stderr`)
44 fin, fout := os.Stdin, os.Stdout
46 if fin, err = os.Open(*input); err != nil {
47 fmt.Fprintln(os.Stderr, "input file err:", err)
53 if fout, err = os.Create(*output); err != nil {
54 fmt.Fprintln(os.Stderr, "output file err:", err)
59 if bin, err = ioutil.ReadAll(fin); err != nil {
60 fmt.Fprintln(os.Stderr, "read input err:", err)
65 decoded, err := base58.Decode(string(bin))
67 fmt.Fprintln(os.Stderr, "decode input err:", err)
73 chk := len(decoded) - 4
74 decodedCk := decoded[chk:]
75 decoded = decoded[:chk]
76 sum := checkSum(decoded)
77 checkResult = hex.EncodeToString(sum[:4]) == hex.EncodeToString(decodedCk)
80 _, err = io.Copy(fout, bytes.NewReader(decoded))
82 fmt.Fprintln(os.Stderr, err)
86 if *check && !checkResult {
88 fmt.Fprintf(os.Stderr, "%t\n", false)
98 bin = append(bin, sum[:4]...)
101 encoded := base58.Encode(bin)
104 lines := (len(encoded) / *lnBreak) + 1
105 for i := 0; i < lines; i++ {
106 start := i * *lnBreak
107 end := start + *lnBreak
109 fmt.Fprintln(fout, encoded[start:])
112 fmt.Fprintln(fout, encoded[start:end])
115 fmt.Fprintln(fout, encoded)