Skip to content

Commit

Permalink
Make the Vault.generate() algorithm work in IE.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Jun 7, 2012
1 parent 40364fc commit 32e6cd5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
20 changes: 18 additions & 2 deletions chrome/lib/vault.js
Expand Up @@ -50,6 +50,22 @@ Vault.createHash = function(key, message, entropy) {
return CJS.PBKDF2(key, message, {keySize: Math.ceil(digits / 8), iterations: 16}).toString();
};

Vault.indexOf = function(list, item) {
if (list.indexOf) return list.indexOf(item);
for (var i = 0, n = list.length; i < n; i++) {
if (list[i] === item) return i;
}
return -1;
};

Vault.map = function(list, callback, context) {
if (list.map) return list.map(callback, context);
var result = [];
for (var i = 0, n = list.length; i < n; i++)
result.push(callback.call(context, list[i]));
return result;
};

Vault.pbkdf2 = function(password, salt, keylen, iterations, callback) {
if (typeof require === 'function' && require('crypto').pbkdf2)
return require('crypto').pbkdf2(password, salt, iterations, 4 * keylen, function(error, key) {
Expand All @@ -72,7 +88,7 @@ Vault.prototype.subtract = function(charset, allowed) {
if (!charset) return;
allowed = allowed || this._allowed;
for (var i = 0, n = charset.length; i < n; i++) {
var index = allowed.indexOf(charset[i]);
var index = Vault.indexOf(allowed, charset[i]);
if (index >= 0) allowed.splice(index, 1);
}
return allowed;
Expand All @@ -97,7 +113,7 @@ Vault.prototype.generate = function(service) {
throw new Error('Length too small to fit all required characters');

var hex = Vault.createHash(this._phrase, service + Vault.UUID, this.entropy()),
bits = hex.split('').map(Vault.toBits).join(''),
bits = Vault.map(hex.split(''), Vault.toBits).join(''),
result = '',
offset = 0,
index, charset, previous, i, same, charbits, code;
Expand Down
20 changes: 18 additions & 2 deletions lib/vault.js
Expand Up @@ -50,6 +50,22 @@ Vault.createHash = function(key, message, entropy) {
return CJS.PBKDF2(key, message, {keySize: Math.ceil(digits / 8), iterations: 16}).toString();
};

Vault.indexOf = function(list, item) {
if (list.indexOf) return list.indexOf(item);
for (var i = 0, n = list.length; i < n; i++) {
if (list[i] === item) return i;
}
return -1;
};

Vault.map = function(list, callback, context) {
if (list.map) return list.map(callback, context);
var result = [];
for (var i = 0, n = list.length; i < n; i++)
result.push(callback.call(context, list[i]));
return result;
};

Vault.pbkdf2 = function(password, salt, keylen, iterations, callback) {
if (typeof require === 'function' && require('crypto').pbkdf2)
return require('crypto').pbkdf2(password, salt, iterations, 4 * keylen, function(error, key) {
Expand All @@ -72,7 +88,7 @@ Vault.prototype.subtract = function(charset, allowed) {
if (!charset) return;
allowed = allowed || this._allowed;
for (var i = 0, n = charset.length; i < n; i++) {
var index = allowed.indexOf(charset[i]);
var index = Vault.indexOf(allowed, charset[i]);
if (index >= 0) allowed.splice(index, 1);
}
return allowed;
Expand All @@ -97,7 +113,7 @@ Vault.prototype.generate = function(service) {
throw new Error('Length too small to fit all required characters');

var hex = Vault.createHash(this._phrase, service + Vault.UUID, this.entropy()),
bits = hex.split('').map(Vault.toBits).join(''),
bits = Vault.map(hex.split(''), Vault.toBits).join(''),
result = '',
offset = 0,
index, charset, previous, i, same, charbits, code;
Expand Down

0 comments on commit 32e6cd5

Please sign in to comment.