| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/sha256" |
| 6 |
"encoding/hex" |
| 7 |
"flag" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"io/ioutil" |
| 11 |
"os" |
| 12 |
|
| 13 |
"github.com/mr-tron/base58" |
| 14 |
) |
| 15 |
|
| 16 |
func checkSum(b []byte) []byte { |
| 17 |
sh1, sh2 := sha256.New(), sha256.New() |
| 18 |
sh1.Write(b) |
| 19 |
sh2.Write(sh1.Sum(nil)) |
| 20 |
return sh2.Sum(nil) |
| 21 |
} |
| 22 |
|
| 23 |
func main() { |
| 24 |
var ( |
| 25 |
err error |
| 26 |
bin []byte |
| 27 |
|
| 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`) |
| 35 |
) |
| 36 |
|
| 37 |
flag.Parse() |
| 38 |
|
| 39 |
if *help { |
| 40 |
flag.Usage() |
| 41 |
os.Exit(0) |
| 42 |
} |
| 43 |
|
| 44 |
fin, fout := os.Stdin, os.Stdout |
| 45 |
if *input != "-" { |
| 46 |
if fin, err = os.Open(*input); err != nil { |
| 47 |
fmt.Fprintln(os.Stderr, "input file err:", err) |
| 48 |
os.Exit(1) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
if *output != "-" { |
| 53 |
if fout, err = os.Create(*output); err != nil { |
| 54 |
fmt.Fprintln(os.Stderr, "output file err:", err) |
| 55 |
os.Exit(1) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if bin, err = ioutil.ReadAll(fin); err != nil { |
| 60 |
fmt.Fprintln(os.Stderr, "read input err:", err) |
| 61 |
os.Exit(1) |
| 62 |
} |
| 63 |
|
| 64 |
if *decode { |
| 65 |
decoded, err := base58.Decode(string(bin)) |
| 66 |
if err != nil { |
| 67 |
fmt.Fprintln(os.Stderr, "decode input err:", err) |
| 68 |
os.Exit(1) |
| 69 |
} |
| 70 |
|
| 71 |
var checkResult bool |
| 72 |
if *check { |
| 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) |
| 78 |
} |
| 79 |
|
| 80 |
_, err = io.Copy(fout, bytes.NewReader(decoded)) |
| 81 |
if err != nil { |
| 82 |
fmt.Fprintln(os.Stderr, err) |
| 83 |
os.Exit(1) |
| 84 |
} |
| 85 |
|
| 86 |
if *check && !checkResult { |
| 87 |
if *useError { |
| 88 |
fmt.Fprintf(os.Stderr, "%t\n", false) |
| 89 |
} |
| 90 |
os.Exit(3) |
| 91 |
} |
| 92 |
|
| 93 |
os.Exit(0) |
| 94 |
} |
| 95 |
|
| 96 |
if *check { |
| 97 |
sum := checkSum(bin) |
| 98 |
bin = append(bin, sum[:4]...) |
| 99 |
} |
| 100 |
|
| 101 |
encoded := base58.Encode(bin) |
| 102 |
|
| 103 |
if *lnBreak > 0 { |
| 104 |
lines := (len(encoded) / *lnBreak) + 1 |
| 105 |
for i := 0; i < lines; i++ { |
| 106 |
start := i * *lnBreak |
| 107 |
end := start + *lnBreak |
| 108 |
if i == lines-1 { |
| 109 |
fmt.Fprintln(fout, encoded[start:]) |
| 110 |
return |
| 111 |
} |
| 112 |
fmt.Fprintln(fout, encoded[start:end]) |
| 113 |
} |
| 114 |
} |
| 115 |
fmt.Fprintln(fout, encoded) |
| 116 |
} |