mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-13 03:27:39 +03:00
Cleanup OpenPGP.js a bit.
openpgp.worker doesn't need webpack
This commit is contained in:
parent
5e63adc904
commit
8c454ccd9d
7 changed files with 212 additions and 207 deletions
|
|
@ -137,6 +137,11 @@ RainLoop 1.15 vs SnappyMail
|
||||||
|TOTAL |1.445.204 | 394.361 |425.477 |123.965 |353.301 |107.321 |
|
|TOTAL |1.445.204 | 394.361 |425.477 |123.965 |353.301 |107.321 |
|
||||||
|TOTAL (no admin) |1.189.690 | 344.490 |351.061 |109.194 |292.627 | 94.108 |
|
|TOTAL (no admin) |1.189.690 | 344.490 |351.061 |109.194 |292.627 | 94.108 |
|
||||||
|
|
||||||
|
|OpenPGP |RainLoop |Snappy |RL gzip |SM gzip |RL brotli |SM brotli |
|
||||||
|
|--------------- |--------: |--------: |------: |------: |--------: |--------: |
|
||||||
|
|openpgp.min.js | 330.742 | 330.300 |102.388 |102.388 | 84.241 | 84.121 |
|
||||||
|
|openpgp.worker | 1.499 | 1.125 | 824 | 567 | 695 | 467 |
|
||||||
|
|
||||||
For a user its around 66% smaller and faster than traditional RainLoop.
|
For a user its around 66% smaller and faster than traditional RainLoop.
|
||||||
|
|
||||||
### CSS changes
|
### CSS changes
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,8 @@ const jsServiceWorker = () => {
|
||||||
// OpenPGP
|
// OpenPGP
|
||||||
const jsOpenPGP = () => {
|
const jsOpenPGP = () => {
|
||||||
return gulp
|
return gulp
|
||||||
.src('vendors/openpgp-2.6.2/dist/openpgp.min.js')
|
.src('vendors/openpgp-2.6.2/dist/openpgp.js')
|
||||||
.pipe(gulp.dest(config.paths.staticMinJS));
|
.pipe(gulp.dest(config.paths.staticJS));
|
||||||
};
|
};
|
||||||
|
|
||||||
// OpenPGP Worker
|
// OpenPGP Worker
|
||||||
|
|
|
||||||
2
vendors/openpgp-2.6.2/dist/openpgp.min.js
vendored
2
vendors/openpgp-2.6.2/dist/openpgp.min.js
vendored
File diff suppressed because one or more lines are too long
8
vendors/openpgp-2.6.2/dist/openpgp.worker.js
vendored
8
vendors/openpgp-2.6.2/dist/openpgp.worker.js
vendored
|
|
@ -1,4 +1,3 @@
|
||||||
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
|
|
||||||
// GPG4Browsers - An OpenPGP implementation in javascript
|
// GPG4Browsers - An OpenPGP implementation in javascript
|
||||||
// Copyright (C) 2011 Recurity Labs GmbH
|
// Copyright (C) 2011 Recurity Labs GmbH
|
||||||
//
|
//
|
||||||
|
|
@ -16,10 +15,6 @@
|
||||||
// License along with this library; if not, write to the Free Software
|
// License along with this library; if not, write to the Free Software
|
||||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
/* globals self: true */
|
|
||||||
|
|
||||||
self.window = {}; // to make UMD bundles work
|
|
||||||
|
|
||||||
importScripts('openpgp.js');
|
importScripts('openpgp.js');
|
||||||
var openpgp = window.openpgp;
|
var openpgp = window.openpgp;
|
||||||
|
|
||||||
|
|
@ -32,7 +27,7 @@ openpgp.crypto.random.randomBuffer.init(MAX_SIZE_RANDOM_BUFFER);
|
||||||
* Handle messages from the main window.
|
* Handle messages from the main window.
|
||||||
* @param {Object} event Contains event type and data
|
* @param {Object} event Contains event type and data
|
||||||
*/
|
*/
|
||||||
self.onmessage = function(event) {
|
self.onmessage = event => {
|
||||||
var msg = event.data || {};
|
var msg = event.data || {};
|
||||||
|
|
||||||
switch (msg.event) {
|
switch (msg.event) {
|
||||||
|
|
@ -101,4 +96,3 @@ function response(event) {
|
||||||
}
|
}
|
||||||
self.postMessage(event, openpgp.util.getTransferables.call(openpgp.util, event.data));
|
self.postMessage(event, openpgp.util.getTransferables.call(openpgp.util, event.data));
|
||||||
}
|
}
|
||||||
},{}]},{},[1]);
|
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1 @@
|
||||||
/*! OpenPGP.js v2.6.2 - 2018-01-21 - this is LGPL licensed code, see LICENSE/our website http://openpgpjs.org/ for more information. */
|
importScripts("openpgp.min.js");var openpgp=window.openpgp,MIN_SIZE_RANDOM_BUFFER=4e4,MAX_SIZE_RANDOM_BUFFER=6e4;function configure(e){for(var n in e)openpgp.config[n]=e[n]}function seedRandom(e){e instanceof Uint8Array||(e=new Uint8Array(e)),openpgp.crypto.random.randomBuffer.set(e)}function delegate(e,n,o){"function"==typeof openpgp[n]?(o=openpgp.packet.clone.parseClonedPackets(o,n),openpgp[n](o).then((function(n){response({id:e,event:"method-return",data:openpgp.packet.clone.clonePackets(n)})})).catch((function(n){response({id:e,event:"method-return",err:n.message,stack:n.stack})}))):response({id:e,event:"method-return",err:"Unknown Worker Event"})}function response(e){openpgp.crypto.random.randomBuffer.size<MIN_SIZE_RANDOM_BUFFER&&self.postMessage({event:"request-seed"}),self.postMessage(e,openpgp.util.getTransferables.call(openpgp.util,e.data))}openpgp.crypto.random.randomBuffer.init(MAX_SIZE_RANDOM_BUFFER),self.onmessage=e=>{var n=e.data||{};switch(n.event){case"configure":configure(n.config);break;case"seed-random":seedRandom(n.buf);break;default:delegate(n.id,n.event,n.options||{})}};
|
||||||
!function e(n,r,t){function o(i,f){if(!r[i]){if(!n[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(a)return a(i,!0);var s=new Error("Cannot find module '"+i+"'");throw s.code="MODULE_NOT_FOUND",s}var u=r[i]={exports:{}};n[i][0].call(u.exports,function(e){var r=n[i][1][e];return o(r||e)},u,u.exports,e,n,r,t)}return r[i].exports}for(var a="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}({1:[function(e,n,r){function t(e){o.crypto.random.randomBuffer.size<a&&self.postMessage({event:"request-seed"}),self.postMessage(e,o.util.getTransferables.call(o.util,e.data))}self.window={},importScripts("openpgp.min.js");var o=window.openpgp,a=4e4;o.crypto.random.randomBuffer.init(6e4),self.onmessage=function(e){var n=e.data||{};switch(n.event){case"configure":!function(e){for(var n in e)o.config[n]=e[n]}(n.config);break;case"seed-random":!function(e){e instanceof Uint8Array||(e=new Uint8Array(e)),o.crypto.random.randomBuffer.set(e)}(n.buf);break;default:!function(e,n,r){"function"==typeof o[n]?(r=o.packet.clone.parseClonedPackets(r,n),o[n](r).then(function(n){t({id:e,event:"method-return",data:o.packet.clone.clonePackets(n)})}).catch(function(n){t({id:e,event:"method-return",err:n.message,stack:n.stack})})):t({id:e,event:"method-return",err:"Unknown Worker Event"})}(n.id,n.event,n.options||{})}}},{}]},{},[1]);
|
|
||||||
|
|
|
||||||
292
vendors/openpgp-2.6.2/src/keyring/keyring.js
vendored
292
vendors/openpgp-2.6.2/src/keyring/keyring.js
vendored
|
|
@ -34,92 +34,72 @@ import LocalStore from './localstore.js';
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {class} [storeHandler] class implementing loadPublic(), loadPrivate(), storePublic(), and storePrivate() methods
|
* @param {class} [storeHandler] class implementing loadPublic(), loadPrivate(), storePublic(), and storePrivate() methods
|
||||||
*/
|
*/
|
||||||
export default function Keyring(storeHandler) {
|
export default class Keyring
|
||||||
this.storeHandler = storeHandler || new LocalStore();
|
{
|
||||||
this.publicKeys = new KeyArray(this.storeHandler.loadPublic());
|
constructor(storeHandler) {
|
||||||
this.privateKeys = new KeyArray(this.storeHandler.loadPrivate());
|
this.storeHandler = storeHandler || new LocalStore();
|
||||||
|
this.publicKeys = new KeyArray(this.storeHandler.loadPublic());
|
||||||
|
this.privateKeys = new KeyArray(this.storeHandler.loadPrivate());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls the storeHandler to save the keys
|
||||||
|
*/
|
||||||
|
store() {
|
||||||
|
this.storeHandler.storePublic(this.publicKeys.keys);
|
||||||
|
this.storeHandler.storePrivate(this.privateKeys.keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear the keyring - erase all the keys
|
||||||
|
*/
|
||||||
|
clear() {
|
||||||
|
this.publicKeys.keys = [];
|
||||||
|
this.privateKeys.keys = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches the keyring for keys having the specified key id
|
||||||
|
* @param {String} keyId provided as string of lowercase hex number
|
||||||
|
* withouth 0x prefix (can be 16-character key ID or fingerprint)
|
||||||
|
* @param {Boolean} deep if true search also in subkeys
|
||||||
|
* @returns {Array<module:key.Key>|null} keys found or null
|
||||||
|
*/
|
||||||
|
getKeysForId(keyId, deep) {
|
||||||
|
var result = [];
|
||||||
|
result = result.concat(this.publicKeys.getForId(keyId, deep) || []);
|
||||||
|
result = result.concat(this.privateKeys.getForId(keyId, deep) || []);
|
||||||
|
return result.length ? result : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes keys having the specified key id from the keyring
|
||||||
|
* @param {String} keyId provided as string of lowercase hex number
|
||||||
|
* withouth 0x prefix (can be 16-character key ID or fingerprint)
|
||||||
|
* @returns {Array<module:key.Key>|null} keys found or null
|
||||||
|
*/
|
||||||
|
removeKeysForId(keyId) {
|
||||||
|
var result = [];
|
||||||
|
result = result.concat(this.publicKeys.removeForId(keyId) || []);
|
||||||
|
result = result.concat(this.privateKeys.removeForId(keyId) || []);
|
||||||
|
return result.length ? result : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all public and private keys
|
||||||
|
* @returns {Array<module:key.Key>} all keys
|
||||||
|
*/
|
||||||
|
getAllKeys() {
|
||||||
|
return this.publicKeys.keys.concat(this.privateKeys.keys);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Calls the storeHandler to save the keys
|
|
||||||
*/
|
|
||||||
Keyring.prototype.store = function () {
|
|
||||||
this.storeHandler.storePublic(this.publicKeys.keys);
|
|
||||||
this.storeHandler.storePrivate(this.privateKeys.keys);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clear the keyring - erase all the keys
|
|
||||||
*/
|
|
||||||
Keyring.prototype.clear = function() {
|
|
||||||
this.publicKeys.keys = [];
|
|
||||||
this.privateKeys.keys = [];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Searches the keyring for keys having the specified key id
|
|
||||||
* @param {String} keyId provided as string of lowercase hex number
|
|
||||||
* withouth 0x prefix (can be 16-character key ID or fingerprint)
|
|
||||||
* @param {Boolean} deep if true search also in subkeys
|
|
||||||
* @return {Array<module:key~Key>|null} keys found or null
|
|
||||||
*/
|
|
||||||
Keyring.prototype.getKeysForId = function (keyId, deep) {
|
|
||||||
var result = [];
|
|
||||||
result = result.concat(this.publicKeys.getForId(keyId, deep) || []);
|
|
||||||
result = result.concat(this.privateKeys.getForId(keyId, deep) || []);
|
|
||||||
return result.length ? result : null;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes keys having the specified key id from the keyring
|
|
||||||
* @param {String} keyId provided as string of lowercase hex number
|
|
||||||
* withouth 0x prefix (can be 16-character key ID or fingerprint)
|
|
||||||
* @return {Array<module:key~Key>|null} keys found or null
|
|
||||||
*/
|
|
||||||
Keyring.prototype.removeKeysForId = function (keyId) {
|
|
||||||
var result = [];
|
|
||||||
result = result.concat(this.publicKeys.removeForId(keyId) || []);
|
|
||||||
result = result.concat(this.privateKeys.removeForId(keyId) || []);
|
|
||||||
return result.length ? result : null;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get all public and private keys
|
|
||||||
* @return {Array<module:key~Key>} all keys
|
|
||||||
*/
|
|
||||||
Keyring.prototype.getAllKeys = function () {
|
|
||||||
return this.publicKeys.keys.concat(this.privateKeys.keys);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Array of keys
|
|
||||||
* @param {Array<module:key~Key>} keys The keys to store in this array
|
|
||||||
*/
|
|
||||||
function KeyArray(keys) {
|
|
||||||
this.keys = keys;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Searches all keys in the KeyArray matching the address or address part of the user ids
|
|
||||||
* @param {String} email email address to search for
|
|
||||||
* @return {Array<module:key~Key>} The public keys associated with provided email address.
|
|
||||||
*/
|
|
||||||
KeyArray.prototype.getForAddress = function(email) {
|
|
||||||
var results = [];
|
|
||||||
for (var i = 0; i < this.keys.length; i++) {
|
|
||||||
if (emailCheck(email, this.keys[i])) {
|
|
||||||
results.push(this.keys[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return results;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks a key to see if it matches the specified email address
|
* Checks a key to see if it matches the specified email address
|
||||||
* @private
|
* @private
|
||||||
* @param {String} email email address to search for
|
* @param {String} email email address to search for
|
||||||
* @param {module:key~Key} key The key to be checked.
|
* @param {module:key.Key} key The key to be checked.
|
||||||
* @return {Boolean} True if the email address is defined in the specified key
|
* @returns {Boolean} True if the email address is defined in the specified key
|
||||||
*/
|
*/
|
||||||
function emailCheck(email, key) {
|
function emailCheck(email, key) {
|
||||||
email = email.toLowerCase();
|
email = email.toLowerCase();
|
||||||
|
|
@ -142,7 +122,7 @@ function emailCheck(email, key) {
|
||||||
* @param {String} keyId provided as string of lowercase hex number
|
* @param {String} keyId provided as string of lowercase hex number
|
||||||
* withouth 0x prefix (can be 16-character key ID or fingerprint)
|
* withouth 0x prefix (can be 16-character key ID or fingerprint)
|
||||||
* @param {module:packet/secret_key|public_key|public_subkey|secret_subkey} keypacket The keypacket to be checked
|
* @param {module:packet/secret_key|public_key|public_subkey|secret_subkey} keypacket The keypacket to be checked
|
||||||
* @return {Boolean} True if keypacket has the specified keyid
|
* @returns {Boolean} True if keypacket has the specified keyid
|
||||||
*/
|
*/
|
||||||
function keyIdCheck(keyId, keypacket) {
|
function keyIdCheck(keyId, keypacket) {
|
||||||
if (keyId.length === 16) {
|
if (keyId.length === 16) {
|
||||||
|
|
@ -153,69 +133,95 @@ function keyIdCheck(keyId, keypacket) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches the KeyArray for a key having the specified key id
|
* Array of keys
|
||||||
* @param {String} keyId provided as string of lowercase hex number
|
* @param {Array<module:key.Key>} keys The keys to store in this array
|
||||||
* withouth 0x prefix (can be 16-character key ID or fingerprint)
|
|
||||||
* @param {Boolean} deep if true search also in subkeys
|
|
||||||
* @return {module:key~Key|null} key found or null
|
|
||||||
*/
|
*/
|
||||||
KeyArray.prototype.getForId = function (keyId, deep) {
|
class KeyArray
|
||||||
for (var i = 0; i < this.keys.length; i++) {
|
{
|
||||||
if (keyIdCheck(keyId, this.keys[i].primaryKey)) {
|
constructor(keys) {
|
||||||
return this.keys[i];
|
this.keys = keys;
|
||||||
}
|
}
|
||||||
if (deep && this.keys[i].subKeys) {
|
|
||||||
for (var j = 0; j < this.keys[i].subKeys.length; j++) {
|
|
||||||
if (keyIdCheck(keyId, this.keys[i].subKeys[j].subKey)) {
|
|
||||||
return this.keys[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Imports a key from an ascii armored message
|
* Searches all keys in the KeyArray matching the address or address part of the user ids
|
||||||
* @param {String} armored message to read the keys/key from
|
* @param {String} email email address to search for
|
||||||
* @return {Array<Error>|null} array of error objects or null
|
* @returns {Array<module:key.Key>} The public keys associated with provided email address.
|
||||||
*/
|
*/
|
||||||
KeyArray.prototype.importKey = function (armored) {
|
getForAddress(email) {
|
||||||
var imported = keyModule.readArmored(armored);
|
var results = [];
|
||||||
var that = this;
|
for (var i = 0; i < this.keys.length; i++) {
|
||||||
imported.keys.forEach(function(key) {
|
if (emailCheck(email, this.keys[i])) {
|
||||||
// check if key already in key array
|
results.push(this.keys[i]);
|
||||||
var keyidHex = key.primaryKey.getKeyId().toHex();
|
}
|
||||||
var keyFound = that.getForId(keyidHex);
|
}
|
||||||
if (keyFound) {
|
return results;
|
||||||
keyFound.update(key);
|
}
|
||||||
} else {
|
|
||||||
that.push(key);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return imported.err ? imported.err : null;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add key to KeyArray
|
* Searches the KeyArray for a key having the specified key id
|
||||||
* @param {module:key~Key} key The key that will be added to the keyring
|
* @param {String} keyId provided as string of lowercase hex number
|
||||||
* @return {Number} The new length of the KeyArray
|
* withouth 0x prefix (can be 16-character key ID or fingerprint)
|
||||||
*/
|
* @param {Boolean} deep if true search also in subkeys
|
||||||
KeyArray.prototype.push = function (key) {
|
* @returns {module:key.Key|null} key found or null
|
||||||
return this.keys.push(key);
|
*/
|
||||||
};
|
getForId(keyId, deep) {
|
||||||
|
for (var i = 0; i < this.keys.length; i++) {
|
||||||
|
if (keyIdCheck(keyId, this.keys[i].primaryKey)) {
|
||||||
|
return this.keys[i];
|
||||||
|
}
|
||||||
|
if (deep && this.keys[i].subKeys) {
|
||||||
|
for (var j = 0; j < this.keys[i].subKeys.length; j++) {
|
||||||
|
if (keyIdCheck(keyId, this.keys[i].subKeys[j].subKey)) {
|
||||||
|
return this.keys[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a key with the specified keyid from the keyring
|
* Imports a key from an ascii armored message
|
||||||
* @param {String} keyId provided as string of lowercase hex number
|
* @param {String} armored message to read the keys/key from
|
||||||
* withouth 0x prefix (can be 16-character key ID or fingerprint)
|
* @returns {Array<Error>|null} array of error objects or null
|
||||||
* @return {module:key~Key|null} The key object which has been removed or null
|
*/
|
||||||
*/
|
importKey(armored) {
|
||||||
KeyArray.prototype.removeForId = function (keyId) {
|
var imported = keyModule.readArmored(armored);
|
||||||
for (var i = 0; i < this.keys.length; i++) {
|
var that = this;
|
||||||
if (keyIdCheck(keyId, this.keys[i].primaryKey)) {
|
imported.keys.forEach(function(key) {
|
||||||
return this.keys.splice(i, 1)[0];
|
// check if key already in key array
|
||||||
}
|
var keyidHex = key.primaryKey.getKeyId().toHex();
|
||||||
}
|
var keyFound = that.getForId(keyidHex);
|
||||||
return null;
|
if (keyFound) {
|
||||||
};
|
keyFound.update(key);
|
||||||
|
} else {
|
||||||
|
that.push(key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return imported.err ? imported.err : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add key to KeyArray
|
||||||
|
* @param {module:key.Key} key The key that will be added to the keyring
|
||||||
|
* @returns {Number} The new length of the KeyArray
|
||||||
|
*/
|
||||||
|
push(key) {
|
||||||
|
return this.keys.push(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a key with the specified keyid from the keyring
|
||||||
|
* @param {String} keyId provided as string of lowercase hex number
|
||||||
|
* withouth 0x prefix (can be 16-character key ID or fingerprint)
|
||||||
|
* @returns {module:key.Key|null} The key object which has been removed or null
|
||||||
|
*/
|
||||||
|
removeForId(keyId) {
|
||||||
|
for (let i = 0; i < this.keys.length; i++) {
|
||||||
|
if (keyIdCheck(keyId, this.keys[i].primaryKey)) {
|
||||||
|
return this.keys.splice(i, 1)[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
105
vendors/openpgp-2.6.2/src/keyring/localstore.js
vendored
105
vendors/openpgp-2.6.2/src/keyring/localstore.js
vendored
|
|
@ -28,39 +28,6 @@ import config from '../config';
|
||||||
import * as keyModule from '../key.js';
|
import * as keyModule from '../key.js';
|
||||||
import util from '../util.js';
|
import util from '../util.js';
|
||||||
|
|
||||||
export default function LocalStore(prefix) {
|
|
||||||
prefix = prefix || 'openpgp-';
|
|
||||||
this.publicKeysItem = prefix + this.publicKeysItem;
|
|
||||||
this.privateKeysItem = prefix + this.privateKeysItem;
|
|
||||||
if (typeof window !== 'undefined' && window.localStorage) {
|
|
||||||
this.storage = window.localStorage;
|
|
||||||
} else {
|
|
||||||
this.storage = new (require('node-localstorage').LocalStorage)(config.node_store);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Declare the localstore itemnames
|
|
||||||
*/
|
|
||||||
LocalStore.prototype.publicKeysItem = 'public-keys';
|
|
||||||
LocalStore.prototype.privateKeysItem = 'private-keys';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load the public keys from HTML5 local storage.
|
|
||||||
* @return {Array<module:key~Key>} array of keys retrieved from localstore
|
|
||||||
*/
|
|
||||||
LocalStore.prototype.loadPublic = function () {
|
|
||||||
return loadKeys(this.storage, this.publicKeysItem);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load the private keys from HTML5 local storage.
|
|
||||||
* @return {Array<module:key~Key>} array of keys retrieved from localstore
|
|
||||||
*/
|
|
||||||
LocalStore.prototype.loadPrivate = function () {
|
|
||||||
return loadKeys(this.storage, this.privateKeysItem);
|
|
||||||
};
|
|
||||||
|
|
||||||
function loadKeys(storage, itemname) {
|
function loadKeys(storage, itemname) {
|
||||||
var armoredKeys = JSON.parse(storage.getItem(itemname));
|
var armoredKeys = JSON.parse(storage.getItem(itemname));
|
||||||
var keys = [];
|
var keys = [];
|
||||||
|
|
@ -78,24 +45,6 @@ function loadKeys(storage, itemname) {
|
||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves the current state of the public keys to HTML5 local storage.
|
|
||||||
* The key array gets stringified using JSON
|
|
||||||
* @param {Array<module:key~Key>} keys array of keys to save in localstore
|
|
||||||
*/
|
|
||||||
LocalStore.prototype.storePublic = function (keys) {
|
|
||||||
storeKeys(this.storage, this.publicKeysItem, keys);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves the current state of the private keys to HTML5 local storage.
|
|
||||||
* The key array gets stringified using JSON
|
|
||||||
* @param {Array<module:key~Key>} keys array of keys to save in localstore
|
|
||||||
*/
|
|
||||||
LocalStore.prototype.storePrivate = function (keys) {
|
|
||||||
storeKeys(this.storage, this.privateKeysItem, keys);
|
|
||||||
};
|
|
||||||
|
|
||||||
function storeKeys(storage, itemname, keys) {
|
function storeKeys(storage, itemname, keys) {
|
||||||
var armoredKeys = [];
|
var armoredKeys = [];
|
||||||
if (keys.length) {
|
if (keys.length) {
|
||||||
|
|
@ -107,3 +56,57 @@ function storeKeys(storage, itemname, keys) {
|
||||||
storage.removeItem(itemname);
|
storage.removeItem(itemname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default class LocalStore
|
||||||
|
{
|
||||||
|
constructor(prefix) {
|
||||||
|
prefix = prefix || 'openpgp-';
|
||||||
|
this.publicKeysItem = prefix + this.publicKeysItem;
|
||||||
|
this.privateKeysItem = prefix + this.privateKeysItem;
|
||||||
|
if (typeof window !== 'undefined' && window.localStorage) {
|
||||||
|
this.storage = window.localStorage;
|
||||||
|
} else {
|
||||||
|
this.storage = new (require('node-localstorage').LocalStorage)(config.node_store);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the public keys from HTML5 local storage.
|
||||||
|
* @return {Array<module:key~Key>} array of keys retrieved from localstore
|
||||||
|
*/
|
||||||
|
loadPublic() {
|
||||||
|
return loadKeys(this.storage, this.publicKeysItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the private keys from HTML5 local storage.
|
||||||
|
* @return {Array<module:key~Key>} array of keys retrieved from localstore
|
||||||
|
*/
|
||||||
|
loadPrivate() {
|
||||||
|
return loadKeys(this.storage, this.privateKeysItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the current state of the public keys to HTML5 local storage.
|
||||||
|
* The key array gets stringified using JSON
|
||||||
|
* @param {Array<module:key~Key>} keys array of keys to save in localstore
|
||||||
|
*/
|
||||||
|
storePublic(keys) {
|
||||||
|
storeKeys(this.storage, this.publicKeysItem, keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the current state of the private keys to HTML5 local storage.
|
||||||
|
* The key array gets stringified using JSON
|
||||||
|
* @param {Array<module:key~Key>} keys array of keys to save in localstore
|
||||||
|
*/
|
||||||
|
storePrivate(keys) {
|
||||||
|
storeKeys(this.storage, this.privateKeysItem, keys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Declare the localstore itemnames
|
||||||
|
*/
|
||||||
|
LocalStore.prototype.publicKeysItem = 'public-keys';
|
||||||
|
LocalStore.prototype.privateKeysItem = 'private-keys';
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue