func GenerateNewKeypair() *Keypair { pk, _ := ecdsa.GenerateKey(elliptic.P224(), rand.Reader) b := bigJoin(KEY_SIZE, pk.PublicKey.X, pk.PublicKey.Y) public := base58.EncodeBig([]byte{}, b) private := base58.EncodeBig([]byte{}, pk.D) kp := Keypair{Public: public, Private: private} return &kp }
func BenchmarkBase58EncodeToStringSmall(b *testing.B) { data := make([]byte, 8) data[0] = 0xff // without this, it's just an inefficient zero b.SetBytes(int64(len(data))) var num big.Int num.SetBytes(data) buf := make([]byte, 12000) b.ResetTimer() for i := 0; i < b.N; i++ { base58.EncodeBig(buf[:0], &num) } }
func (session *Session) GenerateToken() { c := 48 b := make([]byte, c) _, err := rand.Read(b) if err != nil { return } token := big.NewInt(0).SetBytes(b) buf := base58.EncodeBig(nil, token) session.AuthToken = string(buf) }
func ECDSAPublicKeyToString(key ecdsa.PublicKey) string { x := key.X.Bytes() y := key.Y.Bytes() sha := util.SHA256(append(append([]byte{0x04}, x...), y...)) // should it be 0x04? ripe := ripemd160.New().Sum(sha) ripesha := util.SHA256(ripe) ripedoublesha := util.SHA256(ripesha) head := ripedoublesha[0:3] final := append(ripe, head...) i := new(big.Int) i.SetBytes(final) var b []byte return string(base58.EncodeBig(b, i)) }
// Fetch a random picture from one of Haata's keyboard sets func haata(channel string, conn *irc.Conn) { flickrUrl, err := url.Parse(flickrApiUrl) if err != nil { log.Println(err) return } v := flickrUrl.Query() v.Set("method", "flickr.collections.getTree") v.Set("api_key", config.FlickrAPIKey) // triplehaata's user_id v.Set("user_id", "57321699@N06") // Only the keyboard pics v.Set("collection_id", "57276377-72157635417889224") flickrUrl.RawQuery = v.Encode() sets, err := htmlfetch(flickrUrl.String()) if err != nil { log.Println(err) return } var setresp Setresp xml.Unmarshal(sets, &setresp) randsetindex := random(len(setresp.Sets)) randset := setresp.Sets[randsetindex].Id flickrUrl, err = url.Parse(flickrApiUrl) if err != nil { log.Println(err) return } v = flickrUrl.Query() v.Set("method", "flickr.photosets.getPhotos") v.Set("api_key", config.FlickrAPIKey) v.Set("photoset_id", randset) flickrUrl.RawQuery = v.Encode() pics, err := htmlfetch(flickrUrl.String()) if err != nil { log.Println(err) return } var photoresp Photoresp xml.Unmarshal(pics, &photoresp) randpic := random(len(photoresp.Photos)) // flickr's short url's are encoded using base58... this seems messy // Maybe use the proper long url? photostring := string(base58.EncodeBig([]byte{}, big.NewInt(photoresp.Photos[randpic].Id))) conn.Privmsg(channel, strings.TrimSpace(setresp.Sets[randsetindex].Title)+`: http://flic.kr/p/`+photostring) }
func hashString(hasher hash.Hash, hashRep string) string { str := "" if hashRep == "base58" { bigNum := new(big.Int) bigNum.SetBytes(hasher.Sum(nil)[:]) rep := base58.EncodeBig(nil, bigNum) str = string(rep) } else if hashRep == "bip39" { str, _ = bip39.NewMnemonic(hasher.Sum(nil)[:]) } else if hashRep == "proquint" { str = proquint.Encode(hasher.Sum(nil)[:]) } else { // Convert [32]byte to []byte str = hex.EncodeToString(hasher.Sum(nil)[:]) } return str }
func (k *Keypair) Sign(hash []byte) ([]byte, error) { d, err := base58.DecodeToBig(k.Private) if err != nil { return nil, err } b, _ := base58.DecodeToBig(k.Public) pub := splitBig(b, 2) x, y := pub[0], pub[1] key := ecdsa.PrivateKey{ecdsa.PublicKey{elliptic.P224(), x, y}, d} r, s, _ := ecdsa.Sign(rand.Reader, &key, hash) return base58.EncodeBig([]byte{}, bigJoin(KEY_SIZE, r, s)), nil }
// getAtomicCounter returns the base58 encoding of a counter which cycles through // the values in the range 0 to 11,316,495. This is the range that can be represented // with 4 base58 characters. The returned result will be padded with zeros such that // it is always 4 characters long. func getAtomicCounter() string { atomic.AddInt32(&counter, 1) if counter > 58*58*58*58-1 { // Reset the counter if we're beyond what we // can represent with 4 base58 characters atomic.StoreInt32(&counter, 0) } counterBytes := base58.EncodeBig(nil, big.NewInt(int64(counter))) counterStr := string(counterBytes) switch len(counterStr) { case 0: return "0000" case 1: return "000" + counterStr case 2: return "00" + counterStr case 3: return "0" + counterStr default: return counterStr[0:4] } }
// getHardwareId returns a unique identifier for the current machine. It does this // by iterating through the network interfaces of the machine and picking the first // one that has a non-empty hardware (MAC) address. Then it takes the crc32 checksum // of the MAC address and encodes it in base58 encoding. getHardwareId caches results, // so subsequent calls will return the previously calculated result. If no MAC address // could be found, the function will use "0" as the MAC address. This is not ideal, but // generateRandomId uses other means to try and avoid collisions. func getHardwareId() string { if hardwareId != "" { return hardwareId } address := "" inters, err := net.Interfaces() if err == nil { for _, inter := range inters { if inter.HardwareAddr.String() != "" { address = inter.HardwareAddr.String() break } } } if address == "" { address = "0" } check32 := crc32.ChecksumIEEE([]byte(address)) id58 := base58.EncodeBig(nil, big.NewInt(int64(check32))) hardwareId = string(id58) return hardwareId }
// getTimeString returns the current UTC unix time with second precision encoded // with base58 encoding. func getTimeString() string { timeInt := time.Now().UTC().Unix() timeBytes := base58.EncodeBig(nil, big.NewInt(timeInt)) return string(timeBytes) }
// String returns identifier as a base58-encoded byte array. func (id NodeID) String() string { var i big.Int i.SetBytes(id.Bytes()) return string(base58.EncodeBig(nil, &i)) }
func nextCode() string { var number int64 try(codeSeq.QueryRow().Scan(&number)) code := base58.EncodeBig(nil, big.NewInt(number)) return string(code) }
func (p PublicKeyDigest) String() string { var i big.Int i.SetBytes(p[:]) return string(base58.EncodeBig(nil, &i)) }
// String returns the private key as a base58-encoded byte array. func (p *PrivateKey) String() string { data, _ := msgpack.Marshal(p) return string(base58.EncodeBig(nil, big.NewInt(0).SetBytes(data))) }