Skip to content

Instantly share code, notes, and snippets.

@jimsynz
Created June 22, 2012 03:29
Show Gist options
  • Save jimsynz/2970011 to your computer and use it in GitHub Desktop.
Save jimsynz/2970011 to your computer and use it in GitHub Desktop.
@jcoglan's Vault project as a squashed html file for quick downloads and cacheability.
<!doctype html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0"><title>Vault</title><script type="text/javascript">var CryptoJS=CryptoJS||function(e,t){var n={},r=n.lib={},i=r.Base=function(){function e(){}return{extend:function(t){e.prototype=this;var n=new e;return t&&n.mixIn(t),n.$super=this,n},create:function(){var e=this.extend();return e.init.apply(e,arguments),e},init:function(){},mixIn:function(e){for(var t in e)e.hasOwnProperty(t)&&(this[t]=e[t]);e.hasOwnProperty("toString")&&(this.toString=e.toString)},clone:function(){return this.$super.extend(this)}}}(),s=r.WordArray=i.extend({init:function(e,n){e=this.words=e||[],this.sigBytes=n!=t?n:4*e.length},toString:function(e){return(e||u).stringify(this)},concat:function(e){var t=this.words,n=e.words,r=this.sigBytes,e=e.sigBytes;this.clamp();if(r%4)for(var i=0;i<e;i++)t[r+i>>>2]|=(n[i>>>2]>>>24-8*(i%4)&255)<<24-8*((r+i)%4);else if(65535<n.length)for(i=0;i<e;i+=4)t[r+i>>>2]=n[i>>>2];else t.push.apply(t,n);return this.sigBytes+=e,this},clamp:function(){var t=this.words,n=this.sigBytes;t[n>>>2]&=4294967295<<32-8*(n%4),t.length=e.ceil(n/4)},clone:function(){var e=i.clone.call(this);return e.words=this.words.slice(0),e},random:function(t){for(var n=[],r=0;r<t;r+=4)n.push(4294967296*e.random()|0);return s.create(n,t)}}),o=n.enc={},u=o.Hex={stringify:function(e){for(var t=e.words,e=e.sigBytes,n=[],r=0;r<e;r++){var i=t[r>>>2]>>>24-8*(r%4)&255;n.push((i>>>4).toString(16)),n.push((i&15).toString(16))}return n.join("")},parse:function(e){for(var t=e.length,n=[],r=0;r<t;r+=2)n[r>>>3]|=parseInt(e.substr(r,2),16)<<24-4*(r%8);return s.create(n,t/2)}},a=o.Latin1={stringify:function(e){for(var t=e.words,e=e.sigBytes,n=[],r=0;r<e;r++)n.push(String.fromCharCode(t[r>>>2]>>>24-8*(r%4)&255));return n.join("")},parse:function(e){for(var t=e.length,n=[],r=0;r<t;r++)n[r>>>2]|=(e.charCodeAt(r)&255)<<24-8*(r%4);return s.create(n,t)}},f=o.Utf8={stringify:function(e){try{return decodeURIComponent(escape(a.stringify(e)))}catch(t){throw Error("Malformed UTF-8 data")}},parse:function(e){return a.parse(unescape(encodeURIComponent(e)))}},l=r.BufferedBlockAlgorithm=i.extend({reset:function(){this._data=s.create(),this._nDataBytes=0},_append:function(e){"string"==typeof e&&(e=f.parse(e)),this._data.concat(e),this._nDataBytes+=e.sigBytes},_process:function(t){var n=this._data,r=n.words,i=n.sigBytes,o=this.blockSize,u=i/(4*o),u=t?e.ceil(u):e.max((u|0)-this._minBufferSize,0),t=u*o,i=e.min(4*t,i);if(t){for(var a=0;a<t;a+=o)this._doProcessBlock(r,a);a=r.splice(0,t),n.sigBytes-=i}return s.create(a,i)},clone:function(){var e=i.clone.call(this);return e._data=this._data.clone(),e},_minBufferSize:0});r.Hasher=l.extend({init:function(){this.reset()},reset:function(){l.reset.call(this),this._doReset()},update:function(e){return this._append(e),this._process(),this},finalize:function(e){return e&&this._append(e),this._doFinalize(),this._hash},clone:function(){var e=l.clone.call(this);return e._hash=this._hash.clone(),e},blockSize:16,_createHelper:function(e){return function(t,n){return e.create(n).finalize(t)}},_createHmacHelper:function(e){return function(t,n){return c.HMAC.create(e,n).finalize(t)}}});var c=n.algo={};return n}(Math);(function(){var e=CryptoJS,t=e.lib,n=t.WordArray,t=t.Hasher,r=[],i=e.algo.SHA1=t.extend({_doReset:function(){this._hash=n.create([1732584193,4023233417,2562383102,271733878,3285377520])},_doProcessBlock:function(e,t){for(var n=this._hash.words,i=n[0],s=n[1],o=n[2],u=n[3],a=n[4],f=0;80>f;f++){if(16>f)r[f]=e[t+f]|0;else{var l=r[f-3]^r[f-8]^r[f-14]^r[f-16];r[f]=l<<1|l>>>31}l=(i<<5|i>>>27)+a+r[f],l=20>f?l+((s&o|~s&u)+1518500249):40>f?l+((s^o^u)+1859775393):60>f?l+((s&o|s&u|o&u)-1894007588):l+((s^o^u)-899497514),a=u,u=o,o=s<<30|s>>>2,s=i,i=l}n[0]=n[0]+i|0,n[1]=n[1]+s|0,n[2]=n[2]+o|0,n[3]=n[3]+u|0,n[4]=n[4]+a|0},_doFinalize:function(){var e=this._data,t=e.words,n=8*this._nDataBytes,r=8*e.sigBytes;t[r>>>5]|=128<<24-r%32,t[(r+64>>>9<<4)+15]=n,e.sigBytes=4*t.length,this._process()}});e.SHA1=t._createHelper(i),e.HmacSHA1=t._createHmacHelper(i)})(),function(){var e=CryptoJS,t=e.enc.Utf8;e.algo.HMAC=e.lib.Base.extend({init:function(e,n){e=this._hasher=e.create(),"string"==typeof n&&(n=t.parse(n));var r=e.blockSize,i=4*r;n.sigBytes>i&&(n=e.finalize(n));for(var s=this._oKey=n.clone(),o=this._iKey=n.clone(),u=s.words,a=o.words,f=0;f<r;f++)u[f]^=1549556828,a[f]^=909522486;s.sigBytes=o.sigBytes=i,this.reset()},reset:function(){var e=this._hasher;e.reset(),e.update(this._iKey)},update:function(e){return this._hasher.update(e),this},finalize:function(e){var t=this._hasher,e=t.finalize(e);return t.reset(),t.finalize(this._oKey.clone().concat(e))}})}(),function(){var e=CryptoJS,t=e.lib,n=t.Base,r=t.WordArray,t=e.algo,i=t.HMAC,s=t.PBKDF2=n.extend({cfg:n.extend({keySize:4,hasher:t.SHA1,iterations:1}),init:function(e){this.cfg=this.cfg.extend(e)},compute:function(e,t){for(var n=this.cfg,s=i.create(n.hasher,e),o=r.create(),u=r.create([1]),a=o.words,f=u.words,l=n.keySize,n=n.iterations;a.length<l;){var c=s.update(t).finalize(u);s.reset();for(var h=c.words,p=h.length,d=c,v=1;v<n;v++){d=s.finalize(d),s.reset();for(var m=d.words,g=0;g<p;g++)h[g]^=m[g]}o.concat(c),f[0]++}return o.sigBytes=4*l,o}});e.PBKDF2=function(e,t,n){return s.create(n).compute(e,t)}}(),typeof module=="object"&&(module.exports=CryptoJS);var Vault=function(e){this._phrase=e.phrase||"",this._length=e.length||Vault.DEFAULT_LENGTH,this._repeat=e.repeat||Vault.DEFAULT_REPEAT,this._allowed=Vault.ALL.slice(),this._required=[];var t=Vault.TYPES,n;for(var r=0,i=t.length;r<i;r++)n=e[t[r].toLowerCase()],n===0?this.subtract(Vault[t[r]]):typeof n=="number"&&this.require(Vault[t[r]],n);var i=this._length-this._required.length;while(i>=0&&i--)this._required.push(this._allowed)};Vault.UUID="e87eb0f4-34cb-46b9-93ad-766c5ab063e7",Vault.DEFAULT_LENGTH=20,Vault.DEFAULT_REPEAT=0,Vault.LOWER="abcdefghijklmnopqrstuvwxyz".split(""),Vault.UPPER="ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""),Vault.ALPHA=Vault.LOWER.concat(Vault.UPPER),Vault.NUMBER="0123456789".split(""),Vault.ALPHANUM=Vault.ALPHA.concat(Vault.NUMBER),Vault.SPACE=[" "],Vault.DASH=["-","_"],Vault.SYMBOL="!\"#$%&'()*+,./:;<=>?@[\]^{|}~".split("").concat(Vault.DASH),Vault.ALL=Vault.ALPHANUM.concat(Vault.SPACE).concat(Vault.SYMBOL),Vault.TYPES="LOWER UPPER NUMBER SPACE DASH SYMBOL".split(" "),Vault.extend=function(e,t){for(var n in t)e.hasOwnProperty(n)||(e[n]=t[n]);return e},Vault.createHash=function(e,t,n){var r=typeof CryptoJS!="undefined"?CryptoJS:require("./crypto-js-3.0.2"),i=(n||256)/4;return r.PBKDF2(e,t,{keySize:Math.ceil(i/8),iterations:8}).toString()},Vault.indexOf=function(e,t){if(e.indexOf)return e.indexOf(t);for(var n=0,r=e.length;n<r;n++)if(e[n]===t)return n;return-1},Vault.map=function(e,t,n){if(e.map)return e.map(t,n);var r=[];for(var i=0,s=e.length;i<s;i++)r.push(t.call(n,e[i]));return r},Vault.pbkdf2=function(e,t,n,r,i){if(typeof require=="function"&&require("crypto").pbkdf2)return require("crypto").pbkdf2(e,t,r,4*n,function(e,t){i(e,(new Buffer(t,"binary")).toString("hex"))});var s=typeof CryptoJS!="undefined"?CryptoJS:require("./crypto-js-3.0.2"),o=s.PBKDF2(e,t,{keySize:n,iterations:r});i(null,o.toString())},Vault.toBits=function(e){var t=parseInt(e,16).toString(2);while(t.length<4)t="0"+t;return t},Vault.prototype.subtract=function(e,t){if(!e)return;t=t||this._allowed;for(var n=0,r=e.length;n<r;n++){var i=Vault.indexOf(t,e[n]);i>=0&&t.splice(i,1)}return t},Vault.prototype.require=function(e,t){if(!e)return;while(t--)this._required.push(e)},Vault.prototype.entropy=function(){var e=0;for(var t=0,n=this._required.length;t<n;t++)e+=Math.ceil(Math.log(t+1)/Math.log(2)),e+=Math.ceil(Math.log(this._required[t].length)/Math.log(2));return e},Vault.prototype.generate=function(e){if(this._required.length>this._length)throw new Error("Length too small to fit all required characters");if(this._allowed.length===0)throw new Error("No characters available to create a password");var t=this._required.slice(),n=new Vault.Stream(this._phrase,e,this.entropy()),r="",i,s,o,u,a;while(r.length<this._length){i=n.generate(t.length),s=t.splice(i,1)[0],o=r.charAt(r.length-1),u=this._repeat-1,a=o&&u>=0;while(a&&u--)a=a&&r.charAt(r.length+u-this._repeat)===o;a&&(s=this.subtract([o],s.slice())),i=n.generate(s.length),r+=s[i]}return r},Vault.Stream=function(e,t,n){this._phrase=e,this._service=t;var r=Vault.createHash(e,t+Vault.UUID,2*n),i=Vault.map(r.split(""),Vault.toBits).join("").split("");this._bases={2:Vault.map(i,function(e){return parseInt(e,2)})}},Vault.Stream.prototype.generate=function(e,t,n){t=t||2;var r=e,i=Math.ceil(Math.log(e)/Math.log(t)),s=Math.pow(t,i)-e,o;e:while(r>=e){o=this._shift(t,i);if(!o)return n?e:null;r=this._evaluate(o,t);if(r>=e){if(s===1)continue e;this._push(s,r-e),r=this.generate(e,s,!0)}}return r},Vault.Stream.prototype._evaluate=function(e,t){var n=0,r=e.length;while(r--)n+=e[r]*Math.pow(t,e.length-(r+1));return n},Vault.Stream.prototype._push=function(e,t){this._bases[e]=this._bases[e]||[],this._bases[e].push(t)},Vault.Stream.prototype._shift=function(e,t){var n=this._bases[e];return!n||n.length<t?null:n.splice(0,t)},typeof module=="object"&&(module.exports=Vault)</script><style type="text/css">
body,input{background:#fff;color:#666;font:18px/1.5 FreeSans,Helvetica,Arial,sans-serif;margin:0;padding:0;}.sub{margin:0 auto;padding:0 1em 1.5em;width:400px;}.form{background:#f4f4f4;border-top:1px solid #999;border-bottom:1px solid #999;}.info .sub{padding-bottom:0;}h1{color:#333;font-size:1.4em;}h2,p{font-size:1em;margin:1em 0;}a{color:#39f;font-weight:bold;text-decoration:none;}label,input.text,input.password{display:block;width:100%;}label{color:#555;font-size:.9em;margin-top:1em;}label span{color:#999;}.form input{background:#f4f4f4;}input.text,input.password{border:none;border-bottom:1px solid #666;font-size:1em;}.password{;}.password input.text,.password input.password{display:inline;margin-right:1em;width:80%;}.password .show{float:right;font-size:.6em;font-weight:bold;padding-top:.5em;text-transform:uppercase;}.password .show label{display:inline;}.length,.repeat{float:left;margin-bottom:1em;width:47%;}.length{clear:both;margin-right:6%;}table label{display:inline;}table input.text{display:inline;width:1em;}table{border-collapse:collapse;clear:both;margin-top:1em;width:100%;}table label{font-size:1em;}table th,table td{border-left:1px solid #ccc;padding:4px 0;text-align:center;}table th{color:#555;font-size:.6em;font-weight:bold;text-transform:uppercase;width:30px;}table thead th{text-transform:none;}table thead tr{border-bottom:1px solid #ccc;}table tbody th{text-align:left;}table th:first-child,table td:first-child{border:none;}@media(max-width:800px){body{font-size:14px;}.sub{margin:none;width:auto;}}
</style></head><body><div class="info"><div class="sub"><p><strong>Using the same password for every site?</strong> Well, you shouldn’t. Enter your passphrase and the service you’re logging into, and get a unique password for that service. Come back here next time you need to look up your passwords.</p><p>Still have <a href="./faq.html">questions</a>?</p></div></div><div class="form"><div class="sub"> <label for="service">Service name <span>e.g. “twitter”</span></label> <input class="text" name="service" id="service" autocomplete="off"><div class="password"> <label for="passphrase">Passphrase <span>e.g. “I’m the best 17-year old ever.”</span></label> <input type="password" class="password" name="passphrase" id="passphrase" autocomplete="off"> <input class="text" name="passphrase-text" id="passphrase-text" autocomplete="off"> <span class="show"> <label for="show-passphrase">Show</label> <input type="checkbox" name="show-passphrase" id="show-passphrase"> </span></div><div class="length"> <label for="vlength">Password length <span>e.g. 12</span></label> <input class="text" name="vlength" id="vlength" value="20" autocomplete="off"></div><div class="repeat"> <label for="repeat">Max repetition <span>e.g. 2</span></label> <input class="text" name="repeat" id="repeat" value="" autocomplete="off"></div><div class="field"><table><thead><tr><td></td><th scope="col">a–z</th><th scope="col">A–Z</th><th scope="col">0–9</th><th scope="col">- / _</th><th scope="col">SPACE</th><th scope="col">!@#$%</th></tr></thead><tbody><tr><th scope="row"> <label for="required">Required</label> (<input class="text" name="required" id="required" value="2" autocomplete="off">)</th><td><input type="radio" name="lower" value="required"></td><td><input type="radio" name="upper" value="required"></td><td><input type="radio" name="number" value="required"></td><td><input type="radio" name="dash" value="required"></td><td><input type="radio" name="space" value="required"></td><td><input type="radio" name="symbol" value="required"></td></tr><tr><th scope="row">Allowed</th><td><input type="radio" name="lower" value="allowed" checked></td><td><input type="radio" name="upper" value="allowed" checked></td><td><input type="radio" name="number" value="allowed" checked></td><td><input type="radio" name="dash" value="allowed" checked></td><td><input type="radio" name="space" value="allowed" checked></td><td><input type="radio" name="symbol" value="allowed" checked></td></tr><tr><th scope="row">Forbidden</th><td><input type="radio" name="lower" value="forbidden"></td><td><input type="radio" name="upper" value="forbidden"></td><td><input type="radio" name="number" value="forbidden"></td><td><input type="radio" name="dash" value="forbidden"></td><td><input type="radio" name="space" value="forbidden"></td><td><input type="radio" name="symbol" value="forbidden"></td></tr></tbody></table></div></div></div><div class="generator"><div class="sub"> <label for="word">Generated password</label> <input class="text" id="word" autocomplete="off"></div></div><script type="text/javascript">var $=function(e){return document.getElementById(e)},on=function(e,t,n){if(!e)return;e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent("on"+t,n)},getRadio=function(e){var t=document.getElementsByTagName("input"),n;for(var r=0,i=t.length;r<i;r++){n=t[r];if(n.type==="radio"&&n.name===e&&n.checked)return n.value}},setRadio=function(e,t){var n=document.getElementsByTagName("input"),r;for(var i=0,s=n.length;i<s;i++){r=n[i];if(r.type==="radio"&&r.name===e)switch(r.value){case"required":r.checked=t&&t>0;break;case"allowed":r.checked=t===undefined;break;case"forbidden":r.checked=t===0}}},togglePassword=function(e){var t=$(e),n=$(e+"-text"),r=$("show-"+e);n.style.display="none",on(r,"click",function(){r.checked?(t.style.display="none",n.style.display=""):(t.style.display="",n.style.display="none")}),on(t,"keyup",function(){n.value=t.value}),on(n,"keyup",function(){t.value=n.value})};togglePassword("passphrase");var service=$("service"),phrase=$("passphrase"),required=$("required"),length=$("vlength"),repeat=$("repeat"),word=$("word"),wordText=$("word-text"),TYPES="lower upper number dash space symbol".split(" ");service.focus();var getSettings=function(){var e=parseInt(length.value,10),t=parseInt(repeat.value,10),n=parseInt(required.value,10),r={phrase:phrase.value,length:e,repeat:t},i;for(var s=0,o=TYPES.length;s<o;s++)i=getRadio(TYPES[s]),i==="forbidden"?r[TYPES[s]]=0:i==="required"&&(r[TYPES[s]]=n);return r},update=function(){var e=getSettings(),t;try{t=new Vault(e),word.value=t.generate(service.value)}catch(n){word.value="!! "+n.message}},inputs=document.getElementsByTagName("input");for(var i=0,n=inputs.length;i<n;i++){if(inputs[i].id==="word"||inputs[i].type==="checkbox")continue;on(inputs[i],"keyup",update),on(inputs[i],"change",update)}var insert=function(){var e=word.value.replace(/'/g,"\'");chrome.tabs.executeScript(null,{code:"(document.activeElement||{}).value = '"+e+"';"}),window.close()},insertPassword=$("insert-password");on(insertPassword,"click",function(e){e.preventDefault(),insert()}),on(service,"keydown",function(e){e.keyCode===13&&insert()}),on(phrase,"keydown",function(e){e.keyCode===13&&insert()});var saveSettings=$("save-settings");on(saveSettings,"click",function(e){e.preventDefault();var t=getSettings();Config.edit(function(e){e.global.phrase=t.phrase,delete t.phrase,e.services[service.value]=t},function(){})});var clearSettings=$("clear-settings");on(clearSettings,"click",function(e){e.preventDefault(),Config.clear()}),typeof LocalStore=="object"&&(Config=new Vault.Config(LocalStore),Config.read("",function(e,t){var n;phrase.value=t.phrase||"",on(service,"keyup",function(){Config.read(service.value,function(e,t){var r=2;t.length&&(length.value=t.length);for(var i=0,s=TYPES.length;i<s;i++)n=t[TYPES[i]],setRadio(TYPES[i],n),n&&n>0&&(r=n);required.value=r,length.value=t.length||Vault.DEFAULT_LENGTH,repeat.value=t.repeat||Vault.DEFAULT_REPEAT,update()})})}))</script></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment