Permalink
Browse files

First commit: basic password generator.

  • Loading branch information...
0 parents commit 06119f4c3039191e3932d16eac3e8ade26c1fb6c @jcoglan committed Dec 7, 2011
Showing with 170 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 .npmignore
  3. +64 −0 lib/vault.js
  4. +4 −0 lib/vault/browser.js
  5. +25 −0 package.json
  6. +35 −0 spec/browser.html
  7. +6 −0 spec/node.js
  8. +32 −0 spec/vault_spec.js
1 .gitignore
@@ -0,0 +1 @@
+node_modules
3 .npmignore
@@ -0,0 +1,3 @@
+.git
+node_modules
+spec
64 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 lib/vault/browser.js
@@ -0,0 +1,4 @@
+Vault.sha256 = function(string) {
+ return Crypto.SHA256(string);
+};
+
25 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 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 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 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.