Skip to content

Instantly share code, notes, and snippets.

@Fordi
Created July 1, 2011 23:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fordi/1059603 to your computer and use it in GitHub Desktop.
Save Fordi/1059603 to your computer and use it in GitHub Desktop.
Crock32: The Base32 implementation outlined by Douglas Crockford
<?php
/**
* Spec here: http://www.crockford.com/wrmg/base32.html
* I took the liberty of collapsing the decode of 'u' into 'v's slot, since that matches the visual funnelling of L, I, and O.
*
* @package default
* @author Bryan Elliott
**/
abstract class Crock32 {
static private $encodeMap = array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','J','K','M','N','P','Q','R','S','T','V','W','X','Y','Z');
static private $decodeMap = array('L'=>1,'I'=>1,'O'=>0,'U'=>27);
static private function decodeMap($ch) {
if (empty(self::$decodeMap['a']))
forEach(self::$encodeMap as $i=>$v) self::$decodeMap[$v]=$i;
return self::$decodeMap[strToUpper($ch)];
}
static private function bitsFromString($str) {
$bStr = array();
for ($i=0; $i<strlen($str); $i++)
$bStr[]= str_pad(base_convert(ord($str[$i]), 10, 2), 8, '0', STR_PAD_LEFT);
return join('', $bStr);
}
static private function b32FromBits($bits) {
$bStr = str_split($bits, 5);
$enc='';
for($i=0; $i<count($bStr); $i++)
$enc.=self::$encodeMap[base_convert(str_pad($bStr[$i],5,'0'),2,10)];
return $enc;
}
static private function bitsFromb32($str) {
$bStr=array();
$str = preg_replace('/[^\d\w]/','',$str);
for ($i=0; $i<strlen($str); $i++)
$bStr[]=str_pad(base_convert(self::decodeMap($str[$i]),10,2),5,'0',STR_PAD_LEFT);
return join('', $bStr);
}
static private function stringFromBits($bits) {
$bStr = str_split(substr($bits,0,floor(strlen($bits)/8)*8), 8);
$ret = '';
for ($i=0; $i<count($bStr); $i++) {
$b = $bStr[$i];
$o = base_convert($b, 2, 10);
$c = chr($o);
$ret.=$c;
}
return $ret;
}
static function encode($str) {
return self::b32FromBits(self::bitsFromString($str));
}
static function decode($enc) {
return self::stringFromBits(self::bitsFromb32($enc));
}
}
/**
* Spec here: http://www.crockford.com/wrmg/base32.html
* I took the liberty of collapsing the decode of 'u' into 'v's slot, since that matches the visual funnelling of L, I, and O.
*
* @package default
* @author Bryan Elliott
**/
var Crock32 = (function (parseInt, length) {
var i,
encodeMap = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'.split(''),
decodeMap = { L:1,I:1,O:0,U:27 },
splitBy8 = /.{1,8}/g,
splitBy5 = /.{1,5}/g;
for (i in encodeMap) decodeMap[encodeMap[i]]=parseInt(i);
function pad(str, len, right) {
while (str[length] < len) str = (right?'':'0')+str+(right?'0':'');
return str;
}
return {
encode: function (str) {
var i, bStr = '', ret='';
for (i=0; i<str[length]; i++)
bStr+=pad(str.charCodeAt(i).toString(2), 8);
bStr = bStr.match(splitBy5);
for(i=0; i<bStr[length]; i++)
ret+=encodeMap[parseInt(pad(bStr[i], 5, 1),2)];
return ret;
},
decode: function (str) {
var i, bStr = '', bits, ret = '';
str = str.replace(/\W|_/g, '', str);
for (i=0; i<str[length]; i++)
bStr+=pad(decodeMap[str.charAt(i).toUpperCase()].toString(2),5);
bStr = bStr.substr(0,~~(bStr[length]/8)*8).match(splitBy8);
for (i=0; i<bStr[length]; i++)
ret+=String.fromCharCode(parseInt(bStr[i], 2));
return ret;
}
};
})(parseInt, 'length');
Javascript str_split:
String.prototype.strSplit = function (n) {
return this.match(new RegExp('.{1,'+(n||1)+'}', 'g'));
}
@sergeevabc
Copy link

Bryan, could you be so kind and improve JS version to encode Uint8array?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment