@quan-to/chevronlib
Version:
Chevron GPG Library Wrapper for Node.JS
251 lines • 9.72 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const lib = require('bindings')('chevron');
// Ensure that the library has been loaded
lib.__loadnative(__dirname);
/**
* Checks if the data string is a base64 encoded payload
*
* @param {string} data - A string to be tested
* @returns {boolean} - True if the string is a valid base64 format
*/
const isBase64 = (data) => {
const base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
return base64regex.test(data);
};
exports.isBase64 = isBase64;
/**
* Returns all fingerprints contained in the specified key
* @param {string} asciiArmoredKey - The private / public key you want the fingerprints in ASCII Armored Format
* @returns {string[]} the fingerprints in the specified keys
*/
const getKeyFingerprints = (asciiArmoredKey) => lib.getKeyFingerprints(asciiArmoredKey).split(",");
exports.getKeyFingerprints = getKeyFingerprints;
/**
* Loads the specified key into memory store for later use
*
* Both public and private keys can be loaded using loadKey
* @param {string} asciiArmoredKey - The private / public key you want the fingerprints in ASCII Armored Format
* @returns {Promise<string>} the first fingerprint of the loaded key
*/
const loadKey = function (asciiArmoredKey) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
lib.loadKey(asciiArmoredKey, (error) => {
if (error) {
reject(error);
}
else {
const fps = getKeyFingerprints(asciiArmoredKey);
resolve(fps[0]);
}
});
});
});
};
exports.loadKey = loadKey;
/**
* Signs the specified data using a pre-loaded and pre-unlocked key specified by fingerprint
*
* The key should be previously loaded with loadKey and unlocked with unlockKey
* The data should be always encoded as base64
*
* @param {string} data - Base64 Encoded Data to be signed
* @param {string} fingerprint - Fingerprint of the key used to sign data
* @returns {Promise<string>} - The ASCII Armored PGP Signature
*/
const signData = function (data, fingerprint) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
if (!isBase64(data)) {
return reject('Expected a base64 encoded data');
}
lib.signData(data, fingerprint, (error, result) => {
if (error) {
return reject(error);
}
return resolve(result);
});
});
});
};
exports.signData = signData;
/**
* Verifies the signature of a payload and returns true if it's valid.
*
* The data should be always encoded as base64
* The signature field can be in ASCII Armored Format or base64 encoded binary PGP Signature
*
* @param {string} data - Base64 Encoded Data to be signed
* @param {string} signature - A ASCII Armored Format or Base64 Encoded Binary PGP Signature
* @returns {Promise<boolean>}
*/
const verifySignature = function (data, signature) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
if (!isBase64(data)) {
return reject('Expected a base64 encoded data');
}
lib.verifySignature(data, signature, (error, result) => {
if (error) {
if (error.indexOf('invalid signature') > -1) {
return resolve(false);
}
return reject(error);
}
return resolve(result);
});
});
});
};
exports.verifySignature = verifySignature;
/**
* Unlocks a pre-loaded private key with the specified password
*
* The private key should be pre-loaded with loadKey function
*
* @param {string} fingerprint - Fingerprint of the private key that should be unlocked for use
* @param {string} password - Password of the private key
* @returns {Promise}
*/
const unlockKey = function (fingerprint, password) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
lib.unlockKey(fingerprint, password, (error, result) => {
if (error) {
reject(error);
}
else {
resolve(result);
}
});
});
});
};
exports.unlockKey = unlockKey;
/**
* Generates a new PGP Private Key by the specified password, identifier and bits (key-length)
*
* The Identifier should be in one of the following formats:
* - "Name"
* - "Name <email>"
* The key-length (bits parameter) should not be less than 2048
* The key is not automatically loaded into memory after generation
*
* @param {string} password - The password to encrypt the private key
* @param {string} identifier - The identifier of the key
* @param {number} bits - Number of bits of the RSA Key (recommended 3072)
* @returns {Promise<string>} - The generated private key
*/
const generateKey = function (password, identifier, bits) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
lib.generateKey(password, identifier, bits, (error, result) => {
if (error) {
reject(error);
}
else {
resolve(result);
}
});
});
});
};
exports.generateKey = generateKey;
/**
* Returns a ASCII Armored public key of a pre-loaded key
*
* The public/private key should be pre-loaded with loadKey
*
* @param {string} fingerprint - Fingerprint to fetch the public key
* @returns {string} - The public key
*/
const getPublicKey = (fingerprint) => lib.getPublicKey(fingerprint);
exports.getPublicKey = getPublicKey;
/**
* Signs the specified data using a pre-loaded and pre-unlocked key specified by fingerprint
* and returns in Quanto Signature Format
*
* The key should be previously loaded with loadKey and unlocked with unlockKey
* The data should be always encoded as base64
*
* @param {string} data - Base64 Encoded Data to be signed
* @param {string} fingerprint - Fingerprint of the key used to sign data
* @returns {Promise<string>} - The ASCII Armored PGP Signature
*/
const quantoSignData = function (data, fingerprint) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
if (!isBase64(data)) {
return reject('Expected a base64 encoded data');
}
lib.quantoSignData(data, fingerprint, (error, result) => {
if (error) {
return reject(error);
}
return resolve(result);
});
});
});
};
exports.quantoSignData = quantoSignData;
/**
* Verifies the signature in Quanto Signature Format of a payload and returns true if it's valid.
*
* The data should be always encoded as base64
* The signature field can be in ASCII Armored Format or base64 encoded binary PGP Signature
*
* @param {string} data - Base64 Encoded Data to be signed
* @param {string} signature - A ASCII Armored Format or Base64 Encoded Binary PGP Signature
* @returns {Promise<boolean>}
*/
const quantoVerifySignature = function (data, signature) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
if (!isBase64(data)) {
return reject('Expected a base64 encoded data');
}
lib.quantoVerifySignature(data, signature, (error, result) => {
if (error) {
if (error.indexOf('invalid signature') > -1) {
return resolve(false);
}
return reject(error);
}
return resolve(result);
});
});
});
};
exports.quantoVerifySignature = quantoVerifySignature;
/**
* Changes a private key password
*
* @param {string} keyData - The private key
* @param {string} currentPassword - The current password of the key
* @param {string} newPassword - The new password for the key
* @returns {Promise<string>>} the same private key encrypted with the newPassword
*/
const changeKeyPassword = function (keyData, currentPassword, newPassword) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
lib.changeKeyPassword(keyData, currentPassword, newPassword, (error, result) => {
if (error) {
return reject(error);
}
return resolve(result);
});
});
});
};
exports.changeKeyPassword = changeKeyPassword;
//# sourceMappingURL=index.js.map