zbase32

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2017 License: MIT Imports: 4 Imported by: 9

README

z-base-32 - human-oriented base-32 encoding

Build Status Test Coverage Documentation

Golang pacakge which implements base32 encoding of binary data according to http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt.

Note: this is NOT RFC4648 or RFC3548. If you need to be compatible to one of those RFCs, use encoding/base32.

This package:

  • follows the example of encoding/base32;
  • supports encoding and decoding of byte arrays;
  • supports encoding and decoding using io.Writer and io.Reader interfaces.
  • provides a shell command which behaves similar to the BSD base64 command.

Based on the work from github.com/tv42/zbase32 by Tommi Virtanen.

Command line utilities

Included is a simple command-line utility for encoding/decoding data.

Example:

$ echo "Hello world" | zbase32
jb1sa5dxrb5s6huccofy
$ echo -n jb1sa5dxrb5s6huccofy | zbase32 --decode 
Hello world
$ printf '\x01binary!!!1\x00' | zbase32
yftg15ubqjh1nejbgryy
$ echo -n yftg15ubqjh1nejbgryy | zbase32 --decode | hexdump -C
00000000  01 62 69 6e 61 72 79 21  21 21 31 00              |.binary!!!1.|
0000000c

Contributing and license

This library is licences under MIT. For information about how to contribute, see CONTRIBUTING

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

Examples

Constants

This section is empty.

Variables

View Source
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

func NewDecoder(enc *Encoding, r io.Reader) io.Reader

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

func NewEncoding(encoder string) *Encoding

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

func (enc *Encoding) Decode(dst, src []byte) (int, error)

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

func (enc *Encoding) DecodeBits(dst, src []byte, bits int) (int, error)

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

func (enc *Encoding) DecodeBitsString(s string, bits int) ([]byte, error)

DecodeBitsString returns the bytes represented by the z-base-32 string s containing the specified number of bits.

func (*Encoding) DecodeString

func (enc *Encoding) DecodeString(s string) ([]byte, error)

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

func (enc *Encoding) DecodedLen(n int) int

DecodedLen returns the maximum length in bytes of the decoded data corresponding to n bytes of z-base-32-encoded data.

func (*Encoding) Encode

func (enc *Encoding) Encode(dst, src []byte) int

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

func (enc *Encoding) EncodeBits(dst, src []byte, bits int) int

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

func (enc *Encoding) EncodeBitsToString(src []byte, bits int) string

EncodeBitsToString returns the z-base-32 encoding of the specified number of bits of src.

func (*Encoding) EncodeToString

func (enc *Encoding) EncodeToString(src []byte) string

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

func (enc *Encoding) EncodedLen(n int) int

EncodedLen returns the length in bytes of the z-base-32 encoding of an input buffer of length n.

type Value

type Value []byte

Value implements flag parsing for zbase32 values.

func (*Value) Get

func (v *Value) Get() interface{}

Get the data stored in the value. Returns a value of type []byte.

func (*Value) Set

func (v *Value) Set(s string) error

Set the value to data encoded by string.

func (*Value) String

func (v *Value) String() string

String returns the z-base-32 encoding of the value.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL