Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First commit: basic password generator.
- Loading branch information
0 parents
commit 06119f4
Showing
8 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.git | ||
node_modules | ||
spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Vault.sha256 = function(string) { | ||
return Crypto.SHA256(string); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require('jsclass') | ||
JS.require('JS.Test') | ||
JS.ENV.Vault = require('../lib/vault') | ||
require('./vault_spec') | ||
JS.Test.autorun() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") ) | ||
}}) | ||
}}) | ||
}}) | ||
|