title: Encode HEXADECIMAL (or base64) input to base32 output, decode base32 input to hex/base64 - looking for Javascript solution
body: <p>I've encountered a nice base64 encoding implementations in JS, its typical job is to take a utf8-encoded text input and give a base64 output (and vice versa). But I'm surprised I've never seen a suitable solution for base32! Well, that's all I've found:<br>
  1. <a href="https://github.com/agnoster/base32-js" rel="nofollow">agnoster/base32-js</a>. This is for nodejs, and its main base32.encode function takes input as a string.<br>
  2. <a href="http://forthescience.org/blog/2010/11/30/base32-encoding-in-javascript/" rel="nofollow">base32-encoding-in-javascript</a>. This takes input as a string, too. Moreover, this lacks for decoder.<br>
  But I need the script to take the input as HEX (or even base64)!!! If my input is Hex, then output will be shortened; if my input is base64, then, according to <a href="http://en.wikipedia.org/wiki/Base32#Disadvantages" rel="nofollow">wikipedia</a>, output will be 20% oversized - that's what I expect.<br>
  Given the alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567":  </p>
  
  <pre><code>hexdata: 12AB3412AB3412AB3412AB34;
  
  //RFC 3548 chapter 5: The encoding process represents 40-bit groups of input bits 
  //as output strings of 8 encoded characters. 
  
   bin     b32
  00010 --&gt; C
  01010 --&gt; K
  10101 --&gt; V
  10011 --&gt; T
  01000 --&gt; I
  00100 --&gt; E
  10101 --&gt; V
  01011 --&gt; L
  //+40 bits
  00110 --&gt; G
  10000 --&gt; Q
  01001 --&gt; J
  01010 --&gt; K
  10110 --&gt; W
  01101 --&gt; N
  00000 --&gt; A
  10010 --&gt; S
  //+16 bits
  10101 --&gt; V  //RFC 3548 chapter 5 case 3:
  01100 --&gt; M  //the final quantum of encoding input is exactly 16 bits; 
  11010 --&gt; 2  //here, the final unit of encoded output will be four characters 
  0     --&gt;    //followed by four "=" padding characters
  //zero bits are added (on the right) to form an integral number of 5-bit groups
  --&gt; 
  00000 --&gt; A
  --&gt; base32data: CKVTIEVLGQJKWNASVM2A====  
  </code></pre>
  
  <p>I'd like to see javascript <code>hextobase32("12AB3412AB3412AB3412AB34")</code> yielding <strong>CKVTIEVLGQJKWNASVM2A====</strong> and <code>base32tohex("CKVTIEVLGQJKWNASVM2A====")</code> returning <strong>12AB3412AB3412AB3412AB34</strong>.  </p>
  
  <p><strong>UPDATE</strong><br>
  In addition to <a href="https://github.com/agnoster/base32-js" rel="nofollow">agnoster/base32-js</a>, which doesn't seem to handle padding problems, I met the following libs:<br>
  1. <a href="http://www.tumuski.com/2010/04/nibbler/" rel="nofollow">Nibbler</a>. According to <a href="http://en.wikipedia.org/wiki/Base32#Disadvantages" rel="nofollow">wikipedia</a>, there are two ways to encode: 8-bit and 7-bit. This lib even has an option <code>dataBits</code> (maybe it's meant only for base64, not for base32, I don't know) to choose 8-bit or 7-bit way! But this project is not evolving at all. And one more thing: reading comments, I see that this lib also has padding issues!<br>
  2. <a href="https://github.com/chrisumbel/thirty-two/blob/master/lib/thirty-two/thirty-two.js" rel="nofollow">Chris Umbel thirty-two.js</a>. This lib decided to carry the whole byte table with it for decoding. And you can see this interesting comment in the source code:<br>
      /* byte by byte isn't as pretty as quintet by quintet but tests a bit
      faster. will have to revisit. */<br>
  But not evolving.<br>
  3. <a href="http://www.jsliquid.com/docs/data/jsl_data.html" rel="nofollow">jsliquid.Data</a>. Operates on so-called <a href="http://www.jsliquid.com/tutorials/blob_objects.html" rel="nofollow">binary large objects</a>. Seems to get the job done, but since its code is heavily obfuscated, I can't even see how to define my custom alphabet.<br>
  And now, I think that a fully functional Javascript UTF8/hex/base32/base64 library of a reliable quality would be great, but currently, situation is dubious.</p>