secure-pgp-storage is a class on top of OpenPGP.js
Table of Contents
The secure-pgp-storage class is designed to minimize code when working with the OpenPGP.js library.
The index.js
bundle works well in Node.js. It is used by default when you require('secure-pgp-storage')
in Node.js.
Install secure-pgp-storage using npm:
npm install secure-pgp-storage
And import it as a CommonJS module:
const sPGPs = require('secure-pgp-storage');
Here are some examples of how to use secure-pgp-storage. Please review the test.js file to understand how the secure-pgp-storage class works.
The createStorage
function creates a new ECC key pair and stores them in class variables.
(async () => {
await sPGPs.createStorage('John Smith', 'john.smith@gmail.com', '1q2w3e4r5t6y7u8i9o0p');
console.log('Nickname:', sPGPs.nickname);
console.log('E-mail:', sPGPs.email);
console.log('Fingerprint:', sPGPs.fingerprint);
console.log('Public key:');
console.log(sPGPs.publicKeyArmored);
})();
The createStorage
function creates a new ECC key pair and stores them in class variables.
(async () => {
console.log('All data is activated:', await sPGPs.checkAllData());
})();
Encryption will use the algorithm preferred by the public (encryption) key (defaults to aes256 for keys generated in OpenPGP.js). The signature
parameter is optional and is required for signing.
(async () => {
const recipientPublicKeyArmored = sPGPs.publicKeyArmored; // For example, we will use our public key.
let encrypted = await sPGPs.encryptMessage('Hello world!', recipientPublicKeyArmored, signature = true);
console.log('Encrypted message:');
console.log(encrypted);
console.log('Check message:', await sPGPs.checkMessage(encrypted));
})();
Decryption will use the algorithm used for encryption. The senderPublicKeyArmored
parameter is optional and required to verify the signature.
(async () => {
const senderPublicKeyArmored = sPGPs.publicKeyArmored;
let decrypted = await sPGPs.decryptMessage(encrypted, senderPublicKeyArmored);
console.log('Decrypted message:');
console.log(decrypted);
console.log(decrypted.data);
console.log(decrypted.signatures[0].keyID.toHex());
console.log(await decrypted.signatures[0].verified);
})();
By default, encryptMessageSymmetricallyWithCompression
will use openpgp.enums.compression.zlib
symmetric encryption compression.
(async () => {
encrypted = await sPGPs.encryptMessageSymmetricallyWithCompression('Hello again!', '1234567890');
console.log('Encrypted message:');
console.log(encrypted);
console.log('Check message:', await sPGPs.checkMessage(encrypted));
})();
(async () => {
decrypted = await sPGPs.decryptMessageSymmetricallyWithCompression(encrypted, '1234567890');
console.log('Decrypted message:');
console.log(decrypted);
})();
The encryptStorage
function puts publicKeyArmored
and privateKeyArmored
into JSON and encrypts them with symmetric encryption using the password that was used to create the key pair.
(async () => {
const encryptedStorage = await sPGPs.encryptStorage();
console.log('Encrypted storage:');
console.log(encryptedStorage);
console.log('Check message:', await sPGPs.checkMessage(encryptedStorage));
console.log('encodeURIComponent (for file href html):', await sPGPs.generateSecureFile());
})();
The eraseAllData
function clears class variables.
(async () => {
await sPGPs.eraseAllData();
console.log('Nickname:', sPGPs.nickname);
console.log('E-mail:', sPGPs.email);
console.log('Fingerprint:', sPGPs.fingerprint);
console.log('Public key:');
console.log(sPGPs.publicKeyArmored);
console.log('All data is activated:', await sPGPs.checkAllData());
})();
The decryptStorage
function decrypts a message with a key pair inside. After parsing the JSON and reading the keys, the class variables are filled.
(async () => {
const decryptedStorage = await sPGPs.decryptStorage(encryptedStorage, '1q2w3e4r5t6y7u8i9o0p');
console.log('Decrypted storage:', decryptedStorage);
console.log('Nickname:', sPGPs.nickname);
console.log('E-mail:', sPGPs.email);
console.log('Fingerprint:', sPGPs.fingerprint);
console.log('Public key:');
console.log(sPGPs.publicKeyArmored);
console.log('All data is activated:', await sPGPs.checkAllData());
})();
GNU Lesser General Public License (3.0 or any later version). Please take a look at the LICENSE file for more information.