🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# package base32 `import "encoding/base32"` base32包实现了[RFC 4648](http://tools.ietf.org/html/rfc4648)规定的base32编码。 ## Index * [Variables](#pkg-variables) * [type CorruptInputError](#CorruptInputError) * [func (e CorruptInputError) Error() string](#CorruptInputError.Error) * [type Encoding](#Encoding) * [func NewEncoding(encoder string) \*Encoding](#NewEncoding) * [func (enc \*Encoding) DecodedLen(n int) int](#Encoding.DecodedLen) * [func (enc \*Encoding) Decode(dst, src []byte) (n int, err error)](#Encoding.Decode) * [func (enc \*Encoding) DecodeString(s string) ([]byte, error)](#Encoding.DecodeString) * [func (enc \*Encoding) EncodedLen(n int) int](#Encoding.EncodedLen) * [func (enc \*Encoding) Encode(dst, src []byte)](#Encoding.Encode) * [func (enc \*Encoding) EncodeToString(src []byte) string](#Encoding.EncodeToString) * [func NewDecoder(enc \*Encoding, r io.Reader) io.Reader](#NewDecoder) * [func NewEncoder(enc \*Encoding, w io.Writer) io.WriteCloser](#NewEncoder) ### Examples * [Encoding.DecodeString](#example-Encoding-DecodeString) * [Encoding.EncodeToString](#example-Encoding-EncodeToString) * [NewEncoder](#example-NewEncoder) ## Variables ``` var HexEncoding = NewEncoding(encodeHex) ``` [RFC 4648](http://tools.ietf.org/html/rfc4648)定义的“扩展Hex字符集”,用于DNS。 ``` var StdEncoding = NewEncoding(encodeStd) ``` [RFC 4648](http://tools.ietf.org/html/rfc4648)定义的标准base32编码字符集。 ## type [CorruptInputError](https://github.com/golang/go/blob/master/src/encoding/base32/base32.go#L230 "View Source") ``` type CorruptInputError int64 ``` ### func (CorruptInputError) [Error](https://github.com/golang/go/blob/master/src/encoding/base32/base32.go#L232 "View Source") ``` func (e CorruptInputError) Error() string ``` ## type [Encoding](https://github.com/golang/go/blob/master/src/encoding/base32/base32.go#L23 "View Source") ``` type Encoding struct { // 内含隐藏或非导出字段 } ``` 双向的编码/解码协议,根据一个32字符的字符集定义,RFC 4648标准化了两种字符集。默认字符集用于SASI和GSSAPI,另一种用于DNSSEC。 ### func [NewEncoding](https://github.com/golang/go/blob/master/src/encoding/base32/base32.go#L33 "View Source") ``` func NewEncoding(encoder string) *Encoding ``` 使用给出的字符集生成一个\*Encoding,字符集必须是32字节的字符串。 ### func (\*Encoding) [DecodedLen](https://github.com/golang/go/blob/master/src/encoding/base32/base32.go#L428 "View Source") ``` func (enc *Encoding) DecodedLen(n int) int ``` 返回n字节base32编码的数据解码后的最大长度。 ### func (\*Encoding) [Decode](https://github.com/golang/go/blob/master/src/encoding/base32/base32.go#L323 "View Source") ``` func (enc *Encoding) Decode(dst, src []byte) (n int, err error) ``` 将src的数据解码后存入dst,最多写DecodedLen(len(src))字节数据到dst,并返回写入的字节数。如果src包含非法字符,将返回成功写入的字符数和CorruptInputError。换行符(\r、\n)会被忽略。 ### func (\*Encoding) [DecodeString](https://github.com/golang/go/blob/master/src/encoding/base32/base32.go#L330 "View Source") ``` func (enc *Encoding) DecodeString(s string) ([]byte, error) ``` 返回base32编码的字符串s代表的数据。 Example ``` str := "ONXW2ZJAMRQXIYJAO5UXI2BAAAQGC3TEEDX3XPY=" data, err := base32.StdEncoding.DecodeString(str) if err != nil { fmt.Println("error:", err) return } fmt.Printf("%q\n", data) ``` Output: ``` "some data with \x00 and \ufeff" ``` ### func (\*Encoding) [EncodedLen](https://github.com/golang/go/blob/master/src/encoding/base32/base32.go#L224 "View Source") ``` func (enc *Encoding) EncodedLen(n int) int ``` 返回n字节数据进行base32编码后的最大长度。 ### func (\*Encoding) [Encode](https://github.com/golang/go/blob/master/src/encoding/base32/base32.go#L70 "View Source") ``` func (enc *Encoding) Encode(dst, src []byte) ``` 将src的数据编码后存入dst,最多写EncodedLen(len(src))字节数据到dst,并返回写入的字节数。 函数会把输出设置为8的倍数,因此不建议对大数据流的独立数据块执行此方法,使用NewEncoder()代替。 ### func (\*Encoding) [EncodeToString](https://github.com/golang/go/blob/master/src/encoding/base32/base32.go#L138 "View Source") ``` func (enc *Encoding) EncodeToString(src []byte) string ``` 返回将src编码后的字符串。 Example ``` data := []byte("any + old & data") str := base32.StdEncoding.EncodeToString(data) fmt.Println(str) ``` Output: ``` MFXHSIBLEBXWYZBAEYQGIYLUME====== ``` ## func [NewDecoder](https://github.com/golang/go/blob/master/src/encoding/base32/base32.go#L422 "View Source") ``` func NewDecoder(enc *Encoding, r io.Reader) io.Reader ``` 创建一个新的base32流解码器。 ## func [NewEncoder](https://github.com/golang/go/blob/master/src/encoding/base32/base32.go#L218 "View Source") ``` func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser ``` 创建一个新的base32流编码器。写入的数据会在编码后再写入w,base32编码每5字节执行一次编码操作;写入完毕后,使用者必须调用Close方法以便将未写入的数据从缓存中刷新到w中。 Example ``` input := []byte("foo\x00bar") encoder := base32.NewEncoder(base32.StdEncoding, os.Stdout) encoder.Write(input) // Must close the encoder when finished to flush any partial blocks. // If you comment out the following line, the last partial block "r" // won't be encoded. encoder.Close() ``` Output: ``` MZXW6ADCMFZA==== ```