1 // Package base32 implements base32 encoding as specified by RFC 4648.
15 // An Encoding is a radix 32 encoding/decoding scheme, defined by a
16 // 32-character alphabet. The most common is the "base32" encoding
17 // introduced for SASL GSSAPI and standardized in RFC 4648.
18 // The alternate "base32hex" encoding is used in DNSSEC.
19 type Encoding struct {
26 StdPadding rune = '=' // Standard padding character
27 NoPadding rune = -1 // No padding
30 const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
31 const encodeHex = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
33 // NewEncoding returns a new Encoding defined by the given alphabet,
34 // which must be a 32-byte string.
35 func NewEncoding(encoder string) *Encoding {
38 e.padChar = StdPadding
40 for i := 0; i < len(e.decodeMap); i++ {
43 for i := 0; i < len(encoder); i++ {
44 e.decodeMap[encoder[i]] = byte(i)
49 // StdEncoding is the standard base32 encoding, as defined in
51 var StdEncoding = NewEncoding(encodeStd)
53 // HexEncoding is the ``Extended Hex Alphabet'' defined in RFC 4648.
54 // It is typically used in DNS.
55 var HexEncoding = NewEncoding(encodeHex)
57 var removeNewlinesMapper = func(r rune) rune {
58 if r == '\r' || r == '\n' {
64 // WithPadding creates a new encoding identical to enc except
65 // with a specified padding character, or NoPadding to disable padding.
66 // The padding character must not be '\r' or '\n', must not
67 // be contained in the encoding's alphabet and must be a rune equal or
69 func (enc Encoding) WithPadding(padding rune) *Encoding {
70 if padding == '\r' || padding == '\n' || padding > 0xff {
71 panic("invalid padding")
74 for i := 0; i < len(enc.encode); i++ {
75 if rune(enc.encode[i]) == padding {
76 panic("padding contained in alphabet")
88 // Encode encodes src using the encoding enc, writing
89 // EncodedLen(len(src)) bytes to dst.
91 // The encoding pads the output to a multiple of 8 bytes,
92 // so Encode is not appropriate for use on individual blocks
93 // of a large data stream. Use NewEncoder() instead.
94 func (enc *Encoding) Encode(dst, src []byte) {
102 // Unpack 8x 5-bit source blocks into a 5 byte
103 // destination quantum
110 b[6] |= (src[3] << 3) & 0x1F
111 b[5] = (src[3] >> 2) & 0x1F
115 b[4] |= (src[2] << 1) & 0x1F
116 b[3] = (src[2] >> 4) & 0x1F
119 b[3] |= (src[1] << 4) & 0x1F
120 b[2] = (src[1] >> 1) & 0x1F
121 b[1] = (src[1] >> 6) & 0x1F
124 b[1] |= (src[0] << 2) & 0x1F
128 // Encode 5-bit blocks using the base32 alphabet
131 // Common case, unrolled for extra performance
132 dst[0] = enc.encode[b[0]]
133 dst[1] = enc.encode[b[1]]
134 dst[2] = enc.encode[b[2]]
135 dst[3] = enc.encode[b[3]]
136 dst[4] = enc.encode[b[4]]
137 dst[5] = enc.encode[b[5]]
138 dst[6] = enc.encode[b[6]]
139 dst[7] = enc.encode[b[7]]
141 for i := 0; i < size; i++ {
142 dst[i] = enc.encode[b[i]]
146 // Pad the final quantum
148 if enc.padChar == NoPadding {
152 dst[7] = byte(enc.padChar)
154 dst[6] = byte(enc.padChar)
155 dst[5] = byte(enc.padChar)
157 dst[4] = byte(enc.padChar)
159 dst[3] = byte(enc.padChar)
160 dst[2] = byte(enc.padChar)
173 // EncodeToString returns the base32 encoding of src.
174 func (enc *Encoding) EncodeToString(src []byte) string {
175 buf := make([]byte, enc.EncodedLen(len(src)))
180 type encoder struct {
184 buf [5]byte // buffered data waiting to be encoded
185 nbuf int // number of bytes in buf
186 out [1024]byte // output buffer
189 func (e *encoder) Write(p []byte) (n int, err error) {
197 for i = 0; i < len(p) && e.nbuf < 5; i++ {
206 e.enc.Encode(e.out[0:], e.buf[0:])
207 if _, e.err = e.w.Write(e.out[0:8]); e.err != nil {
213 // Large interior chunks.
215 nn := len(e.out) / 8 * 5
220 e.enc.Encode(e.out[0:], p[0:nn])
221 if _, e.err = e.w.Write(e.out[0 : nn/5*8]); e.err != nil {
229 for i := 0; i < len(p); i++ {
237 // Close flushes any pending output from the encoder.
238 // It is an error to call Write after calling Close.
239 func (e *encoder) Close() error {
240 // If there's anything left in the buffer, flush it out
241 if e.err == nil && e.nbuf > 0 {
242 e.enc.Encode(e.out[0:], e.buf[0:e.nbuf])
244 _, e.err = e.w.Write(e.out[0:8])
249 // NewEncoder returns a new base32 stream encoder. Data written to
250 // the returned writer will be encoded using enc and then written to w.
251 // Base32 encodings operate in 5-byte blocks; when finished
252 // writing, the caller must Close the returned encoder to flush any
253 // partially written blocks.
254 func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser {
255 return &encoder{enc: enc, w: w}
258 // EncodedLen returns the length in bytes of the base32 encoding
259 // of an input buffer of length n.
260 func (enc *Encoding) EncodedLen(n int) int {
261 if enc.padChar == NoPadding {
264 return (n + 4) / 5 * 8
271 type CorruptInputError int64
273 func (e CorruptInputError) Error() string {
274 return "illegal base32 data at input byte " + strconv.FormatInt(int64(e), 10)
277 // decode is like Decode but returns an additional 'end' value, which
278 // indicates if end-of-message padding was encountered and thus any
279 // additional data is an error. This method assumes that src has been
280 // stripped of all supported whitespace ('\r' and '\n').
281 func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
283 for len(src) > 0 && !end {
284 // Decode quantum using the base32 alphabet
290 // We have reached the end and are missing padding
291 if len(src) == 0 && enc.padChar != NoPadding {
292 return n, false, CorruptInputError(olen - len(src) - j)
295 // We have reached the end and are not expecing any padding
296 if len(src) == 0 && enc.padChar == NoPadding {
303 if in == byte(enc.padChar) && j >= 2 && len(src) < 8 {
304 // We've reached the end and there's padding
305 if len(src)+j < 8-1 {
306 // not enough padding
307 return n, false, CorruptInputError(olen)
309 for k := 0; k < 8-1-j; k++ {
310 if len(src) > k && src[k] != byte(enc.padChar) {
312 return n, false, CorruptInputError(olen - len(src) + k - 1)
316 // 7, 5 and 2 are not valid padding lengths, and so 1, 3 and 6 are not
317 // valid dlen values. See RFC 4648 Section 6 "Base 32 Encoding" listing
318 // the five valid padding lengths, and Section 9 "Illustrations and
319 // Examples" for an illustration for how the 1st, 3rd and 6th base32
320 // src bytes do not yield enough information to decode a dst byte.
321 if dlen == 1 || dlen == 3 || dlen == 6 {
322 return n, false, CorruptInputError(olen - len(src) - 1)
326 dbuf[j] = enc.decodeMap[in]
328 return n, false, CorruptInputError(olen - len(src) - 1)
333 // Pack 8x 5-bit source blocks into 5 byte destination
337 dst[4] = dbuf[6]<<5 | dbuf[7]
340 dst[3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3
343 dst[2] = dbuf[3]<<4 | dbuf[4]>>1
346 dst[1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4
349 dst[0] = dbuf[0]<<3 | dbuf[1]>>2
372 // Decode decodes src using the encoding enc. It writes at most
373 // DecodedLen(len(src)) bytes to dst and returns the number of bytes
374 // written. If src contains invalid base32 data, it will return the
375 // number of bytes successfully written and CorruptInputError.
376 // New line characters (\r and \n) are ignored.
377 func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
378 src = bytes.Map(removeNewlinesMapper, src)
379 n, _, err = enc.decode(dst, src)
383 // DecodeString returns the bytes represented by the base32 string s.
384 func (enc *Encoding) DecodeString(s string) ([]byte, error) {
385 s = strings.Map(removeNewlinesMapper, s)
386 dbuf := make([]byte, enc.DecodedLen(len(s)))
387 n, _, err := enc.decode(dbuf, []byte(s))
391 type decoder struct {
395 end bool // saw end of message
396 buf [1024]byte // leftover input
398 out []byte // leftover decoded output
399 outbuf [1024 / 8 * 5]byte
402 func readEncodedData(r io.Reader, buf []byte, min int) (n int, err error) {
403 for n < min && err == nil {
405 nn, err = r.Read(buf[n:])
408 if n < min && n > 0 && err == io.EOF {
409 err = io.ErrUnexpectedEOF
414 func (d *decoder) Read(p []byte) (n int, err error) {
415 // Use leftover decoded output from last read.
438 nn, d.err = readEncodedData(d.r, d.buf[d.nbuf:nn], 8-d.nbuf)
444 // Decode chunk into p, or d.out and then p if p is too small.
448 nw, d.end, err = d.enc.decode(d.outbuf[0:], d.buf[0:nr])
449 d.out = d.outbuf[0:nw]
453 n, d.end, err = d.enc.decode(p, d.buf[0:nr])
456 for i := 0; i < d.nbuf; i++ {
457 d.buf[i] = d.buf[i+nr]
460 if err != nil && (d.err == nil || d.err == io.EOF) {
465 // We cannot return all the decoded bytes to the caller in this
466 // invocation of Read, so we return a nil error to ensure that Read
467 // will be called again. The error stored in d.err, if any, will be
468 // returned with the last set of decoded bytes.
475 type newlineFilteringReader struct {
479 func (r *newlineFilteringReader) Read(p []byte) (int, error) {
480 n, err := r.wrapped.Read(p)
483 for i, b := range p[0:n] {
484 if b != '\r' && b != '\n' {
491 if err != nil || offset > 0 {
494 // Previous buffer entirely whitespace, read again
495 n, err = r.wrapped.Read(p)
500 // NewDecoder constructs a new base32 stream decoder.
501 func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
502 return &decoder{enc: enc, r: &newlineFilteringReader{r}}
505 // DecodedLen returns the maximum length in bytes of the decoded data
506 // corresponding to n bytes of base32-encoded data.
507 func (enc *Encoding) DecodedLen(n int) int {
508 if enc.padChar == NoPadding {