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:
1. agnoster/base32-js. This is for nodejs, and its main base32.encode function takes input as a string.
2. base32-encoding-in-javascript. This takes input as a string, too. Moreover, this lacks for decoder.
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 wikipedia, output will be 20% oversized - that's what I expect.
Given the alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567":
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 --> C
01010 --> K
10101 --> V
10011 --> T
01000 --> I
00100 --> E
10101 --> V
01011 --> L
//+40 bits
00110 --> G
10000 --> Q
01001 --> J
01010 --> K
10110 --> W
01101 --> N
00000 --> A
10010 --> S
//+16 bits
10101 --> V //RFC 3548 chapter 5 case 3:
01100 --> M //the final quantum of encoding input is exactly 16 bits;
11010 --> 2 //here, the final unit of encoded output will be four characters
0 --> //followed by four "=" padding characters
//zero bits are added (on the right) to form an integral number of 5-bit groups
-->
00000 --> A
--> base32data: CKVTIEVLGQJKWNASVM2A====
I'd like to see javascript hextobase32("12AB3412AB3412AB3412AB34")
yielding CKVTIEVLGQJKWNASVM2A==== and base32tohex("CKVTIEVLGQJKWNASVM2A====")
returning 12AB3412AB3412AB3412AB34.
UPDATE
In addition to agnoster/base32-js, which doesn't seem to handle padding problems, I met the following libs:
1. Nibbler. According to wikipedia, there are two ways to encode: 8-bit and 7-bit. This lib even has an option dataBits
(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!
2. Chris Umbel thirty-two.js. This lib decided to carry the whole byte table with it for decoding. And you can see this interesting comment in the source code:
/* byte by byte isn't as pretty as quintet by quintet but tests a bit
faster. will have to revisit. */
But not evolving.
3. jsliquid.Data. Operates on so-called binary large objects. Seems to get the job done, but since its code is heavily obfuscated, I can't even see how to define my custom alphabet.
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.
Well the first node.js takes input in binary string, what you want is for it take input in base-16 or base-64. Since you already have nice base-64 implementations and base16 decoder is dead simple to do, I think you're set.
https://github.com/agnoster/base32-js/blob/master/lib/base32.js Also works for browsers out of the box.
So you'd use it like this in browser:
var result = base32.encode(base64decode(base64input));
var result2 = base32.encode(base16decode(base16input));
var result3 = base32.encode(binaryInput);
Where base16decode
:
function base16decode( str ) {
return str.replace( /([A-fa-f0-9]{2})/g, function( m, g1 ) {
return String.fromCharCode( parseInt( g1, 16 ));
});
}