# # Nim's Unofficial Library # (c) Copyright 2015 Huy Doan # # See the file "LICENSE", included in this # distribution, for details about the copyright. # ## This module implements a base32 encoder and decoder. const VERSION* = "0.1.2" base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=" proc encode*(s: openArray[char]; pad=true): string = var i, j, idx, digit: int = 0 var current, next: int var len = (s.len * 8 / 5).int if len mod 8 != 0: len += 8 - (len mod 8) result = newString(len) while i < s.len: current = s[i].ord if idx > 3: if i + 1 < s.len: next = s[i+1].ord else: next = 0 digit = current and (0xFF shr idx) idx = (idx + 5) mod 8 digit = digit shl idx digit = digit or (next shr (8 - idx)) i += 1 else: digit = (current shr (8 - (idx + 5))) and 0x1F idx = (idx + 5) mod 8 if idx == 0: i += 1 result[j] = base32Chars[digit] j += 1 if pad: for i in j..= 8: bits -= 8 result[idx] = char(buf shr bits and 0xFF) idx += 1 if idx < len: setLen(result, idx)