Skip to content

Instantly share code, notes, and snippets.

@flesler
Last active October 30, 2015 14:03
Show Gist options
  • Save flesler/30209e193b2552fa72bf to your computer and use it in GitHub Desktop.
Save flesler/30209e193b2552fa72bf to your computer and use it in GitHub Desktop.
Bookmarklet to generate deterministic passwords based on a service name
<p>Minified and ready to be dragged to bookmarks toolbar<br />
Remember to customize the 4 parameters at the end</p>
<a href="javascript:(function(c,f,g,e){var a=location.hostname.split('.');do var b=a.pop();while(a.length&&3>=b.length);this._custom_?b=prompt('Write the service name',b):this._custom_=!0;var a=b,d;for(d in e)a=a.replace(new RegExp(d,'i'),e[d]);a=a.replace(/[a-z]/,function(a){return a.toUpperCase()});a=f+a;a+=g.slice(-(c-a.length));a=a.slice(0,c);alert('Service:'+b+'\nPassword:'+a)})(10,'Gp','_T8e.P2j!',{i:'!',a:'@',s:'$',l:1,z:2,e:3,g:6,t:7,b:8,o:0});">
Generate Pass
</a>
<p>Uncompressed source code</p>
<pre>
(function(length, prefix, suffix, map) {
/* 1.0 */
var p = location.hostname.split('.');
// Find service name
do { var service = p.pop(); }
while (p.length && service.length <= 3);
// First time assume domain
if (!this._custom_) {
this._custom_ = true;
} else {
// If clicked again let them fill the name themselves
service = prompt('Write the service name', service);
}
var pass = service;
// Replace characters, only once per character
for (var c in map) {
pass = pass.replace(new RegExp(c, 'i'), map[c]);
}
// Upper case first lower case char
pass = pass.replace(/[a-z]/, function(c) { return c.toUpperCase(); });
// Add prefix
pass = prefix + pass;
// Fill with suffix chars up to desired length
var diff = length - pass.length;
pass += suffix.slice(-diff);
// Crop to desired maximum length
pass = pass.slice(0, length);
alert('Service:'+service+'\nPassword:' + pass);
// NOTE: Change these values to customize your pass
})(10, 'Gp', '_T8e.P2j!', {
i: '!',
a: '@',
s: '$',
l: 1,
z: 2,
e: 3,
g: 6,
t: 7,
b: 8,
o: 0
});
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment