Fix nodejs support

This commit is contained in:
Robert Nelson 2014-01-09 07:51:05 -08:00
parent 24d0c173a0
commit 5768fd5b23
2 changed files with 10 additions and 16 deletions

View File

@ -25,10 +25,8 @@
var type_mpi = require('../type/mpi.js');
var nodeCrypto = null;
if (typeof window === undefined) {}
try {
if (typeof window === 'undefined') {
nodeCrypto = require('crypto');
} catch (e) {
}
module.exports = {
@ -81,9 +79,9 @@ module.exports = {
* @param {Uint32Array} buf
*/
getRandomValues: function(buf) {
try {
if (nodeCrypto == null) {
window.crypto.getRandomValues(buf);
} catch (e) {
} else {
var bytes = nodeCrypto.randomBytes(4);
buf[0] = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
}

View File

@ -25,6 +25,11 @@ module.exports = LocalStore;
var openpgp = require('openpgp');
function LocalStore() {
if (typeof window != 'undefined' && window.localStorage) {
this.storage = window.localStorage;
} else {
this.storage = new (require('node-localstorage').LocalStorage)(openpgp.config.node_store);
}
}
/**
@ -32,16 +37,7 @@ function LocalStore() {
* @return {Array<module:key~Key>} array of keys retrieved from localstore
*/
LocalStore.prototype.load = function () {
var storage = null;
try {
storage = window.localStorage;
} catch (e) {
}
if (storage === null) {
storage = new (require('node-localstorage').LocalStorage)(openpgp.config.node_store);
}
var armoredKeys = JSON.parse(storage.getItem("armoredKeys"));
var armoredKeys = JSON.parse(this.storage.getItem('armoredKeys'));
var keys = [];
if (armoredKeys !== null && armoredKeys.length !== 0) {
var key;
@ -63,5 +59,5 @@ LocalStore.prototype.store = function (keys) {
for (var i = 0; i < keys.length; i++) {
armoredKeys.push(keys[i].armor());
}
window.localStorage.setItem("armoredKeys", JSON.stringify(armoredKeys));
this.storage.setItem('armoredKeys', JSON.stringify(armoredKeys));
};