package base58import ()var ( bn0 = big.NewInt(0) bn58 = big.NewInt(58))// Encode encodes the passed bytes into a base58 encoded string.func ( []byte) string {returnFastBase58Encoding()}// EncodeAlphabet encodes the passed bytes into a base58 encoded string with the// passed alphabet.func ( []byte, *Alphabet) string {returnFastBase58EncodingAlphabet(, )}// FastBase58Encoding encodes the passed bytes into a base58 encoded string.func ( []byte) string {returnFastBase58EncodingAlphabet(, BTCAlphabet)}// FastBase58EncodingAlphabet encodes the passed bytes into a base58 encoded// string with the passed alphabet.func ( []byte, *Alphabet) string { := .encode[0] := len()var , , , intvaruint32for < && [] == 0 { ++ } := ((-)*138/100 + 1)// allocate one big buffer up front := make([]byte, *2+)// use the second half for the temporary buffer := [+:] = - 1for = ; < ; ++ { = - 1for = uint32([]); > || != 0; -- { = + 256*uint32([]) [] = byte( % 58) /= 58 } = }for = 0; < && [] == 0; ++ { }// Use the first half for the result := [:-+]if != 0 {for = 0; < ; ++ { [] = } }for = ; < ; ++ { [] = .encode[[]] ++ }returnstring()}// TrivialBase58Encoding encodes the passed bytes into a base58 encoded string// (inefficiently).func ( []byte) string {returnTrivialBase58EncodingAlphabet(, BTCAlphabet)}// TrivialBase58EncodingAlphabet encodes the passed bytes into a base58 encoded// string (inefficiently) with the passed alphabet.func ( []byte, *Alphabet) string { := .encode[0] := len()*138/100 + 1 := make([]byte, ) := new(big.Int).SetBytes()var *big.Intfor .Cmp(bn0) != 0 { , = .DivMod(, bn58, new(big.Int)) -- [] = .encode[.Int64()] }for := range {if [] != 0 {break } -- [] = }returnstring([:])}// Decode decodes the base58 encoded bytes.func ( string) ([]byte, error) {returnFastBase58Decoding()}// DecodeAlphabet decodes the base58 encoded bytes using the given b58 alphabet.func ( string, *Alphabet) ([]byte, error) {returnFastBase58DecodingAlphabet(, )}// FastBase58Decoding decodes the base58 encoded bytes.func ( string) ([]byte, error) {returnFastBase58DecodingAlphabet(, BTCAlphabet)}// FastBase58DecodingAlphabet decodes the base58 encoded bytes using the given// b58 alphabet.func ( string, *Alphabet) ([]byte, error) {iflen() == 0 {returnnil, fmt.Errorf("zero length string") }var (uint64 , uint32int = []rune() = len() = ( + 3) / 4// check to see if we need to change this buffer size to optimize = make([]byte, (+3)*3) = % 4 = rune(.encode[0]) )if > 0 { = (0xffffffff << uint32(*8)) } else { = 4 }var = make([]uint32, )for := 0; < && [] == ; ++ { ++ }for , := range {if > 127 {returnnil, fmt.Errorf("High-bit set on invalid digit") }if .decode[] == -1 {returnnil, fmt.Errorf("Invalid base58 digit (%q)", ) } = uint32(.decode[])for := ( - 1); >= 0; -- { = uint64([])*58 + uint64() = uint32(>>32) & 0x3f [] = uint32( & 0xffffffff) }if > 0 {returnnil, fmt.Errorf("Output number too big (carry to the next int32)") }if [0]& != 0 {returnnil, fmt.Errorf("Output number too big (last int32 filled too far)") } }// the nested for-loop below is the same as the original code: // switch (bytesleft) { // case 3: // *(binu++) = (outi[0] & 0xff0000) >> 16; // //-fallthrough // case 2: // *(binu++) = (outi[0] & 0xff00) >> 8; // //-fallthrough // case 1: // *(binu++) = (outi[0] & 0xff); // ++j; // //-fallthrough // default: // break; // } // // for (; j < outisz; ++j) // { // *(binu++) = (outi[j] >> 0x18) & 0xff; // *(binu++) = (outi[j] >> 0x10) & 0xff; // *(binu++) = (outi[j] >> 8) & 0xff; // *(binu++) = (outi[j] >> 0) & 0xff; // }var , intfor , = 0, 0; < ; ++ {for := byte(-1) * 8; <= 0x18; , = -8, +1 { [] = byte([] >> ) }if == 0 { = 4// because it could be less than 4 the first time through } }for , := range {if > 0 { := - if < 0 { = 0 }return [:], nil } }return [:], nil}// TrivialBase58Decoding decodes the base58 encoded bytes (inefficiently).func ( string) ([]byte, error) {returnTrivialBase58DecodingAlphabet(, BTCAlphabet)}// TrivialBase58DecodingAlphabet decodes the base58 encoded bytes// (inefficiently) using the given b58 alphabet.func ( string, *Alphabet) ([]byte, error) { := .encode[0]varintfor := 0; < len() && [] == ; ++ { ++ } := make([]byte, )varrune = -1 := []byte() := 0for ; < len() && [] == byte(); ++ { } := new(big.Int)for := range [:] { := .decode[[]]if == -1 {returnnil, fmt.Errorf("illegal base58 data at input index: %d", ) } .Mul(, bn58) .Add(, big.NewInt(int64())) }returnappend(, .Bytes()...), nil}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.