package main import ( "crypto/sha256" "fmt" "os" "os/signal" "time" ) const b58alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" func b58encode(b []byte) string { zeroes := 0 for zeroes < len(b) && b[zeroes] == 0 { zeroes++ } var x [64]byte buf := x[:0] for _, v := range b { carry := int(v) for i := len(buf) - 1; i >= 0; i-- { carry += int(buf[i]) * 256 buf[i] = byte(carry % 58) carry /= 58 } for carry > 0 { buf = append([]byte{byte(carry % 58)}, buf...) carry /= 58 } } out := make([]byte, zeroes) for _, v := range buf { out = append(out, b58alphabet[int(v)]) } for i := 0; i < zeroes; i++ { out[i] = '1' } return string(out) } func b58checkEncode(version byte, payload []byte) string { b := make([]byte, 1+len(payload)+4) b[0] = version copy(b[1:], payload) h := sha256.Sum256(b[:1+len(payload)]) h2 := sha256.Sum256(h[:]) copy(b[1+len(payload):], h2[:4]) return b58encode(b) } func main() { h := sha256.Sum256([]byte(fmt.Sprint(time.Now().UnixNano()))) done := make(chan os.Signal, 1) signal.Notify(done, os.Interrupt) go func() { for { select { case <-done: return default: h = sha256.Sum256(h[:]) } } }() <-done fmt.Println("\n\nhex:", fmt.Sprintf("%x", h[:])) wif := b58checkEncode(0x80, h[:]) fmt.Println("WIF (uncompressed):", wif) compressedPayload := make([]byte, 33) copy(compressedPayload, h[:]) compressedPayload[32] = 0x01 wifCompressed := b58checkEncode(0x80, compressedPayload) fmt.Println("WIF (compressed): ", wifCompressed) }