A Go library for Z85 encoding and decoding, implementing the ZeroMQ Base-85 encoding.
Z85 encodes binary data into printable ASCII characters using an 85-character alphabet. Every 4 bytes of input produce 5 bytes of output, resulting in a 25% size increase (compared to 33% for Base64).
go get github.com/tortxof/z85package main
import (
"fmt"
"github.com/tortxof/z85"
)
func main() {
// Encode binary data
data := []byte{0x86, 0x4F, 0xD2, 0x6F, 0xB5, 0x59, 0xF7, 0x5B}
encoded := z85.Encode(data)
fmt.Println(string(encoded)) // HelloWorld
// Decode Z85 string
decoded := z85.Decode([]byte("HelloWorld"))
fmt.Printf("%x\n", decoded) // 864fd26fb559f75b
}Encode(data []byte) []byte- Encode a byte slice to Z85Decode(data []byte) []byte- Decode a Z85 byte slice to binaryEncodeChunk(chunk [4]byte) [5]byte- Encode a single 4-byte chunkDecodeChunk(chunk [5]byte) [4]byte- Decode a single 5-byte chunk
Partial chunks are handled automatically with internal padding.
Note: The decode functions do not validate input. Bytes outside the Z85
alphabet are silently treated as the character '0' (value 0). Ensure input
contains only valid Z85 characters if correctness is critical.
NewEncoder(w io.Writer) io.WriteCloser- Create a streaming encoderNewDecoder(w io.Writer) io.WriteCloser- Create a streaming decoder
The streaming types implement io.WriteCloser. Data written to the
encoder/decoder is processed and written to the underlying writer. Call
Close() to flush any buffered partial chunks.
// Streaming encode
var buf bytes.Buffer
encoder := z85.NewEncoder(&buf)
encoder.Write([]byte{0x86, 0x4F, 0xD2, 0x6F})
encoder.Write([]byte{0xB5, 0x59, 0xF7, 0x5B})
encoder.Close()
fmt.Println(buf.String()) // HelloWorld
// Streaming decode
var out bytes.Buffer
decoder := z85.NewDecoder(&out)
io.Copy(decoder, strings.NewReader("HelloWorld"))
decoder.Close()
fmt.Printf("%x\n", out.Bytes()) // 864fd26fb559f75bBuild the command-line tools:
go build ./cmd/z85encode
go build ./cmd/z85decodeUsage:
# Encode
echo -n "test" | ./z85encode
# Output: By/Jn
# Decode
echo -n "By/Jn" | ./z85decode
# Output: test
# Encode a file
./z85encode < input.bin > output.z85
# Decode a file
./z85decode < input.z85 > output.bin# Run tests
go test ./...
# Run benchmarks
go test -bench=.
# Run fuzz tests
go test -fuzz=FuzzZ85RoundTrip
go test -fuzz=FuzzStreamRoundTripSee LICENSE for details.