Skip to content

Instantly share code, notes, and snippets.

@mwgamera
Created August 29, 2012 23:00
Show Gist options
  • Save mwgamera/3520024 to your computer and use it in GitHub Desktop.
Save mwgamera/3520024 to your computer and use it in GitHub Desktop.
klg.passwordEntropy
(function(m){"use strict";function n(g,c){var d=0;c.d.i(g.length);g.unshift(255);for(var a=2;a<=g.length;a++){var f=g.slice(0,a);f.length>c.length&&(f=f.slice(f.length-c.length,f.length));d+=i(f,c)}setTimeout(function(){c.d.e()},0);return d}function i(g,c){for(var d,a=0;1<g.length;){if((d=c.get(g))!==c.ZERO)return a+d[0];a+=c.get(g.slice(0,g.length-1))[1];g=g.slice(1)}(d=c.get(g))!==c.ZERO?(a+=Math.log((c.a+2)/(c.b+1))/Math.log(2),a+=d[0],c.b++):(a+=Math.log((c.a+2)/(c.a-c.b+1))/Math.log(2),a+=c.j);c.a++;return a}
function o(){var g={},c=2048,d=0;this.i=function(a){c=a>c?a:c};this.set=function(a,f){return(g[a]=[d++,f])[1]};this.get=function(a){if(void 0!==g[a])return g[a][0]=d++,g[a][1]};this.e=function(){var a=[],f=0,b;for(b in g)g.hasOwnProperty(b)&&(a[f++]=[g[b][0],b]);if(a.length>c&&(a.sort(function(b,f){return b[0]-f[0]}),a.length>c))for(f=1;f<c-a.length;f++)delete g[a[f][1]]}}var p=function(){function g(f){var b=[],a=[],e=0,c=0,g=RegExp("[!#%,:;=@_]","g"),j=g.exec(f);if(!j||j.index)b.push([0,0],a=[0,
0,0]);if(j){do{var i=[0,0],l=[0,0,0],e=e+(j.index-c)/4;i[0]=e;a[1]=e-1;e+=d.g(f,j.index);l[0]=e;i[1]=e-1;l[2]=c=d.lastIndex;b.push(i,l);a=l}while(null!=(j=g.exec(f)))}a[1]=e-1+(f.length-c)/4;return b}var c=[Number.POSITIVE_INFINITY,0],d=function(){var a=function(){var b=new ("undefined"!=typeof Uint8Array?Uint8Array:Array)(256),a;for(a=0;a<b.length;a++)b[a]=-1;for(a=0;64>a;a++)b["+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charCodeAt(a)]=a;for(a=0;9>a;a++)b["!#%,:;=@_".charCodeAt(a)]=
a+1;return b}();return{f:function(b,c){if(void 0!==c)this.lastIndex=c;var e=a[b.charCodeAt(this.lastIndex++)&255],d=e>>>3,e=(e&7)<<6|a[b.charCodeAt(this.lastIndex++)&255];return 0==d?0==e?0:e/2048:(1+e/512)*Math.pow(2,d-3)},g:function(b,c){if(void 0!==c)this.lastIndex=c;var e=a[b.charCodeAt(this.lastIndex++)];if(9>e)return e;for(e=0;this.lastIndex<b.length;){var d=a[b.charCodeAt(this.lastIndex++)],e=32*e;if(0==(d&32))return e+d+9;e+=d-31}throw"Unexpected end of string";},lastIndex:0}}(),a=function(){return function(a,
b,g){var e,k=0,i=b.length-1;if(0>g||g>b[i][1])return c;for(;;)if(e=Math.floor((k+i)/2),g<b[e][0])i=e-1;else if(g>b[e][1])k=e+1;else{if(e&1){k=Array(2);d.lastIndex=b[e][2]+4*(g-b[e][0]);for(b=0;2>b;b++)k[b]=d.f(a);b=k}else b=c;return b}}}();return function(f){var b=g(f);this.ZERO=c;this.k=2;this.get=function(c){var e,d=0;for(e=0;e<c.length;e++)d*=256,d+=c[e];return a(f,b,d)};this.c=function(){for(var d="",e=0;256>e;e++)a(f,b,e)!=c&&(d+=String.fromCharCode(e));return d}();this.j=Math.log((224>this.c.length?
224:256)-this.c.length)/Math.log(2);this.length=Math.ceil(Math.log(b[b.length-1][1])/Math.log(256));this.reset=function(){this.b=this.a=this.c.length};this.reset()}}(),i=function(g){return function(c,d){var a=c+"\u0001"+(""+(d.a+2)/(d.a-d.b+1)),f=d.d.get(a);if(void 0!==f)return d.a=f[1],d.b=f[2],f[0];f=g(c,d);d.d.set(a,[f,d.a,d.b]);return f}}(i);return m.passwordEntropy=function(g){var c=new p(g);c.d=new o;return function(d){c.reset();var a,f,b,g=[];for(a=0;a<d.length;a++){b=d.charCodeAt(a);if(55296<=
b&&57344>b){if(56320<=b)throw"Unpaired surrogate at "+a;f=d.charCodeAt(++a);if(56320>f||57344<=f)throw"Unpaired surrogate at "+a;b=1024*(b-55296)+(f-56320)+65536}if(65534==b||65535==b)throw"Non-character at "+a;f=128>b?0:2048>b?1:65536>b?2:2097152>b?3:67108864>b?4:5;for(g.push([0,192,224,240,248,252][f]^b>>6*f);f--;)g.push(128^b>>6*f&63)}return n(g,c)}}})("undefined"!==typeof module&&module.exports||window.klg||(window.klg={}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment