main.go raw
1 package main
2
3 import (
4 "crypto/sha256"
5 "fmt"
6 "os"
7 "os/signal"
8 "time"
9 )
10
11 const b58alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
12
13 func b58encode(b []byte) string {
14 zeroes := 0
15 for zeroes < len(b) && b[zeroes] == 0 {
16 zeroes++
17 }
18 var x [64]byte
19 buf := x[:0]
20 for _, v := range b {
21 carry := int(v)
22 for i := len(buf) - 1; i >= 0; i-- {
23 carry += int(buf[i]) * 256
24 buf[i] = byte(carry % 58)
25 carry /= 58
26 }
27 for carry > 0 {
28 buf = append([]byte{byte(carry % 58)}, buf...)
29 carry /= 58
30 }
31 }
32 out := make([]byte, zeroes)
33 for _, v := range buf {
34 out = append(out, b58alphabet[int(v)])
35 }
36 for i := 0; i < zeroes; i++ {
37 out[i] = '1'
38 }
39 return string(out)
40 }
41
42 func b58checkEncode(version byte, payload []byte) string {
43 b := make([]byte, 1+len(payload)+4)
44 b[0] = version
45 copy(b[1:], payload)
46 h := sha256.Sum256(b[:1+len(payload)])
47 h2 := sha256.Sum256(h[:])
48 copy(b[1+len(payload):], h2[:4])
49 return b58encode(b)
50 }
51
52 func main() {
53 h := sha256.Sum256([]byte(fmt.Sprint(time.Now().UnixNano())))
54
55 done := make(chan os.Signal, 1)
56 signal.Notify(done, os.Interrupt)
57
58 go func() {
59 for {
60 select {
61 case <-done:
62 return
63 default:
64 h = sha256.Sum256(h[:])
65 }
66 }
67 }()
68
69 <-done
70
71 fmt.Println("\n\nhex:", fmt.Sprintf("%x", h[:]))
72
73 wif := b58checkEncode(0x80, h[:])
74 fmt.Println("WIF (uncompressed):", wif)
75
76 compressedPayload := make([]byte, 33)
77 copy(compressedPayload, h[:])
78 compressedPayload[32] = 0x01
79 wifCompressed := b58checkEncode(0x80, compressedPayload)
80 fmt.Println("WIF (compressed): ", wifCompressed)
81 }
82