diff --git a/lib/assets/libs/hashlib.js b/lib/assets/libs/hashlib.js new file mode 100644 --- /dev/null +++ b/lib/assets/libs/hashlib.js @@ -0,0 +1,126 @@ +var $module=(function($B){ + +var _b_ = $B.builtins + +var $s=[] +for(var $b in _b_) $s.push('var ' + $b +'=_b_["'+$b+'"]') +eval($s.join(';')) + +var $mod = { + + __getattr__ : function(attr){ + if (attr == 'new') return $hashlib_new; + return this[attr] + }, + md5: function(obj) {return $hashlib_new('md5', obj)}, + sha1: function(obj) {return $hashlib_new('sha1', obj)}, + sha224: function(obj) {return $hashlib_new('sha224', obj)}, + sha256: function(obj) {return $hashlib_new('sha256', obj)}, + sha384: function(obj) {return $hashlib_new('sha384', obj)}, + sha512: function(obj) {return $hashlib_new('sha512', obj)}, + + algorithms_guaranteed: ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'], + algorithms_available: ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'] +} + + +//todo: eventually move this function to a "utility" file or use ajax module? +function $get_CryptoJS_lib(alg) { + var imp=$importer() + var $xmlhttp=imp[0], fake_qs=imp[1], timer=imp[2], res=null + + $xmlhttp.onreadystatechange = function(){ + if($xmlhttp.readyState==4){ + window.clearTimeout(timer) + if($xmlhttp.status==200 || $xmlhttp.status==0){res=$xmlhttp.responseText} + else{ + // don't throw an exception here, it will not be caught (issue #30) + res = Error() + res.name = 'NotFoundError' + res.message = "No CryptoJS lib named '"+alg+"'" + } + } + } + + $xmlhttp.open('GET', $B.brython_path+'libs/crypto_js/rollups/'+alg+'.js'+fake_qs,false) + if('overrideMimeType' in $xmlhttp){$xmlhttp.overrideMimeType("text/plain")} + $xmlhttp.send() + if(res.constructor===Error){throw res} // module not found + + try{ + eval(res + "; $B.CryptoJS=CryptoJS;") + } catch (err) { + throw Error("JS Eval Error", "Cannot eval CryptoJS algorithm '" + alg + "' : error:" + err); + } +} + +function bytes2WordArray(obj){ + // Transform a bytes object into an instance of class WordArray + // defined in CryptoJS + if(!_b_.isinstance(obj, _b_.bytes)){ + throw _b_.TypeError("expected bytes, got "+ + $B.get_class(obj).__name__) + } + + var words = [] + for(var i=0;i