Documentation
¶
Overview ¶
Package zbase32 implements the z-base-32 encoding as specified in http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt
Note that this is NOT RFC 4648, for that see encoding/base32. z-base-32 is a variant that aims to be more human-friendly, and in some circumstances shorter.
Bits ¶
When the amount of input is not a full number of bytes, encoding the data can lead to an unnecessary, non-information-carrying, trailing character in the encoded data. This package provides 'Bits' variants of the functions that can avoid outputting this unnecessary trailing character. For example, encoding a 20-bit message:
StdEncoding.EncodeToString([]byte{0x10, 0x11, 0x10}) == "nyety" StdEncoding.EncodeBitsToString([]byte{0x10, 0x11, 0x10}, 20) == "nyet"
Decoding such a message requires also using the 'Bits' variant function.
Example ¶
s := zbase32.StdEncoding.EncodeToString([]byte{240, 191, 199}) fmt.Println(s)
Output: 6n9hq
Index ¶
- Variables
- func NewDecoder(enc *Encoding, r io.Reader) io.Reader
- func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser
- type CorruptInputError
- type Encoding
- func (enc *Encoding) Decode(dst, src []byte) (int, error)
- func (enc *Encoding) DecodeBits(dst, src []byte, bits int) (int, error)
- func (enc *Encoding) DecodeBitsString(s string, bits int) ([]byte, error)
- func (enc *Encoding) DecodeString(s string) ([]byte, error)
- func (enc *Encoding) DecodedLen(n int) int
- func (enc *Encoding) Encode(dst, src []byte) int
- func (enc *Encoding) EncodeBits(dst, src []byte, bits int) int
- func (enc *Encoding) EncodeBitsToString(src []byte, bits int) string
- func (enc *Encoding) EncodeToString(src []byte) string
- func (enc *Encoding) EncodedLen(n int) int
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var StdEncoding = NewEncoding(encodeStd)
StdEncoding is the standard z-base-32 encoding, using an alphabet as defined in http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt.
Functions ¶
func NewDecoder ¶
NewDecoder returns a new z-base-32 stream decoder. Data read from the returned reader will be read from r and then decoded using enc.
Example ¶
input := []byte("c3zs6ydncf3y") decoder := zbase32.NewDecoder(zbase32.StdEncoding, bytes.NewReader(input)) output := make([]byte, 16) n, err := decoder.Read(output) if err != nil { fmt.Println("error:", err) return } fmt.Println(output[:n])
Output: [102 111 111 0 98 97 114]
func NewEncoder ¶
func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser
NewEncoder returns a new z-base-32 stream encoder. Data written to the returned writer will be encoded using enc and then written to r. z-Base-32 encodings operate in 5-byte blocks; when finished writing, the caller must Close the returned encoder to flush any partially written blocks.
Example ¶
input := []byte("foo\x00bar") encoder := zbase32.NewEncoder(zbase32.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: c3zs6ydncf3y
Types ¶
type CorruptInputError ¶
type CorruptInputError int64
CorruptInputError means that the byte at this offset was not a valid z-base-32 encoding byte.
func (CorruptInputError) Error ¶
func (e CorruptInputError) Error() string
type Encoding ¶
type Encoding struct {
// contains filtered or unexported fields
}
An Encoding is a radix 32 encoding/decoding scheme, defined by a 32-character alphabet.
func NewEncoding ¶
NewEncoding returns a new Encoding defined by the given alphabet, which must be a 32-byte string.
Note that this is NOT RFC 4648, for that see encoding/base32. z-base-32 is a variant that aims to be more human-friendly, and in some circumstances shorter.
func (*Encoding) Decode ¶
Decode decodes src using the encoding enc. It writes at most DecodedLen(len(src)) bytes to dst and returns the number of bytes written. If src contains invalid z-base-32 data, it will return the number of bytes successfully written and CorruptInputError.
func (*Encoding) DecodeBits ¶
DecodeBits decodes the specified number of bits of z-base-32 encoded data from src. It writes at most DecodedLen(len(src)) bytes to dst and returns the number of bytes written.
If src contains invalid z-base-32 data, it will return the number of bytes successfully written and CorruptInputError.
func (*Encoding) DecodeBitsString ¶
DecodeBitsString returns the bytes represented by the z-base-32 string s containing the specified number of bits.
func (*Encoding) DecodeString ¶
DecodeString returns the z-base-32 decoded bytes of string s.
Example ¶
str := "qpzs43jyctozeajyq7wze4byyyogn5urrdz5zxa" data, err := zbase32.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) DecodedLen ¶
DecodedLen returns the maximum length in bytes of the decoded data corresponding to n bytes of z-base-32-encoded data.
func (*Encoding) Encode ¶
Encode encodes src using the encoding enc, writing EncodedLen(len(src)) bytes to dst.
The encoding is not appropriate for use on individual blocks of a large data stream. Use NewEncoder() instead.
func (*Encoding) EncodeBits ¶
EncodeBits encodes the specified number of bits of src. It writes at most EncodedLen(len(src)) bytes to dst and returns the number of bytes written.
EncodeBits is not appropriate for use on individual blocks of a large data stream.
func (*Encoding) EncodeBitsToString ¶
EncodeBitsToString returns the z-base-32 encoding of the specified number of bits of src.
func (*Encoding) EncodeToString ¶
EncodeToString returns the z-base-32 encoding of src.
Example ¶
data := []byte("any + old & data") str := zbase32.StdEncoding.EncodeToString(data) fmt.Println(str)
Output: cfz81ebmrbzsa3byraogeamwcr
func (*Encoding) EncodedLen ¶
EncodedLen returns the length in bytes of the z-base-32 encoding of an input buffer of length n.