gpg-ts
Version:
GPG encryption and decryption in node.js by way of the gpg command-line tool. Now with TypeScript.
272 lines • 10.2 kB
JavaScript
/*!
* node-gpg-ts
* Copyright(c) 2020 Stevan Dedovic
* MIT Licensed
*
* node-gpg-ts was ported from node-gpg
* Copyright(c) 2011 Nicholas Penree <drudge@conceited.net>
* MIT Licensed
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GPG = void 0;
const fs = __importStar(require("fs"));
const spawnGPG_1 = require("./spawnGPG");
const keyRegex = /key (.*?):/;
class GPG {
/**
* Raw call to gpg.
*
* @param {String} stdin String to send to stdin.
* @param {Array} [args] Array of arguments.
* @param {Function} [fn] Callback.
* @api public
*/
static call(stdin, args, fn) {
spawnGPG_1.spawnGPG(stdin, args, [], fn);
}
/**
* Raw streaming call to gpg. Reads from input file and writes to output file.
*
* @param input
* @param output
* @param {Array} [args] Array of arguments.
* @param {Function} [fn] Callback.
* @api public
*/
static callStreaming(input, output, args, fn) {
spawnGPG_1.spawnStreamingGPG({ source: input, dest: output }, args, fn);
}
/**
* Encrypt source file passed as `options.source` and store it in a file specified in `options.dest`.
*
* @param {Opts} options Should contain 'source' and 'dest' keys.
* @param {Function} [fn] Callback.
* @api public
*/
static encryptToFile(options, fn) {
spawnGPG_1.spawnStreamingGPG(options, ['--encrypt'], fn);
}
/**
* Encrypt source `file` and pass the encrypted contents to the callback `fn`.
*
* @param {PathLike} file Filename.
* @param {Function} [fn] Callback containing the encrypted file contents.
* @api public
*/
static encryptFile(file, fn) {
fs.readFile(file, (err, data) => {
if (err)
fn(err, null, null);
else
GPG.encrypt(data, [], fn);
});
}
/**
* Encrypt source stream passed as `options.source` and pass it to the stream specified in `options.dest`.
* Is basically the same method as `encryptToFile()`.
*
* @param {Opts} options Should contain 'source' and 'dest' keys that are streams.
* @param {Function} [fn] Callback.
* @api public
*/
static encryptToStream(options, fn) {
spawnGPG_1.spawnStreamingGPG(options, ['--encrypt'], fn);
}
/**
* Encrypt source `stream` and pass the encrypted contents to the callback `fn`.
*
* @param {ReadableStream} stream Stream to read from.
* @param {Array} [args] Array of additonal gpg arguments.
* @param {Function} [fn] Callback containing the encrypted file contents.
* @api public
*/
static encryptStream(stream, args, fn) {
const chunks = [];
stream.on('data', function (chunk) {
chunks.push(chunk);
});
stream.on('end', function () {
GPG.encrypt(Buffer.concat(chunks), args, fn);
});
stream.on('error', fn);
}
/**
* Encrypt `str` and pass the encrypted version to the callback `fn`.
*
* @param {String|Buffer} str String to encrypt.
* @param {Array} [args] Array of additonal gpg arguments.
* @param {Function} [fn] Callback containing the encrypted Buffer.
* @api public
*/
static encrypt(str, args, fn) {
spawnGPG_1.spawnGPG(str, ['--encrypt'], args, fn);
}
/**
* Decrypt `str` and pass the decrypted version to the callback `fn`.
*
* @param {String|Buffer} str Data to decrypt.
* @param {Array} [args] Array of additonal gpg arguments.
* @param {Function} [fn] Callback containing the decrypted Buffer.
* @api public
*/
static decrypt(str, args, fn) {
spawnGPG_1.spawnGPG(str, ['--decrypt'], args, fn);
}
/**
* Decrypt source `file` and pass the decrypted contents to the callback `fn`.
*
* @param {PathLike} file Filename.
* @param {Function} fn Callback containing the decrypted file contents.
* @api public
*/
static decryptFile(file, fn) {
fs.readFile(file, (err, content) => {
if (err)
return fn(err, null, null);
else
return GPG.decrypt(content, [], fn);
});
}
/**
* Decrypt source file passed as `options.source` and store it in a file specified in `options.dest`.
*
* @param {Opts} options Should contain 'source' and 'dest' keys.
* @param {Function} fn Callback
* @api public
*/
static decryptToFile(options, fn) {
spawnGPG_1.spawnStreamingGPG(options, ['--decrypt'], fn);
}
/**
* Decrypt source `stream` and pass the decrypted contents to the callback `fn`.
*
* @param {ReadableStream} stream Stream to read from.
* @param {Array} [args] Array of additonal gpg arguments.
* @param {Function} [fn] Callback containing the decrypted file contents.
* @api public
*/
static decryptStream(stream, args, fn) {
const chunks = [];
stream.on('data', function (chunk) {
chunks.push(chunk);
});
stream.on('end', function () {
GPG.decrypt(Buffer.concat(chunks), args, fn);
});
stream.on('error', fn);
}
/**
* Decrypt source stream passed as `options.source` and pass it to the stream specified in `options.dest`.
* This is basically the same method as `decryptToFile()`.
*
* @param {Opts} options Should contain 'source' and 'dest' keys that are streams.
* @param {Function} fn Callback
* @api public
*/
static decryptToStream(options, fn) {
spawnGPG_1.spawnStreamingGPG(options, ['--decrypt'], fn);
}
/**
* Clearsign `str` and pass the signed message to the callback `fn`.
*
* @param {String|Buffer} str String to clearsign.
* @param {Array} [args] Array of additonal gpg arguments.
* @param {Function} fn Callback containing the signed message Buffer.
* @api public
*/
static clearsign(str, args, fn) {
spawnGPG_1.spawnGPG(str, ['--clearsign'], args, fn);
}
/**
* Verify `str` and pass the output to the callback `fn`.
*
* @param {String|Buffer} str Signature to verify.
* @param {Array} [args] Array of additonal gpg arguments.
* @param {Function} [fn] Callback containing the signed message Buffer.
* @api public
*/
static verifySignature(str, args, fn) {
// Set logger fd, verify otherwise outputs to stderr for whatever reason
const defaultArgs = ['--logger-fd', '1', '--verify'];
spawnGPG_1.spawnGPG(str, defaultArgs, args, fn);
}
/**
* Add a key to the keychain by filename.
*
* @param {PathLike} fileName Key filename.
* @param {Array} [args] Array of additonal gpg arguments.
* @param {Function} [fn] Callback containing the signed message Buffer.
* @api public
*/
static importKeyFromFile(fileName, args, fn) {
fs.readFile(fileName, (readErr, str) => {
if (readErr)
fn(readErr, null, null);
else
GPG.importKey(str.toString("utf-8"), args, fn);
});
}
/**
* Add an ascii-armored key to gpg. Expects the key to be passed as input.
*
* @param {String | Buffer} keyStr Key string (armored).
* @param {Array} args Optional additional arguments to pass to gpg.
* @param {Function} fn Callback containing the signed message Buffer.
* @api public
*/
static importKey(keyStr, args, fn) {
// Set logger fd, verify otherwise outputs to stderr for whatever reason
const defaultArgs = ['--logger-fd', '1', '--import'];
let resultStr;
spawnGPG_1.spawnGPG(keyStr, defaultArgs, args, (importError, result) => {
if (importError) {
// Ignorable errors
if (/already in secret keyring/.test(importError.message)) {
resultStr = importError.message;
}
else {
fn(importError, null, null);
}
}
else {
// Grab key fingerprint and send it back as second arg
const match = result.toString().match(keyRegex);
fn(null, resultStr || result.toString(), match && match[1]);
}
});
}
/**
* Removes a key by fingerprint. Warning: this will remove both pub and privkeys!
*
* @param {String} keyID Key fingerprint.
* @param {Array} [args] Array of additonal gpg arguments.
* @param {Function} fn Callback containing the signed message Buffer.
* @api public
*/
static removeKey(keyID, args, fn) {
// Set logger fd, verify otherwise outputs to stderr for whatever reason
const defaultArgs = ['--logger-fd', '1', '--delete-secret-and-public-key'];
spawnGPG_1.spawnGPG(keyID, defaultArgs, args, fn);
}
}
exports.GPG = GPG;
//# sourceMappingURL=gpg.js.map