Skip to content

Commit

Permalink
First commit: basic password generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Dec 7, 2011
0 parents commit 06119f4
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
3 changes: 3 additions & 0 deletions .npmignore
@@ -0,0 +1,3 @@
.git
node_modules
spec
64 changes: 64 additions & 0 deletions lib/vault.js
@@ -0,0 +1,64 @@
var Vault = function(settings) {
this._phrase = settings.phrase || '';
this._length = settings.length || Vault.DEFAULT_LENGTH;
this._charset = Vault.ALL.slice();
this._charbits = Math.log(this._charset.length) / Math.log(2);
};

Vault.UUID = '05c704ed-d4b9-4b40-b282-ba6a1c78183f';
Vault.DEFAULT_LENGTH = 20;

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.sha256 = function(string) {
var sha256 = require('crypto').createHash('sha256');
sha256.update(string);
return sha256.digest('hex');
};

Vault.toBits = function(digit) {
var string = parseInt(digit, 16).toString(2);
while (string.length < 4) string = '0' + string;
return string;
};

Vault.prototype.generate = function(password) {
var string = this._phrase + Vault.UUID + password,
hex = Vault.sha256(string),
bits = hex.split('').map(Vault.toBits).join(''),
result = '',
index = 0,
charbits, offset;

while (result.length < this._length) {
charbits = this.generateCharBits(bits, index);
index += charbits.length;
offset = parseInt(charbits.replace(/^0*(.+)$/, '$1'), 2);
result += this._charset[offset];
}

return result;
};

Vault.prototype.generateCharBits = function(bits, index) {
var size = Math.ceil(this._charbits),
chunk = bits.substr(index, size);

if (chunk.charAt(0) === '0') return chunk;

var value = parseInt(chunk, 2);
if (value >= this._charset.length) size -= 1;
return bits.substr(index, size);
};

if (typeof module === 'object')
module.exports = Vault;

4 changes: 4 additions & 0 deletions lib/vault/browser.js
@@ -0,0 +1,4 @@
Vault.sha256 = function(string) {
return Crypto.SHA256(string);
};

25 changes: 25 additions & 0 deletions package.json
@@ -0,0 +1,25 @@
{ "name" : "vault"
, "description" : "Generates safe passwords for the web"
, "homepage" : "http://github.com/jcoglan/vault"
, "author" : "James Coglan <jcoglan@gmail.com> (http://jcoglan.com/)"
, "keywords" : ["security", "passwords"]

, "version" : "0.1.0"
, "engines" : {"node": ">=0.4.0"}
, "main" : "./index.js"
, "bin" : {"vault": "./bin/vault"}
, "devDependencies" : {"jsclass": ""}

, "bugs" : "http://github.com/jcoglan/vault/issues"

, "licenses" : [ { "type" : "MIT"
, "url" : "http://www.opensource.org/licenses/mit-license.php"
}
]

, "repositories" : [ { "type" : "git"
, "url" : "git://github.com/jcoglan/vault.git"
}
]
}

35 changes: 35 additions & 0 deletions spec/browser.html
@@ -0,0 +1,35 @@
<!doctype html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Vault tests</title>
</head>
<body>

<script type="text/javascript" src="../node_modules/jsclass/min/loader.js"></script>
<script type="text/javascript">
JS.Packages(function() { with(this) {
file('http://crypto-js.googlecode.com/files/2.3.0-crypto-sha256.js')
.provides('Crypto')

loader(function(callback) {
load('../lib/vault.js', function() {
load('../lib/vault/browser.js', callback)
})
})
.provides('Vault')
.requires('Crypto')

file('./vault_spec.js')
.provides('VaultSpec')
.requires('JS.Test', 'Vault')
}})
</script>

<script type="text/javascript">
JS.require('VaultSpec', function() { JS.Test.autorun() })
</script>

</body>
</html>

6 changes: 6 additions & 0 deletions spec/node.js
@@ -0,0 +1,6 @@
require('jsclass')
JS.require('JS.Test')
JS.ENV.Vault = require('../lib/vault')
require('./vault_spec')
JS.Test.autorun()

32 changes: 32 additions & 0 deletions spec/vault_spec.js
@@ -0,0 +1,32 @@
var PHRASE = "She cells C shells bye the sea shoars"

JS.ENV.VaultSpec = JS.Test.describe("Vault", function() { with(this) {
before(function() { this.vault = new Vault(this.options()) })

define("options", function() { return {} })

describe("with a passphrase", function() { with(this) {
define("options", function() {
return {phrase: PHRASE}
})

it("generates a password", function() { with(this) {
assertEqual( 'L}%";X7-2nYy1}!C^X2_', vault.generate("google") )
}})

it("generates a different password for each service", function() { with(this) {
assertEqual( "96j4-6F']svoy1Gkif@3", vault.generate("twitter") )
}})
}})

describe("with a length", function() { with(this) {
define("options", function() {
return {phrase: PHRASE, length: 4}
})

it("generates a password of the given length", function() { with(this) {
assertEqual( 'L}%"', vault.generate("google") )
}})
}})
}})

0 comments on commit 06119f4

Please sign in to comment.